127

Without foreach, how can I turn an array like this

array("item1"=>"object1", "item2"=>"object2",......."item-n"=>"object-n");

to a string like this

item1='object1', item2='object2',.... item-n='object-n'

I thought about implode() already, but it doesn't implode the key with it.

If foreach it necessary, is it possible to not nest the foreach?

EDIT: I've changed the string


EDIT2/UPDATE: This question was asked quite a while ago. At that time, I wanted to write everything in one line so I would use ternary operators and nest built in function calls in favor of foreach. That was not a good practice! Write code that is readable, whether it is concise or not doesn't matter that much.

In this case: putting the foreach in a function will be much more readable and modular than writing a one-liner(Even though all the answers are great!).

tom91136
  • 8,202
  • 12
  • 55
  • 74
  • How nested foreach would be necesarry? – Shubham Jul 11 '12 at 07:14
  • What are you attempting? Why do you have those constraints? – Madara's Ghost Jul 11 '12 at 07:27
  • this was my database class for web app i'm building, i don't want it to look messy since it's already populated with a bunch of foreach and for-loop all together – tom91136 Jul 11 '12 at 07:30
  • 1
    This selected answer is method that you asked for, however it should be noted that it is critically slower and that if you are storing this information via a database it would be vastly superior to both a loop and this to just use json_encode. Exhibit A: http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/ – Case Sep 07 '13 at 05:16
  • 1
    possible duplicate of [Fastest way to implode an associative array with keys](http://stackoverflow.com/questions/408032/fastest-way-to-implode-an-associative-array-with-keys) – Félix Adriyel Gagnon-Grenier Jul 07 '15 at 18:42

13 Answers13

231

You could use http_build_query, like this:

<?php
  $a=array("item1"=>"object1", "item2"=>"object2");
  echo http_build_query($a,'',', ');
?>

Output:

item1=object1, item2=object2 

Demo

stewe
  • 40,424
  • 13
  • 77
  • 74
206

and another way:

$input = array(
    'item1'  => 'object1',
    'item2'  => 'object2',
    'item-n' => 'object-n'
);

$output = implode(', ', array_map(
    function ($v, $k) {
        if(is_array($v)){
            return $k.'[]='.implode('&'.$k.'[]=', $v);
        }else{
            return $k.'='.$v;
        }
    }, 
    $input, 
    array_keys($input)
));

or:

$output = implode(', ', array_map(
    function ($v, $k) { return sprintf("%s='%s'", $k, $v); },
    $input,
    array_keys($input)
));
SebHallin
  • 873
  • 5
  • 11
Yoshi
  • 53,111
  • 13
  • 85
  • 102
  • 7
    This method is what the Author was asking for, but it should be noted that it is critically slower and that if you are storing this information via a database it would be vastly superior to both a loop and this to just use json_encode. Exhibit A: http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/ – Case Sep 07 '13 at 05:14
  • None of the links here seem to work anymore, they all `echo "Hello, World!";` Should I be seeing code examples of those functions? – Félix Adriyel Gagnon-Grenier Jul 07 '15 at 18:36
  • This method is very easy to customize according to own demand. – Kabir Hossain Nov 24 '16 at 04:01
  • I was using this method to build select options and populate the selected option, and since both arrays must be the same size you can do something like this for the second array. `array_fill(0, $input, 'selected-value-you want-to-check-against');` This will generate same size array with single value for all the rows. – krasenslavov Dec 18 '19 at 00:08
49

I spent measurements (100000 iterations), what fastest way to glue an associative array?

Objective: To obtain a line of 1,000 items, in this format: "key:value,key2:value2"

We have array (for example):

$array = [
    'test0' => 344,
    'test1' => 235,
    'test2' => 876,
    ...
];

Test number one:

Use http_build_query and str_replace:

str_replace('=', ':', http_build_query($array, null, ','));

Average time to implode 1000 elements: 0.00012930955084904

Test number two:

Use array_map and implode:

implode(',', array_map(
        function ($v, $k) {
            return $k.':'.$v;
        },
        $array,
        array_keys($array)
    ));

Average time to implode 1000 elements: 0.0004890081976675

Test number three:

Use array_walk and implode:

array_walk($array,
        function (&$v, $k) {
            $v = $k.':'.$v;
        }
    );
implode(',', $array);

Average time to implode 1000 elements: 0.0003874126245348

Test number four:

Use foreach:

    $str = '';
    foreach($array as $key=>$item) {
        $str .= $key.':'.$item.',';
    }
    rtrim($str, ',');

Average time to implode 1000 elements: 0.00026632803902445

I can conclude that the best way to glue the array - use http_build_query and str_replace

Maxim Tkach
  • 1,557
  • 11
  • 23
9

I would use serialize() or json_encode().

While it won't give your the exact result string you want, it would be much easier to encode/store/retrieve/decode later on.

NHG
  • 5,751
  • 6
  • 33
  • 45
Madara's Ghost
  • 165,920
  • 50
  • 255
  • 304
3

Using array_walk

$a = array("item1"=>"object1", "item2"=>"object2","item-n"=>"object-n");
$r=array();
array_walk($a, create_function('$b, $c', 'global $r; $r[]="$c=$b";'));
echo implode(', ', $r);

IDEONE

Shiplu Mokaddim
  • 54,465
  • 14
  • 131
  • 183
3

You could use PHP's array_reduce as well,

$a = ['Name' => 'Last Name'];

function acc($acc,$k)use($a){ return $acc .= $k.":".$a[$k].",";}

$imploded = array_reduce(array_keys($a), "acc");
sapenov
  • 1,401
  • 1
  • 10
  • 7
3

There is also var_export and print_r more commonly known for printing debug output but both functions can take an optional argument to return a string instead.

Using the example from the question as data.

$array = ["item1"=>"object1", "item2"=>"object2","item-n"=>"object-n"];

Using print_r to turn the array into a string

This will output a human readable representation of the variable.

$string = print_r($array, true);
echo $string;

Will output:

Array
(
    [item1] => object1
    [item2] => object2
    [item-n] => object-n
)

Using var_export to turn the array into a string

Which will output a php string representation of the variable.

$string = var_export($array, true);
echo $string;

Will output:

array (
  'item1' => 'object1',
  'item2' => 'object2',
  'item-n' => 'object-n',
)

Because it is valid php we can evaluate it.

eval('$array2 = ' . var_export($array, true) . ';');
var_dump($array2 === $array);

Outputs:

bool(true)
nickl-
  • 7,659
  • 4
  • 38
  • 48
2

Change

-    return substr($result, (-1 * strlen($glue)));
+    return substr($result, 0, -1 * strlen($glue));

if you want to resive the entire String without the last $glue

function key_implode(&$array, $glue) {
    $result = "";
    foreach ($array as $key => $value) {
        $result .= $key . "=" . $value . $glue;
    }
    return substr($result, (-1 * strlen($glue)));
}

And the usage:

$str = key_implode($yourArray, ",");
Community
  • 1
  • 1
Björn
  • 28,401
  • 9
  • 64
  • 81
2

For debugging purposes. Recursive write an array of nested arrays to a string. Used foreach. Function stores National Language characters.

function q($input)
{
    $glue = ', ';
    $function = function ($v, $k) use (&$function, $glue) {
        if (is_array($v)) {
            $arr = [];
            foreach ($v as $key => $value) {
                $arr[] = $function($value, $key);
            }
            $result = "{" . implode($glue, $arr) . "}";
        } else {
            $result = sprintf("%s=\"%s\"", $k, var_export($v, true));
        }
        return $result;
    };
    return implode($glue, array_map($function, $input, array_keys($input))) . "\n";
}
Sergey Yurich
  • 31
  • 1
  • 4
2

Here is a simple example, using class:

$input = array(
    'element1'  => 'value1',
    'element2'  => 'value2',
    'element3' =>  'value3'
);

echo FlatData::flatArray($input,', ', '=');

class FlatData
{

    public static function flatArray(array $input = array(), $separator_elements = ', ', $separator = ': ')
    {
        $output = implode($separator_elements, array_map(
            function ($v, $k, $s) {
                return sprintf("%s{$s}%s", $k, $v);
            },
            $input,
            array_keys($input),
            array_fill(0, count($input), $separator)
        ));
      return $output;
    }

}
Ivan Ferrer
  • 479
  • 5
  • 11
1

For create mysql where conditions from array

$sWheres = array('item1'  => 'object1',
                 'item2'  => 'object2',
                 'item3'  => 1,
                 'item4'  => array(4,5),
                 'item5'  => array('object3','object4'));
$sWhere = '';
if(!empty($sWheres)){
    $sWhereConditions = array();
    foreach ($sWheres as $key => $value){
        if(!empty($value)){
            if(is_array($value)){
                $value = array_filter($value); // For remove blank values from array
                if(!empty($value)){
                    array_walk($value, function(&$item){ $item = sprintf("'%s'", $item); }); // For make value string type 'string'
                    $sWhereConditions[] = sprintf("%s in (%s)", $key, implode(', ', $value));
                }
            }else{
                $sWhereConditions[] = sprintf("%s='%s'", $key, $value);
            }
        }
    }
    if(!empty($sWhereConditions)){
        $sWhere .= "(".implode(' AND ', $sWhereConditions).")";
    }
}
echo $sWhere;  // (item1='object1' AND item2='object2' AND item3='1' AND item4 in ('4', '5') AND item5 in ('object3', 'object4'))
Ajay Patidar
  • 184
  • 2
  • 9
0

Using explode to get an array from any string is always OK, because array is an always in standard structure.

But about array to string, is there any reason to use predefined string in codes? while the string SHOULD be in any format to use!

The good point of foreach is that you can create the string AS YOU NEED IT! I'd suggest still using foreach quiet readable and clean.

$list = array('a'=>'1', 'b'=>'2', 'c'=>'3');

$sql_val = array();
foreach ($list as $key => $value) {
    $sql_val[] = "(" . $key . ", '" . $value . "') ";
}
$sql_val = implode(', ', $sql_val);

with results:

(a, '1') , (b, '2') , (c, '3') 

|

(a: '1') , (b: '2') , (c: '3') 

|

a:'1' , b:'2' , c:'3' 

etc.

Saghachi
  • 703
  • 9
  • 15
0

Short one:

$string = implode('; ', array_map(fn($k, $v) => "$k=$v", array_keys($array), $array));
Andy Webov
  • 481
  • 1
  • 5
  • 8