[英]Merge multple arrays from one result into a single array in PHP
I'm really sorry to bug you, put I've got a problem that I've been trying to resolve for quite some time now. 非常抱歉给您打扰,请问我有一个问题已经解决了一段时间了。 I've done some research and have found things like array_merge but it doesn't appear to help me. 我已经进行了一些研究,发现了诸如array_merge之类的东西,但似乎对我没有帮助。
Anyway, enough waffle. 无论如何,足够的华夫饼干。 I have a result of a query that looks something like this: 我得到的查询结果看起来像这样:
Array
(
[0] => STRINGA
)
Array
(
[0] => STRINGA
[1] => STRINGB
)
Array
(
[0] => STRINGA
[1] => STRINGB
[2] => STRINGC
)
Array
(
[0] => STRINGD
[1] => STRINGC
[2] => STRINGA
[3] => STRINGB
[4] => STRINGE
[5] => STRINGF
)
How can I combine the above into one array so that the result will look more like: 我如何将以上内容合并到一个数组中,以便结果看起来更像:
Array
(
[0] => STRINGA
[1] => STRINGB
[2] => STRINGC
[3] => STRINGD
[4] => STRINGE
[5] => STRINGF
)
Duplicates in the original arrays can be ignored as I only need the string to be placed into the new array once. 原始数组中的重复项可以忽略,因为我只需要将字符串一次放入新数组中即可。
Any help would be massively appreciated. 任何帮助将不胜感激。
Thank you. 谢谢。
EDITED: This is the block of code that brings out the result from the database: 编辑:这是从数据库中带出结果的代码块:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
foreach($row as $splitrow) {
if(NULL != $splitrow) {
$therow = explode(';',$splitrow);
}
//print_r retrieves result shown above
print_r($therow);
}
}
$bigarray = array(
array (
0 => 'STRINGA',
),
array (
0 => 'STRINGA',
1 => 'STRINGB',
),
array(
0 => 'STRINGA',
1 => 'STRINGB',
2 => 'STRINGC',
)
);
$result = array_values(
array_unique(
array_merge( $bigarray[0], $bigarray[1], $bigarray[2] )
)
);
// array_merge will put all arrays together, including duplicates
// array_unique removes duplicates
// array_values will sort out the indexes in ascending order (1, 2, 3 etc...)
$bigarray = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
foreach($row as $value){
if($value != NULL){
$therow = explode(';',$value);
foreach($therow as $key=>$values){
//push the value into the single array 'bigarray'
array_push($bigarray, $values);
}
}
}
}
//remove duplicates
$uniquearray = array_unique($bigarray);
//reset key values
$indexedarray = array_values($uniquearray);
print_r($indexedarray);
Thanks to all of those that helped, much appreciated! 感谢所有这些帮助,非常感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.