简体   繁体   中英

how to echo same key value first after other key value from multidimensional array in php

I have get data from array and I have get same key value data first than other key value in multidimensional array.

My array result like this

Array
(
    [0] => Array
        (
            [id] => 113
            [timeIn] => 10:40:11
            [timeOut] => 10:53:13
            [hour] => 00:13:00
            [emp_id] => 42

)


    [1] => Array
        (
            [id] => 114
            [timeIn] => 15:18:57
            [timeOut] => 15:00:00
            [hour] => 04:19:00
            [emp_id] => 42

   )
)  ......

I have first get timeIn and timeOut record from all than echo other key.

How to do from any method or loop in php?

I expected result like this

10:40:11 10:53:13
15:18:57 15:00:00
42
00:13:00 04:19:00

You can try this .

Code

$array = [
    [
        [
            "id" => 113,
            "timeIn" => "10:40:11",
            "timeOut" => "10:53:13",
            "hour" => "00:13:00",
            "emp_id" => 42
        ],
        [
            "id" => 114,
            "timeIn" => "15:18:57",
            "timeOut" => "15:00:00",
            "hour" => "04:19:00",
            "emp_id" => 42
        ]
    ],
    [
        [
            "id" => 113,
            "timeIn" => "10:40:11",
            "timeOut" => "10:53:13",
            "hour" => "00:13:00",
            "emp_id" => 43
        ],
        [
            "id" => 114,
            "timeIn" => "15:18:57",
            "timeOut" => "15:00:00",
            "hour" => "04:19:00",
            "emp_id" => 43
        ]
    ]
];

foreach($array as $val){

    foreach($val as $time){
        echo $time['timeIn'] . ' ' . $time['timeOut'] . "\n";
    }
    echo $time['emp_id'] ."\n";
    echo $val[0]['hour'] . ' ' . $val[1]['hour'] . "\n\n";
}

Result

10:40:11 10:53:13
15:18:57 15:00:00
42
00:13:00 04:19:00

10:40:11 10:53:13
15:18:57 15:00:00
43
00:13:00 04:19:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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