Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Add key/value pair to multidim array

$
0
0

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

 


Viewing all articles
Browse latest Browse all 13200

Trending Articles