Ravshan Abdurasulov Ravshan Abdurasulov - 6 months ago 18
JSON Question

Multidimensional array for data table

How to Select key values by index

I want to create datatable but I do not know how to select exact array[1] with key values. Now my datatable cycle returns nothing

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
)


I somehow started in PHP and decoded the Json into array

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

Answer

This code will work for you, Use PHP LIST to print key to value of an array ...

<?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>';
    }
}   
?>

This will give you :

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

LIVE EXAMPLE : CLICK HERE