Compare the structure of php arrays regardless of content

I have two arrays that I want to compare with their structure, i.e. the same keys. I tried to use array_diff_key

but the problem is one array is defined like this:

    $fields = array('id' , 'site', 'placement', 'device', 'source', 'campaign', 'url', 'country', 'dof_count', 'dof_idx', 'active');

      

so when i use var_dump()

i get this result:

{
  [0]=>
  string(2) "id"
  [1]=>
  string(4) "site"
  [2]=>
  string(9) "placement"
  [3]=>
  string(6) "device"
  [4]=>
  string(6) "source"
  [5]=>
  string(8) "campaign"
  [6]=>
  string(3) "url"
  [7]=>
  string(7) "country"
  [8]=>
  string(9) "dof_count"
  [9]=>
  string(7) "dof_idx"
  [10]=>
  string(6) "active"
}

      

and the other is created by the function and returned like this:

{
  ["id"]=>
  NULL
  ["site"]=>
  NULL
  ["placement"]=>
  NULL
  ["device"]=>
  NULL
  ["source"]=>
  NULL
  ["campaign"]=>
  NULL
  ["url"]=>
  NULL
  ["country"]=>
  NULL
  ["dof_count"]=>
  int(0)
  ["dof_idx"]=>
  NULL
  ["active"]=>
  NULL
}

      

so while the two arrays have the same structure array_diff_key

won't help. is there a way in php to compare this array structure while ignoring the content, which in my case is all zero and one int in the second array?

+3


source to share


3 answers


You can just use array_diff

along with array_keys

like

$result = array_diff($fields,array_keys($keys_array));

      



Note: not tested

+2


source


I have seen other answers and as far as I know they are correct. These functions can help you.

But I couldn't figure out why you would create your array like this:

$fields = array('id' , 'site', 'placement', 'device', 'source', 'campaign', 'url', 'country', 'dof_count', 'dof_idx', 'active');

      

If your goal was to just check for another array, then just associatively create it:



<?php
        $fields = array(
            'id' => null,
            'site' => null,
            'placement' => null,
            /*...*/
            'active' => null
        );

      

But nevertheless, I don't understand that you need to check the structure of the array, given that it should always be the same. If you have multiple input types for an array, then you must create a field of type type on all the arrays you are going to return, and "if" them from there. Example:

<?php
/*This array has a type and only two indexes of data.*/
$inputArray = array(
    'type' => 'firstType',
    'data1' => 'data',
    'data2' => 'data'
);

/*This array also has a type but 6 indexes containing datas*/
$anotherInputArray = array(
    'type' => 'secondType',
    'data3' => 'data',
    'data4' => 'data',
    'data4' => 'data',
    'data4' => 'data',
    'data4' => 'data',
    'data4' => 'data'
);

treatArray($inputArray);
treatArray($anotherInputArray);

function treatArray($array){
    if($array['type']=='firstType'){
        /*Treat it in one way*/
    }elseif($array['type']=='secondType'){
        /*Or the other way*/
    }
}

      

Hope I can help, but you didn't describe the context you are working with, so I did my best to guess the lasso (although not recommended).

+1


source


Just array_flip

your array $field

:

var_dump(array_diff_key(array_flip($fields), $array2));

      

0


source







All Articles