0

真的很抱歉打扰你,我有一个问题,我已经尝试解决了很长一段时间了。我做了一些研究,发现了像 array_merge 这样的东西,但它似乎对我没有帮助。

无论如何,足够的华夫饼。我的查询结果如下所示:

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
)

如何将上述内容组合到一个数组中,以使结果看起来更像:

Array
(
    [0] => STRINGA
    [1] => STRINGB
    [2] => STRINGC
    [3] => STRINGD
    [4] => STRINGE
    [5] => STRINGF
)

可以忽略原始数组中的重复项,因为我只需要将字符串放入新数组一次。

任何帮助将不胜感激。

谢谢你。

编辑:这是从数据库中提取结果的代码块:

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);                                    
    }
}
4

2 回答 2

5
$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...)
于 2013-03-06T16:59:32.987 回答
0
    $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);

感谢所有帮助过的人,非常感谢!

于 2013-03-07T14:27:22.583 回答