Kavvson Empcraft
Kavvson Empcraft

Reputation: 443

Creating new array from 2 existing arrays - one of them is the key other value

My "main" array looks like this - var_dump($main)

[zlec_addresoperator] => and
[filtervalue0] => Test
[filtercondition0] => CONTAINS
[filteroperator0] => 1
[filterdatafield0] => zlec_addres
[zlec_nroperator] => and
[filtervalue1] => SecondVal
[filtercondition1] => CONTAINS
[filteroperator1] => 1
[filterdatafield1] => zlec_nr

I want to build a new array as

array( filterdatafield0 = > filtervalue0 , filterdatafield1 = > filtervalue1)

etc

First of all I decided to filter out what I wan't with the following codes. Creating new arrays to keep the data I wan't, so $arraykeys will contain the filterdatafield.{1,2} values. In this case it will be zlec_addres and zlec_nr. The second $arrayvalue will keep the filtervalue.{1,2} which is the value for the filter.

$newarray = array();
$arraykeys = array();
$arrayvalue = array();

foreach($_GET as $key => $value):

 if(preg_match("/^filterdatafield.{1,2}$/",$key)>0) {
        // $key is matched by the regex
        $arrayvalue[] = $value;
    }


    if(preg_match("/^filtervalue.{1,2}$/",$key)>0) {
        // $key is matched by the regex
        $arraykeys[] = $key;
    }
endforeach;
foreach($arraykeys as $a){
    $newarray[$a] = $arrayvalue;
}

So the desired output would be

array(
zlec_addres => 'Test', zlec_nr = 'SecondVal'
)

Now it is

array(12) {
  ["filtervalue0"]=>
  array(12) {
    [0]=>
    string(11) "zlec_addres"
    [1]=>
    string(7) "zlec_nr"
...
  }
  ["filtervalue1"]=>
  array(12) {
    [0]=>
    string(11) "zlec_addres"
    [1]=>
    string(7) "zlec_nr"
...
  }

Upvotes: 1

Views: 57

Answers (2)

HenryTK
HenryTK

Reputation: 1287

    $newarray = array();
    $arraykeys = array();
    $arrayvalue = array();

    foreach($_GET as $key => $value){

     if(preg_match("/^filterdatafield.{1,2}$/",$key)>0) {
            // $key is matched by the regex
            $arraykeys[] = $value;
        }


        if(preg_match("/^filtervalue.{1,2}$/",$key)>0) {
            // $key is matched by the regex
            $arrayvalues[] = $value;
        }
    }
$newArray = array_combine($arraykeys, $arrayvalues);

Upvotes: 1

Rizier123
Rizier123

Reputation: 59681

This should work for you:

Just grab your keys which you want with preg_grep() and then array_combine() both arrays together.

<?php

    $arr = [
        "zlec_addresoperator" => "and",
        "filtervalue0" => "Test",
        "filtercondition0" => "CONTAINS",
        "filteroperator0" => "1",
        "filterdatafield0" => "zlec_addres",
        "zlec_nroperator" => "and",
        "filtervalue1" => "SecondVal",
        "filtercondition1" => "CONTAINS",
        "filteroperator1" => "1",
        "filterdatafield1" => "zlec_nr",
    ];

    $newArray = array_combine(
        preg_grep("/^filterdatafield\d+$/", array_keys($arr)),
        preg_grep("/^filtervalue\d+$/", array_keys($arr))
    );

    print_r($newArray);

?>

output:

Array
(
    [filterdatafield0] => filtervalue0
    [filterdatafield1] => filtervalue1
)

Upvotes: 0

Related Questions