Murali Murali - 2 months ago 5
PHP Question

Push element in array in last php

I want to push this value(blue) in array at last position right now this value is coming outside the array See below output

$data = array();
foreach ($labors as $result) {
$data[] = (array)$result;
array_push($data,"blue");

}


Output

0 => array:9 [▼
"Date" => "2016-09-04"
"Emp" => "ADDISA01"
"Job" => "24-1604"
"Extra" => null
"Cost" => "26-01-10"
"Union" => null
"Cert" => ""
"Shift" => "1"
"EPay" => "1"
]
"blue" => "1"


expected output

0 => array:9 [▼
"Date" => "2016-09-04"
"Emp" => "ADDISA01"
"Job" => "24-1604"
"Extra" => null
"Cost" => "26-01-10"
"Union" => null
"Cert" => ""
"Shift" => "1"
"EPay" => "1"
"blue" => "1"
]

Answer

Did you try,

$data = array();
foreach ($labors as $result) {
    $temp = (array)$result;  
    $temp['blue'] = 1;
    $data[] = $temp;
}
Comments