Jump to content

Add key/value pair to multidim array


quasiman
Go to solution Solved by quasiman,

Recommended Posts

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.  :(

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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);
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.