0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I dont know why but it returns me ("Invalid argument supplied for foreach")
but as per your your array format, I'm using the same format then please verify format of your array and let me know @RavshanAbdurasulov
when I am debugging that comes for me which I putted in question however I do not know why this problem showing
@RavshanAbdurasulov Now I've replaced foreach loop in answer, try now
yes there only two code first my decoding and your cycle
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.