0x0
0x0

Reputation: 373

Accessing Element from a Multi-dimensional Array with php

I have an array of data in the format below and I find it hard accessing the elements especially the "data" array with php.

Array ( 
[jsonrpc] => 2.0 
[result] => Array ( 
    [random] => Array ( 
        [data] => Array ( 
            [0] => 15 
            [1] => 36 
            [2] => 11 
            [3] => 2 
            [4] => 31 
            [5] => 16 
            [6] => 19 
            [7] => 50 
            [8] => 24 
            [9] => 33 
            [10] => 3 
            [11] => 46 
            [12] => 37 
            [13] => 7 
            [14] => 29 
            [15] => 13 
            [16] => 52 
            [17] => 14 
            [18] => 49 
            [19] => 42 
            [20] => 40 
            [21] => 25 
            [22] => 39 
            [23] => 21 
            [24] => 28 
            [25] => 27 
            [26] => 34 
            [27] => 8 
            [28] => 10 
            [29] => 41 
            [30] => 45 
            [31] => 47 
            [32] => 26 
            [33] => 20 
            [34] => 12 
            [35] => 9 
            [36] => 6 
            [37] => 32 
            [38] => 18 
            [39] => 17 
            [40] => 38 
            [41] => 23 
            [42] => 5 
            [43] => 51 
            [44] => 35 
            [45] => 43 
            [46] => 44 
            [47] => 30 
            [48] => 48 
            [49] => 4 
            [50] => 1 
            [51] => 22 
                        ) 
        [completionTime] => 2016-01-22 11:11:26Z 
                    ) 
        [bitsUsed] => 296 
        [bitsLeft] => 933024 
        [requestsLeft] => 197725 
        [advisoryDelay] => 70 
                ) 
        [id] => 16083686 )

I have tried

echo $myArray['data'];

but I got error Undefined index: data

I have also tried the foreach function

foreach ($myArray as $row){
 echo $row['result']['random']['data'];
}

but I get error Illegal string offset 'result'

I've had my time with this and would be very glad to get help with accessing the elements in this array.

Upvotes: 0

Views: 48

Answers (2)

sandyclone
sandyclone

Reputation: 149

your elements are stored in something like this, try using variable for index and loop it, to get all the values.

echo $row['result']['random']['data'][0];
echo $row['result']['random']['data'][1];
echo $row['result']['random']['data'][2];

Upvotes: 0

Jeremy
Jeremy

Reputation: 279

You must specify the full path to access an item

echo $row['result']['random']['data'][0];

$row['result']['random']['data'] is an array, and the function echo can't display an array. To display an array use print_r() or var_dump().

var_dump($row['result']['random']['data']);
print_r($row['result']['random']['data']);

If you want to access all data items you can use :

foreach($row['result']['random']['data'] as $k => $v) {
    echo $k . " = " . $v . "<br />";
}

Upvotes: 1

Related Questions