简体   繁体   English

如何通过键从对象属性的多维数组中获取值作为PHP中的数字

[英]How to get value by key from multidimensional array from an object attribute as number in PHP

How to get value by key from the multidimensional array in PHP如何从PHP中的多维数组中按键获取值

We Get TradeID in foreach loop我们在 foreach 循环中获取 TradeID

We get TradeID by Loop one by one我们通过Loop一一得到TradeID

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
                    )
            )
    )

You need to use foreach twice.您需要使用 foreach 两次。

PHP Code: PHP代码:

/* 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请检查输出: https : //3v4l.org/d3KbN

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:考虑到$object变量将是您看到的对象,并且只有第一层是一个对象,您可以使用符号(array)转换为数组并继续您可能已经知道的逻辑:

$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抱歉之前的答案,此答案已在此处修复和测试: https : //3v4l.org/LOYtp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM