MSA MSA -4 years ago 117
PHP Question

Multidimentional array to array string

Find Below my array result

Array
(
[0] => Array
(
[0] => stdClass Object
(
[task_id] => 1
)

)

[1] => Array
(
[0] => stdClass Object
(
[task_id] => 28
)

[1] => stdClass Object
(
[task_id] => 43
)

[2] => stdClass Object
(
[task_id] => 44
)

)
)


I would like to store task_id as a string as below

Array(
[0] => 1
[1] => 28,43, 44
)


I tried with implode it does not work can anyone plz help me out in this?

Answer Source

You can do that:

foreach ($arr as &$item) {
    $item = implode(',', array_map(function ($i) { return $i->task_id; }, $item));
}

Each item of your array is an array of stdClass instances with the property task_id. With array_map I change it to an array of integers and then I only have to use implode and to affect the result to the current item.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download