Php help how to turn a two-dimensional array into an one-dimensional array?

there is now a two-dimensional array in the following format:

Array
(
    [0] => Array
        (
            [id] => 38
            [goods_id] => 77426
            [model_name] => 
            [model_sub_name] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [goods_stock] => Array
                (
                    [0] => 321
                )

            [curr_price] => Array
                (
                    [0] => 99
                )

            [goods_weight] => Array
                (
                    [0] => 280
                )

        )

    [1] => Array
        (
            [id] => 39
            [goods_id] => 77426
            [model_name] => 
            [model_sub_name] => Array
                (
                    [0] => 32A
                    [1] => 34A
                    [2] => 36A
                    [3] => 32B
                    [4] => 34B
                    [5] => 36B
                    [6] => 38B
                    [7] => 34C
                    [8] => 36C
                    [9] => 38C
                    [10] => 40C
                    [11] => 
                )

            [goods_stock] => Array
                (
                    [0] => 321
                )

            [curr_price] => Array
                (
                    [0] => 99
                )

            [goods_weight] => Array
                (
                    [0] => 280
                )

        )

)

now you need to change the two-dimensional array into the following format:

Array
(
    [0] => Array
        (
            [model_name] => 
            [model_sub_name] => 
            [goods_stock] => 321
            [curr_price] => 99
            [goods_weight] => 280

        )
    [1] => Array
        (
            [model_name] => 
            [model_sub_name] => 
            [goods_stock] => 321
            [curr_price] => 99
            [goods_weight] => 280

        )
        N

    [N+1] => Array
        (
            [model_name] => 
            [model_sub_name] => 32A
            [goods_stock] => 321
            [curr_price] => 99
            [goods_weight] => 280
               
        )
    [N+2] => Array
        (
            [model_name] => 
            [model_sub_name] => 34A
            [goods_stock] => 321
            [curr_price] => 99
            [goods_weight] => 280
               
        )
    
)

in the above array, goods_stock, curr_price, and goods_weight are all arrays. If there is only one element in the array of these keys, then on the same premise as model_name, goods_stock, curr_price, and goods_weight all take this value, otherwise, take the values in goods_stock, curr_price, and goods_weight corresponding to the subscript of the elements in model_sub_name. For example, when model_sub_name is "white", the corresponding subscript is 1, and there is only one element for goods_stock, curr_price, and goods_weight in the above array, then take the value of this element, and if it is not an element, take the value of goods_stock [1], curr_price [1], and goods_weight [1]. I do not know whether such a description is understood or not. Turn to the gods for help.


you need to set up an empty array first, then loop in two layers, the first layer loops the large array, the model_sub_name, in the second layer loop, and then assign a value to the empty array


first of all, I would like to blame, how to restore the output of print_r to array code, demo can not write, it is going to die.

$result = array_reduce($arr, function ($carry, $item) {
    $extraProperties = ['goods_stock', 'curr_price', 'goods_weight'];

    // 
    $lengths = [];
    foreach ($extraProperties as $property) {
        $lengths[$property] = count($item[$property]) - 1;
    }

    // 
    foreach ($item['model_sub_name'] as $index => $subName) {
        $sub = [
            'model_name' => $item['model_name'],
            'model_sub_name' => $subName,
        ];
        
        foreach ($extraProperties as $property) {
            $propertyIndex = min($index, $lengths[$property]);
            $sub[$property] = $item[$property][$propertyIndex];
        }
       
        $carry[] = $sub;
    }

    return $carry;
}, []);

where $propertyIndex uses the last element by default, which should meet your needs. According to your actual needs, you can

Menu