Imploding an Associative Array

In the previous article, I said that implode() only works on simMODX logople, numeric arrays, but what if you want to do something similar with an associative array of keys and values. You can create a new array that implode() will like and run impolde() on that array. In this article we’ll see how.

Let’s take this array from the previous article:

[code language=”php”]
$nameArray = array(
‘BobRay’ => ‘bobray@somewhere.com’,
‘JohnDoe’ => ‘johndoe@gmail.com’,
‘JaneRoe’ => ‘janeroe@aol.com’,
);
[/code]

We want to print a line of text to display the results like this:

The values are: BobRay (bobray@somewhere.com), JohnDoe (johndoe@gmail.com), JaneRoe (janeroe@aol.com)

We can’t call implode() directly on the associative array, but we can create a new simple numeric array that will give us what we want when we use implode() on it.

 

The Code

Here’s our snippet. Notice that at the top of the snippet we’ve included the array of names we showed above, though it could just as well have come from somewhere else.

[code language=”php”]
$output = ”;

$nameArray = array(
‘BobRay’ => ‘bobray@somewhere.com’,
‘JohnDoe’ => ‘johndoe@gmail.com’,
‘JaneRoe’ => ‘janeroe@aol.com’,
);

/* Create the new array */
$users = Array();

/* Add each user to the new array */
foreach ($nameArray as $key => $value) {
$users[] = $key . ‘ (‘ . $value . ‘)’;
}
$output = ‘The values are: ‘. implode(‘, ‘,$users);
return $output;
[/code]

Because of the way PHP handles variable in double quotes, we could also use this line inside the foreach() loop:

[code language=”php”]
$users[] = "$key ($value)";
[/code]

The $key and $value variables will be replaced, the space and the two parentheses will be left as is. Which method you use is a matter of personal preference.

It’s possible to do this in a custom function using a combination of array_map(), array_keys(), and array_values(), but I consider it ugly, inefficient, and difficult to read. It also depends on the version of PHP. For the curious, here it is:

[code language=”php”]
$array = Array(
‘foo’ => 5,
‘bar’ => 12,
‘baz’ => 8
);

// pre PHP 5.3:
$output = ‘The values are: ‘. implode(‘, ‘, array_map(
create_function(‘$k,$v’, ‘return "$k ($v)";’),
array_keys($array),
array_values($array)
));

// PHP 5.3+:
$output = ‘The values are: ‘. implode(‘, ‘, array_map(
function ($k, $v) { return "$k ($v)"; },
array_keys($array),
array_values($array)
));

return $output;
[/code]

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *