magnusalex Posted March 16, 2011 Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230819-is-there-a-way-to-join-several-arrays-stored-in-one-variable/ Share on other sites More sharing options...
ManiacDan Posted March 16, 2011 Share Posted March 16, 2011 $newArray = array(); foreach ( $array as $val ) { $newArray = array_merge($newArray, $val); } print_r($newArray); -Dan Link to comment https://forums.phpfreaks.com/topic/230819-is-there-a-way-to-join-several-arrays-stored-in-one-variable/#findComment-1188237 Share on other sites More sharing options...
magnusalex Posted March 16, 2011 Author Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230819-is-there-a-way-to-join-several-arrays-stored-in-one-variable/#findComment-1188250 Share on other sites More sharing options...
ManiacDan Posted March 16, 2011 Share Posted March 16, 2011 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 https://forums.phpfreaks.com/topic/230819-is-there-a-way-to-join-several-arrays-stored-in-one-variable/#findComment-1188257 Share on other sites More sharing options...
magnusalex Posted March 16, 2011 Author Share Posted March 16, 2011 I see that now... sorry about that! This solution works like a charm! Thank you very much! Link to comment https://forums.phpfreaks.com/topic/230819-is-there-a-way-to-join-several-arrays-stored-in-one-variable/#findComment-1188307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.