0

I have a two dimensional array like this:

Array
(
    [0] => Array
        (
            [Product_Main_category] =>  Value 
            [Product_Sub_category1] =>  Value 
            [Product_Sub_category2] =>  Value 
            [Product_Title] =>  Value 
)
 [1] => Array
        (
            [Product_Main_category] =>  Value 
            [Product_Sub_category1] =>  Value 
            [Product_Sub_category2] =>  Value 
            [Product_Title] =>  Value 
            [Product_SKU] =>  Value 
            [Product_MODEL] =>  Value 
            [Product_manf_link] => Value 
            [Product_manf_Image_link] => Value 
)
 [2] => Array
        (
            [Product_Sub_category1] => Value 
            [Product_Title] =>  Value 
            [Product_SKU] =>  Value 
            [Product_MODEL] =>  Value 
            [Product_manf_link] => Value 
            [Product_manf_Image_link] => Value 
)

I want to make the child array keys same for all like if one child array have extra keys then all other child arrays must have those keys but can be empty.

like:

Array
(
    [0] => Array
        (
            [Product_Main_category] =>  
            [Product_Sub_category1] =>  
            [Product_Sub_category2] =>  
            [Product_Title] =>  Value 
            [Product_SKU] =>  Value 
            [Product_MODEL] =>  Value 
            [Product_manf_link] => Value 
            [Product_manf_Image_link] => 
)
 [1] => Array
        (
            [Product_Main_category] =>  Value 
            [Product_Sub_category1] =>  Value 
            [Product_Sub_category2] =>  Value 
            [Product_Title] =>  Value 
            [Product_SKU] =>  Value 
            [Product_MODEL] =>  Value 
            [Product_manf_link] => Value 
            [Product_manf_Image_link] => Value 
)
 [2] => Array
        (
           [Product_Main_category] =>  Value 
           [Product_Sub_category1] =>  Value 
           [Product_Sub_category2] =>  Value 
           [Product_Title] =>  
           [Product_SKU] =>  
           [Product_MODEL] =>  
           [Product_manf_link] => Value 
           [Product_manf_Image_link] => Value 
)

The child arrays are being made dynamically I am setting the in the main array like this

$array[] = $sub; 

Maybe after completing the main array which is $array we can do that?

Thank you.

Please let me know if this is a possible duplicate so I can close it myself not by down votting.

mubashermubi
  • 8,846
  • 4
  • 16
  • 30

1 Answers1

1

First you need to accumulate all the keys somehow. I think it would be better to do this while you build the array, but if it needs to be done afterward, you could use something like this.

$all_keys = array_reduce($old_array, function($keys, $item){
  return array_merge($keys, array_keys($item));
}, array());

Then make a template with empty values for the unique keys

$template = array_fill_keys(array_unique($all_keys), '');

You can apply the template to your old array to generate one where each child array contains all the same keys.

$new_array = array_map(function ($item) use ($template) {
  return array_merge($template, $item);
}, $old_array);

If you would rather modify the existing array instead of creating a new one, it can be done this way

array_walk($old_array, function(&$child, $key, $template){
  $child = array_merge($template, $child);
}, $template);
Don't Panic
  • 41,125
  • 10
  • 61
  • 80