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?

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.