-1

How to get value by key from the multidimensional array in PHP

We Get TradeID in foreach loop

We get TradeID by Loop one by one

stdClass Object
    (
        [0] => 
        [1] => 1
        [2] => Array
            (
                [0] => Array
                    (
                        [TradeID] => 15950315
                        [Price] => 0.00000170
                        [Type] => buy
                        [Amount] => 712.85989430
                        [Total] => 0.00121368
                        [Time] => 1535337908
                    )

                [1] => Array
                    (
                        [TradeID] => 15908375
                        [Price] => 0.00000300
                        [Type] => buy
                        [Amount] => 574.71264368
                        [Total] => 0.00172673
                        [Time] => 1535022882
                    )
            )
    )
Gaurav
  • 19
  • 1
  • 6
  • 2
    Possible duplicate of [How to get an array of specific "key" in multidimensional array without looping](https://stackoverflow.com/questions/7994497/how-to-get-an-array-of-specific-key-in-multidimensional-array-without-looping) – Habib Aug 28 '18 at 07:19
  • array_column('TradeID') return array of TradeID – TsV Aug 28 '18 at 07:19
  • @HabibQadoos, I think it might be a duplicate, but not of that one. This one is an object probably result of a **json_decode()**, it's not an array initially and, therefore, the way to access the array is slightly different. – Rafael Aug 28 '18 at 07:25

2 Answers2

1

You need to use foreach twice.

PHP Code:

/* Generating structure */
$rawdata = array(
        '',
        1,
        array(
            array(
                'TradeID' => 15950315,
                'Price' => 0.00000170,
                'Type' => 'buy',
                'Amount' => 712.85989430,
                'Total' => 0.00121368,
                'Time' => 1535337908,
            ),
            array(
                'TradeID' => 15908375,
                'Price' => 0.00000300,
                'Type' => 'buy',
                'Amount' => 574.71264368,
                'Total' => 0.00172673,
                'Time' => 1535022882,
            )
        )
    );
$data = (object)$rawdata;
print_r($data);// same output as shown in the question


/** Getting TradeID */

foreach ($data as $key => $value) {
    if (is_array($value)) {
        foreach ($value as $tradeKey => $tradevalue) {
            echo $tradevalue['TradeID'].'<br/>';
        }
    }
}

Please check output at: https://3v4l.org/d3KbN

Ketan Yekale
  • 2,108
  • 3
  • 26
  • 33
-1

Considering the $object variable would be the object you see, and only the first layer is an object, you can cast to an array using the notation (array) and continue the logic that you probably know already:

$array = (array) $object;

foreach ($array['2'] as $item) {
    echo $item['TradeID'];
}

Sorry for the previous answer, this one is fixed and tested here: https://3v4l.org/LOYtp

Rafael
  • 1,495
  • 1
  • 14
  • 25