shane0714 Posted May 22, 2007 Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52431-solved-convert-two-dimensional-array-to-string/ Share on other sites More sharing options...
Guest JMitchell Posted May 22, 2007 Share Posted May 22, 2007 so you only want array[*][1] ? foreach($array as $v) $str .= ', '.$v[1]; Quote Link to comment https://forums.phpfreaks.com/topic/52431-solved-convert-two-dimensional-array-to-string/#findComment-258776 Share on other sites More sharing options...
sasa Posted May 22, 2007 Share Posted May 22, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/52431-solved-convert-two-dimensional-array-to-string/#findComment-258777 Share on other sites More sharing options...
shane0714 Posted May 22, 2007 Author Share Posted May 22, 2007 Thank you sasa! That worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/52431-solved-convert-two-dimensional-array-to-string/#findComment-259015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.