quasiman Posted March 2, 2013 Share Posted March 2, 2013 Hi all, I feel like this is a dumb question, but I can't seem to get around it. I'm working with several arrays and pulling them together basically like this: $services[] = getData('....', $params); $services[] = getData('....', $params); $services[] = getData('....', $params); $services[] = getData('....', $params); $merge = call_user_func_array('array_merge', $services); Now, the getData function is pulling through info from an API and I want to add a little reference to each array to show where it came from. The array I'm getting back looks like this: Array ( [0] => Array ( [entity.entityid] => 44705207 [orders.endtime] => 1372226238 [orders.resellerlock] => false [orders.timestamp] => 2012-06-26 05:57:20.292531+00 [orders.creationdt] => 1340690238 ) [1] => Array ( [entity.entityid] => 44705207 [orders.endtime] => 1372226238 [orders.resellerlock] => false [orders.timestamp] => 2012-06-26 05:57:20.292531+00 [orders.creationdt] => 1340690238 ) ) I tried this:$services[][]['key_name'] = 'key_value';. But it just adds a new array set : Array ( [0] => Array ( [entity.entityid] => 44705207 [orders.endtime] => 1372226238 [orders.resellerlock] => false [orders.timestamp] => 2012-06-26 05:57:20.292531+00 [orders.creationdt] => 1340690238 ) [1] => Array ( [entity.entityid] => 44705207 [orders.endtime] => 1372226238 [orders.resellerlock] => false [orders.timestamp] => 2012-06-26 05:57:20.292531+00 [orders.creationdt] => 1340690238 ) [2] => Array ( [key_name] => key_value ) ) So how do I add a key / value to the arrays that are already there? $services[] = getData('....', $params); foreach ($services as $service) { if (is_array($service)) { foreach ($service as $key) { $key['key_name'] = "key_value"; } } } This didn't even show up in the results.... I'm lost. Quote Link to comment https://forums.phpfreaks.com/topic/275103-add-keyvalue-pair-to-multidim-array/ Share on other sites More sharing options...
requinix Posted March 2, 2013 Share Posted March 2, 2013 Using [][] will just tell PHP to create a new value inside a new array inside the variable. Put the key_name inside the source array before you merge it into $services. At least I think that's what you want. As for why your last bit of code doesn't work, foreach will create copies of values. $key is a copy of something in $service. You can go ahead and add whatever you want to it but you'll only be modifying the copy. It's possible to make it not copy but that's not the best solution (if it would work anyways, I didn't think it through) in this case. Quote Link to comment https://forums.phpfreaks.com/topic/275103-add-keyvalue-pair-to-multidim-array/#findComment-1415930 Share on other sites More sharing options...
Solution quasiman Posted March 4, 2013 Author Solution Share Posted March 4, 2013 I figured it out..... "In order to be able to directly modify array elements within the loop precede $value with &": http://php.net/manual/en/control-structures.foreach.php So all I had to do was: $services[] = getData('domains/search', $params); foreach ($services as &$service) { foreach ($service as &$s) { if (is_array($s)) { $s['key_name'] = "key value"; } } } print_r($services); Quote Link to comment https://forums.phpfreaks.com/topic/275103-add-keyvalue-pair-to-multidim-array/#findComment-1416393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.