1

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"
]
1
  • 1
    i am astonished that when you push how "blue" becomes index? $data["blue"]=1; needed instead of push Commented Sep 20, 2016 at 11:38

4 Answers 4

4
foreach ($labors as $result) {
        $item = (array)$result;  
        $item['blue'] = '1';
        $data[] = $item;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2

Did you try,

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

1 Comment

But in the question you put that the blue should be string "1" so how can this work fine?
0

This may help you:

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

1 Comment

It will fail if result is an object, i guess that's why there is typecasting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.