Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

My solution uses nested foreach which is not what I wanted

**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 trinary operators and nest built in function calls in favor of foreach. That was not a good practice! Write code that is readable, whether it's concise 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 :)).

share|improve this question
    
How nested foreach would be necesarry? –  Shubham Jul 11 '12 at 7:14
    
What are you attempting? Why do you have those constraints? –  Second Rikudo Jul 11 '12 at 7: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 7:30
    
@downvoter, why downvote? –  Tom91136 Jul 11 '12 at 7:38
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: willem.stuursma.name/2010/11/22/… –  Iscariot Sep 7 '13 at 5:16

5 Answers 5

up vote 51 down vote accepted

and another way:

$input = array('item1' => 'object1', 'item2' => 'object2', 'item-n' => 'object-n');
$output = implode(', ', array_map(function ($v, $k) { return $k . '=' . $v; }, $input, array_keys($input)));

http://codepad.viper-7.com/QDkjeh

or:

$output = implode(', ', array_map(function ($v, $k) { return sprintf("%s='%s'", $k, $v); }, $input, array_keys($input)));

http://codepad.viper-7.com/DUPJ0y

share|improve this answer
3  
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: willem.stuursma.name/2010/11/22/… –  Iscariot Sep 7 '13 at 5:14

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

share|improve this answer
    
I like the array_walk approach, but I don't understand why people suggest using create_function instead of just using an anonymous function? –  Rikki May 4 '14 at 18:27
    
@Rikki Compatibility! –  shiplu.mokadd.im May 5 '14 at 4:36
    
Ah! I didn't realise it was a PHP4 thing! –  Rikki May 13 '14 at 0:08
    
@Rikki No. Its pre PHP 5.3 thing. Anonymous function first appeared on PHP 5.3 –  shiplu.mokadd.im May 13 '14 at 11:10
    
But it was introduced in PHP 4.0.1, so is a PHP4 thing. Semantics... Adding a feature as significant as anonymous functions in a minor version update is bizarre. –  Rikki May 14 '14 at 14:58

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

share|improve this answer
    
is it possible to make the object1 into 'object1' –  Tom91136 Jul 11 '12 at 7:23
2  
Never used the other parameters of http_build_query. –  shiplu.mokadd.im Jul 11 '12 at 7:38
3  
+1 Hell yeah I love finding little nuggets of goodness like this :) –  Steve Oct 3 '13 at 2:28
2  
Beware of the string encoding!If you are not building an URL maybe you do not want it on your array key&value –  Matteo Sep 10 '14 at 12:51
1  
@Matteo You can wrap http_build_query in urldecode to avoid it. –  Afterlame Nov 19 '14 at 11:24
function key_implode(&$array, $glue) {
    $result = "";

    foreach ($array as $key => $value) {
        $result .= $key . "=" . $value . $glue;
    }

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

Usage:

$str = key_implode($yourArray, ",");
share|improve this answer
    
without foreach - please re-read the OP's question. –  a coder Jun 26 '14 at 13:32

I would use serialise() 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.

share|improve this answer
    
i need the string to look exactly like that –  Tom91136 Jul 11 '12 at 7:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.