Joining - Merge Multidimentional Array with a key value as identifier in PHP -
[{ "title" = > "ceo", "name" = > "george", "columns" = > [{ "display_name" = > "salary", "value" = > "3.85", }, { "display_name" = > "bonus", "value" = > "994.19", }, { "display_name" = > "increment", "value" = > "8.15", }] }] data2 = [{ "title" = > "ceo", "name" = > "george", "columns" = > [{ "display_name" = > "address", "value" = > "albany", }, { "display_name" = > "phone", "value" = > "47123", }, { "display_name" = > "mobile", "value" = > "784123", }] }]
i have above 2 arrays, want join values inside columns
1 array, if conditions met name = george
in both hashes.
the required output this:
[{ "title" = > "ceo", "name" = > "george", "columns" = > [{ "display_name" = > "salary", "value" = > "3.85", }, { "display_name" = > "bonus", "value" = > "994.19", }, { "display_name" = > "increment", "value" = > "8.15", }, { "display_name" = > "address", "value" = > "albany", }, { "display_name" = > "phone", "value" = > "47123", }, { "display_name" = > "mobile", "value" = > "784123", }] }]
"name"=>"george"
- identifier? or matcher? id name in both same dump value of columns joint array hash.
made function you:
<?php function array_merge_on_key($original, $new, $key){ if(is_array($original) && is_array($new)){ $copy_from = array(); if(array_key_exists($key, $new)){ if(is_array($new[$key])){ $copy_from = $new[$key]; } } return is_null($copy_from) ? array_merge($original[$key], $copy_from) : $original; } return false; }
Comments
Post a Comment