Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten an array to a string of the values?

Tags:

arrays

php

I have an array that looks like this.

'keyvals' => 
    array
      'key1' => 'value1'
      'key2' => 'value2'
      'key3' => 'value3'

Is there a cool way to flatten it to a string like 'value1 value2 value3'? I also have access to PHP 5.3 if there's something new there.

like image 448
sameold Avatar asked Oct 29 '11 20:10

sameold


2 Answers

$someArray = array(
  'key1' => 'value1',
  'key2' => 'value2',
  'key3' => 'value3'
);
implode(' ', $someArray); // => "value1 value2 value3"
like image 74
James Avatar answered Nov 03 '22 15:11

James


See implode:

$flat = implode(' ', $array['keyvals']);
like image 24
hakre Avatar answered Nov 03 '22 13:11

hakre