往二维数组中插入新的元素,非foreach

用array_walk往二维数组中插入新的元素,非foreach循环

image description

用array_walk往二维数组中插入新的元素

非foreach循环插入。

测试例子: 被插入原数组数据:


    $dataList = [
        [
            "articleCode" => "L0104505",
            "articleName" => "通用流量100M",
            "isOverAllCount" =>  0,
            "isOverEverydayCount" =>  0,
            "articleAttr" =>  "100M",
            "articlePoints" =>  "1200",
            "isFuture" =>  "1",
            "onSale" =>  "1"
        ],
        [
            "articleCode" => "L0104505",
            "articleName" => "通用流量500M",
            "isOverAllCount" =>  0,
            "isOverEverydayCount" =>  0,
            "articleAttr" =>  "500M",
            "articlePoints" =>  "6000",
            "isFuture" =>  "1",
            "onSale" =>  "1"
        ],
    ];

需要插入的数据:


    $dataCateArr = [ 'cateCode' => 'L0104' ];//单条情况
    $dataCateArr = [ 'cateCode' => 'L0104' , 'testCode' => 'L01' ];//多条情况

插入数据:


    array_walk( $dataList , function( &$dataValue , $key , $dataCateArr ) {
        $dataValue = array_merge( $dataValue , $dataCateArr );
    } , $dataCateArr );

结果对比: 未插入前: description 单条情况: description 多条情况: description

    END