如何以下列格式转换数组

时间:2015-02-19 13:09:22

标签: php arrays multidimensional-array

我正在使用以下多维数组。我想以数组2中给出的新数组形式转换此数组

Array-1

阵 (

[0] => Array
    (
        [0] => PRpriyer
        [1] => job1232
    )

[1] => Array
    (
        [0] => PRpriyer
        [1] => job1234
    )

[2] => Array
    (
        [0] => PRpriyer
        [1] => job1235
    )

[3] => Array
    (
        [0] => PRpriyer
        [1] => job1236
    )

[4] => Array
    (
        [0] => JSjais
        [1] => job1232
    )

[5] => Array
    (
        [0] => JSjais
        [1] => job1234
    )

我想要这种格式的新数组,如下所示: 阵列-2

阵 (

[0] => Array
    (
        [0] => PRpriyer
        [1] => job1232 | job1234 | job1235 | job1236
    )

[1] => Array
    (
        [0] => JSjais
        [1] => job1232 | job1234
    )

如何转换?请帮忙

3 个答案:

答案 0 :(得分:2)

foreach($array1 as $value){   $tmparray[$value[0]][]=$value[1]; }
foreach($tmparray as $key=>$values){   
  $result[]=array($key,implode(" | ",$values)); 
}

print_r($result);

答案 1 :(得分:0)

$ array1是原始的

$newArray = array();
foreach($array1 as $value){

   $newArray[$value[0]] .= $value[1]." | ";

}

newArray:

Array
(
    [PRpriyer] => job1232 | job1234 | job1235 | job1236 | 
    [JSjais] => job1232 | job1234 | 
)

答案 2 :(得分:0)

我要做以下事情:

function convertArray($inputArray) {
    $array = array();
    foreach ($inputArray as $value) {
        $array[$value[0]][] = $value[1];
   }

    $returnArray = array();
    foreach ($array as $key => $values) {
        $returnArray[] = array($key, implode(" | ", $values));
    }
    return $returnArray;
}

$result = convertArray($inputArray);
print_r($result);