Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,通过重复值组合两个数组,并合并值-逗号分隔_Php_Arrays - Fatal编程技术网

在PHP中,通过重复值组合两个数组,并合并值-逗号分隔

在PHP中,通过重复值组合两个数组,并合并值-逗号分隔,php,arrays,Php,Arrays,我有两个数组。第一个有一个数组,另一个有两个数组。因此,它们是这样分组的 array(1) { [0]=> array(9) { ["id"]=> int(9) ["product_group_id"]=> int(9) ["sold_to"]=> int(107824) ["value"]=> int(19845) ["created_at"]=> string(19)

我有两个数组。第一个有一个数组,另一个有两个数组。因此,它们是这样分组的

array(1) {
  [0]=>
  array(9) {
    ["id"]=>
    int(9)
    ["product_group_id"]=>
    int(9)
    ["sold_to"]=>
    int(107824)
    ["value"]=>
    int(19845)
    ["created_at"]=>
    string(19) "2019-01-24 14:48:25"
    ["updated_at"]=>
    string(19) "2019-01-24 14:48:25"
  }
}
array(2) {
  [0]=>
  array(9) {
    ["id"]=>
    int(17)
    ["product_group_id"]=>
    int(8)
    ["sold_to"]=>
    int(126124)
    ["value"]=>
    int(15555)
    ["created_at"]=>
    string(19) "2019-01-24 14:48:25"
    ["updated_at"]=>
    string(19) "2019-01-24 14:48:25"
  }
  [1]=>
  array(9) {
    ["id"]=>
    int(20)
    ["product_group_id"]=>
    int(8)
    ["sold_to"]=>
    int(141877)
    ["value"]=>
    int(10848)
    ["created_at"]=>
    string(19) "2019-01-24 14:48:25"
    ["updated_at"]=>
    string(19) "2019-01-24 14:48:25"
  }
}
我想根据
product\u group\u id
like将第二个数组合并到一个数组中


['id'=>“17,20”、'product\u group\u id'=>“9”、'sall\u to'=>“107824126124”]

试试这个:下面的测试用例

function groupImplode( $key, $array, $glue = ',' ){
    $result = [];
    foreach( $array as $index => $element ){
        if( array_key_exists( $key, $element ) ){
            if( isset( $result[ $element[$key] ] ) ){
                //concat values
                foreach( array_keys( $element ) as $elementKey ){
                    if( $elementKey !== $key )
                        $result[ $element[$key] ][ $elementKey ] .= $glue . $element[$elementKey];
                }
            }else
                $result[ $element[$key] ] = $element;
        }
    }
    return array_values( $result );
}

$myArray = [
    ['id' => 17, 'product_group_id' => 8, 'sold_to' => 126124, 'value' => 15555, 'created_at' => '2019-01-24 14:48:25', 'updated_at' => '2019-01-24 14:48:25' ],
    ['id' => 20, 'product_group_id' => 8, 'sold_to' => 141877, 'value' => 10848, 'created_at' => '2019-01-24 14:48:25', 'updated_at' => '2019-01-24 14:48:25' ],
];

$res = groupImplode( 'product_group_id', $myArray );
print_r($res);

在“产品组id”上构建关联数组,然后使用数组值重置键

foreach($arr as $sub){
    foreach($sub as $item){
        $new[$item["product_group_id"]][] = $item;
    }
}
$new = array_values($new);

为什么不从查询本身获得正确的结果呢?为什么你需要把事情复杂化?因为它们来自一个API。你有没有尝试向我们展示一下,至少…?如果它们来自一个API,为什么你不共享json而不是var_转储?我们不能用那个垃圾堆。