Jump to content

[SOLVED] Convert Two-Dimensional Array to String


shane0714

Recommended Posts

I'm trying to convert a two-dimensional array to a string that I can put in a database. It needs to be searchable so the serialize() function will not work. I found a script at php.net that will implode a multi-dimensional array and it works fine. The script is:

function implode_r ($glue, $pieces){
$out = "";
foreach ($pieces as $piece)
  if (is_array ($piece)) $out .= implode_r ($glue, $piece); // recurse
  else                   $out .= $glue.$piece;

return $out;
}

$imagenames = implode_r(", ",$images);

 

The array I am using changes depending on how many images I have in a page. This is an example of an array:

array(2) { [0]=>  array(2) { [0]=>  string(41) "src="/images/userfiles/image/uploader.png" [1]=>  string(12) "uploader.png" } [1]=>  array(2) { [0]=>  string(42) "src="/images/userfiles/image/llcorner2.gif" [1]=>  string(13) "llcorner2.gif" } }

 

And this is the result:

, src="/images/userfiles/image/uploader.png, uploader.png, src="/images/userfiles/image/llcorner2.gif, llcorner2.gif

 

What I want to do is return only the second, fourth, sixth, etc. parts of the array. So, this is images[0][1],images[1][1],images[2][1],etc. Then I want to store this comma separated result as a variable.

 

I've been searching around for several hours now and have not been able to figure out a way to do this.

try

<?php
$a = array( 0 =>  array(0 =>"src=\"/images/userfiles/image/uploader.png\"", 1 => "uploader.png"),
		1 =>  array(0 => "src=\"/images/userfiles/image/llcorner2.gif\"", 1 => "llcorner2.gif" ));
$tmp = array();
foreach ($a as $b) $tmp[] = $b[1]; //pull out data 
echo $out = implode(', ', $tmp); //and implode 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.