Jump to content

Interweaving 2 arrays into a string. Please help.


bluegray

Recommended Posts

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?

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

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.