数据表的多维数组

时间:2016-06-03 11:19:55

标签: php json

如何按索引选择键值

我想创建数据表,但我不知道如何用键值选择精确数组[1]。现在我的数据表循环没有返回任何内容

Array
(
[0] => Array
    (
        [0] => Array
            (
                [success] => 1
                [error] => 0
                [error_note] => 
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [type_pay] => 0
                [oper_type] => 4
                [name_pay] => CLICK
                [name_oper] => Основной долг
                [time_pay] => 2016-05-01 00:00:00
                [amount] => 1461128
            )

        [1] => Array
            (
                [type_pay] => 1
                [oper_type] => 3
                [name_pay] => Наличные
                [name_oper] => Депозит
                [time_pay] => 2016-05-01 00:00:00
                [amount] => 207866
            )

我不知怎的从PHP开始并将Json解码为数组

$data["reports"] = json_decode(file_get_contents('URL'), true);

1 个答案:

答案 0 :(得分:1)

此代码适用于您,使用PHP LIST打印数组值的键...

<?php 
$data['reports'] = array(
                0 => array
                    (
                        0 => array
                            (
                                'success' => 1,
                                'error' => 0,
                                'error_note' => ''
                            )

                    ),
                1 => array
                    (
                        0 => array
                            (
                                'type_pay' => 0,
                                'oper_type' => 4,
                                'name_pay' => 'MNP',
                                'name_oper' => 'OPQ',
                                'time_pay'=> '2016-05-01 00:00:00',
                                'amount' => 1461128
                            ),
                         1 => array
                            (
                                'type_pay' => 1,
                                'oper_type' => 3,
                                'name_pay' => 'XYZ',
                                'name_oper' => 'ABC',
                                'time_pay' => '2016-05-01 00:00:00',
                                'amount' => 207866
                            )
                        )
                    );  

foreach($data['reports'][1] as $key => $val)
{
    while(list($k, $v) = each($val)){
        echo $k.' : '.$v.'<br>';
    }
}   
?>

这会给你:

type_pay : 0
oper_type : 4
name_pay : MNP
name_oper : OPQ
time_pay : 2016-05-01 00:00:00
amount : 1461128
type_pay : 1
oper_type : 3
name_pay : XYZ
name_oper : ABC
time_pay : 2016-05-01 00:00:00
amount : 207866

现场示例:CLICK HERE