user752746
user752746

Reputation: 617

PHP equivalent ColdFusion ValueList()

I'm learning PHP and was wondering if there some like ValueList() for PHP? Thank you in advance.

$data = Array
(
[0] => Array
    (
        [id] => 851099
        [title] => Iron Maiden
    )

[1] => Array
    (
        [id] => 852099
        [orgName] => Judas Priest
    )

[2] => Array
    (
        [id] => 861099
        [orgName] => Black Sabbath
    )

)

$valueListTitle = ValueList($data.title)
echo $valueListTitle;

which return this: "Iron Maiden, Judas Priest, Black Sabbath"

Upvotes: 0

Views: 214

Answers (1)

nico
nico

Reputation: 2121

Yes, check out array_column and implode to add commas.

Sample usage:

$data = array(
  ["id" =>1, "title"=>"Iron Maiden"], 
  ["id" =>2, "title"=>"Judas Priest"],
  ["id" =>3, "title"=>"Black Sabbath"],
  ["id" =>4, "title"=>"Deep Purple"],
  ["id" =>5, "title"=>"Rolling Stones"]
);

$valueListTitle = array_column($data, 'title');
$commaSeperated = implode(", ", $valueListTitle);
echo $commaSeperated;

which return this: "Iron Maiden, Judas Priest, Black Sabbath, Deep Purple, Rolling Stones"

Upvotes: 3

Related Questions