PHP array two-dimensional array conversion
过去多啦不再A梦
过去多啦不再A梦 2017-07-04 13:46:19
0
3
892
$oldArr = [
    ['a', 1],
    ['', 2],
    ['', 3],
    ['b', 4],
    ['', 5],
    ['c',6],
    ['',7]
];

转换为

$newArr = [
    'a'=>'1,2,3',
    'b'=>'4,5',
    'c'=>'6,7'
]
过去多啦不再A梦
过去多啦不再A梦

reply all(3)
某草草
<?php

$oldArr = [
    ['a', 1],
    ['', 2],
    ['', 3],
    ['b', 4],
    ['', 5],
    ['c',6],
    ['',7]
];

$newArray = [];

$temp = '';
foreach ($oldArr as $item) {
    if ($item[0]) {
        $temp = $item[0];
        $newArray[$temp] = $item[1];
        continue;
    }
    $newArray[$temp] .= ','.$item[1];
}

var_dump($newArray);
学习ing

Shouldn’t we think about why the arrays are spliced ​​like this?

大家讲道理

I feel that when fetching it from the database, it can be processed with mysql built-in functions
such as concat() or concat_group()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template