bluegray Posted June 21, 2010 Share Posted June 21, 2010 Hi Php freaks, So I'm pulling an array from a $_POST, and in it I have its keys and values. Take this example: "1" => "a" "2" => "b" "3" => "c" Now, if I can separate the keys and the values into their own arrays, what I need to do is interweave them into a string where all keys are matched up to their values like such: <string> 1 = "a", 2 = "b", 3 = "c" </string> I'm trying to build a function that does, this and here's what it looks like so far: if(!isset($data)) { $boxdata = array(); } else { $boxdata = isset($data)?$data:null ; //var_dump($boxdata) ; foreach ($boxdata as $bd) { $keys = array_keys($boxdata); } I figured it would be simplest to build the string from the foreach () loop, but don't know how to do it, or if there are better ways. Ideas please? Link to comment https://forums.phpfreaks.com/topic/205442-interweaving-2-arrays-into-a-string-please-help/ Share on other sites More sharing options...
beta0x64 Posted June 21, 2010 Share Posted June 21, 2010 Well, there are actually many different ways you could do this, as you can imagine. I don't see why you don't do something like this: echo "<string>"; foreach ($boxdata as $bkey => $bdatum) { echo "$bkey = $bdatum<br />"; } echo "</string>"; this way you don't actually have to deal with two arrays. you can get the key right from the foreach loop Link to comment https://forums.phpfreaks.com/topic/205442-interweaving-2-arrays-into-a-string-please-help/#findComment-1075111 Share on other sites More sharing options...
kenrbnsn Posted June 21, 2010 Share Posted June 21, 2010 Try this: <?php $test = array('first','second','third'); $tmp = array(); foreach ($test as $k => $v) { $tmp[] = $k . ' = ' . $v; } echo implode(', ',$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/205442-interweaving-2-arrays-into-a-string-please-help/#findComment-1075122 Share on other sites More sharing options...
bluegray Posted June 21, 2010 Author Share Posted June 21, 2010 Thanks! Link to comment https://forums.phpfreaks.com/topic/205442-interweaving-2-arrays-into-a-string-please-help/#findComment-1075200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.