Jump to content

Is there a way to join several arrays stored in one variable?


magnusalex

Recommended Posts

Hi,

 

I have this problem I really hope someone can help me with. I have a variable that stores several arrays. The content of the variable is as follows:

 

print_r(array_values($array));

Array ( 
[0] => Value 0)
[1] => Value 1)
[2] => Value 2)
[3] => Value 3)
) 
Array ( 
[0] => Value 4)
[1] => Value 5)
[2] => Value 6)
[3] => Value 7)
[4] => Value 
) 
Array ( 
[0] => Value 9)
[1] => Value 10)
[2] => Value 11)
[3] => Value 12)
[4] => Value 13)
)

 

Is there a way to join this multiple arrays into a single array? I do not need to keep the Key, as all of the values contains text that I only want to sort with sort();. If I sort it like this, it will only come out as a,b,c,d - a,b,c,d, not a, a, b, b, c, c, d, d and so forth.

 

The php script that creates this array is as follows:

 

while($row = mysql_fetch_array($run_mysql_query)){;

 $models = $row['text_value']; 
 $attribute1 = $row['attribute1']; 
 $attribute2 = $row['attribute2'];

 $array = explode(',', $models);

		foreach($array as &$value) {
   			$value = $row['varchar_value'] . " - " . $value . " - " . $attribute1 . " - " . $attribute2;
		}

 

I will appreciate any help!

Link to comment
Share on other sites

Thank you!

 

I can however, not get it to work properly... It still prints as several arrays.

 

I put the code here:

 

while($row = mysql_fetch_array($run_mysql_query)){;

 $models = $row['text_value']; 
 $attribute1 = $row['attribute1']; 
 $attribute2 = $row['attribute2'];

 $array = explode(',', $models);

		foreach($array as &$value) {
   			$value = $row['varchar_value'] . " - " . $value . " - " . $attribute1 . " - " . $attribute2;
		}

	$newArray = array();
	foreach ( $array as $val ) {
  		$newArray = array_merge($newArray, $val);
	}

print_r($newArray);	

}

 

Is that correct? If I put it outside the while loop, it will only print the last of the arrays...

Link to comment
Share on other sites

You didn't say you already had a loop that was generating these arrays.

 

$newArray = array();
while($row = mysql_fetch_array($run_mysql_query)){;  //this is wrong, no semicolon here.

 $models = $row['text_value']; 
 $attribute1 = $row['attribute1']; 
 $attribute2 = $row['attribute2'];

 $array = explode(',', $models);
         $newArray = array_merge($newArray, $array);

		foreach($array as &$value) {
   			$value = $row['varchar_value'] . " - " . $value . " - " . $attribute1 . " - " . $attribute2;
		}




}	
print_r($newArray);	

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.