Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imploding specific associative array key values [duplicate]

I've got associative array with results from database containing data like in following structure:

$arr[0] = Array("id"=>4, "otherdata"=>"something");
$arr[1] = Array("id"=>6, "otherdata"=>"something else");
$arr[2] = Array("id"=>15, "otherdata"=>"something totally different");

I would like to implode data that is only in id key for each $arr entry, so that final imploded string is 4,6,15 (gluded with ,).

Right now I've got some solutions:

  1. Doing it in pure PHP within Smarty.
  2. Creating function that will implode result from array_map which creates new table with id's only.
  3. Assigning variable within Smarty template and creating implode-like result string with foreach.

but neither of them I am happy of.

Is there any other simple way to achieve desired result?

like image 345
DevilaN Avatar asked May 23 '26 10:05

DevilaN


1 Answers

The 4th solution:

echo implode(',', array_column($arr, 'id'));
like image 135
u_mulder Avatar answered May 24 '26 23:05

u_mulder