简体   繁体   中英

PHP Dynamically fill array

I have a large array of product data organized as:

Array ( 
[products] => Array ( 
        [0] => Array ( 
            [title] => Tropika
            [id] => 21645 
            [created_at] => 2016-03-14T19:04:19Z 
            [updated_at] => 2016-05-03T17:52:59Z 
            [type] => variable
            ...
            [variations] => Array ( 
                [0] => Array ( 
                    [id] => 21648 
                    [created_at] => 2016-03-14T19:04:19Z
                    ...
                    [attributes] => Array ( 
                        [0] => Array ( 
                            [name] => nicotine content 
                            [slug] => nicotine-content 
                            [option] => 12 )
                        ...
                    ...
                [1] => array (
                    ...
                ...
            ...
        ...

I currently parse/flip this into the following "buffer array"

Array ( 
    [Tropika] => Array ( 
        [0] => 21645 // parent product id
        [21648] => Array ( [12] => nicotine content [30ml] => volume )
        ...
    ... 

The reason I parse it as above is so I can loop with a couple isset statements for the title and attributes. This lets me quickly go through a user provided csv file and check for matches.

The issue I have is taking the resulting matches and getting an array that matches the following to send back to the REST API handling product updates.

$data = [
    'products' => [
        [
            'id' => 604,
            'variations' => [
                [
                    'id' => 609,
                    'regular_price' => '29.99'
                ],
                [
                    'id' => 611,
                    'regular_price' => '29.99'
                ]
            ]
        ]
    ]
];

I'm constrained to PHP 5.3.28 The question specifically is this: I can flip the array / array_push and this lets me add the matching variations no problem. But how can I do that without also adding a new product element each time (the variations must be a sub-array of their parent product.)

Right now I add the parent product 8 times with 1 variation in each, I need to reverse those numbers so it's 1 product with it's variation as children.

At first I thought I could add the variation as children by using the product id as the key for the products array (flipping it later) but that didn't quite meet the spec for sending bulk product data back either.

This is the block (within an !feof while) that's responsible for getting matches based on title and 2 variations.

            if (isset($rjw_buffer[$rjw_csv[0]]))    // If Title Matches
            {

                foreach($rjw_buffer[$rjw_csv[0]] as $buff_id => $buff_var)  // Parse Variations for Matches.
                {
                    if (isset($buff_var[$rjw_variant[0]]))      // If one matches, test the other.
                    {
                        if (isset($buff_var[$rjw_variant[1]]))  //  If both match, update product.
                        {
                            $rjw_data['products'][$rjw_buffer[$rjw_csv[0]][0]] = array(
                                'id' => $rjw_buffer[$rjw_csv[0]][0],
                                'variations' => array());
                            array_push($rjw_data['products'][$rjw_buffer[$rjw_csv[0]][0]]['variations'],
                                array(
                                    'id' => $buff_id,
                                    'stock_quantity' => $rjw_csv[5],
                                    'regular_price' => $rjw_csv[9]));

                        }
                    }
                }
            }

I then put the array through array_values. Right now I'm producing

Array ( 
    [0] => Array ( 
        [21638] => Array ( 
            [id] => 21638 
            [variations] => Array ( 
                [0] => Array ( 
                    [id] => 21819 
                    [stock_quantity] => 8.00 
                    [regular_price] => 29.99 ) ) ) 
        [21645] => Array ( 
            [id] => 21645 
            [variations] => Array ( 
                [0] => Array ( 
                    [id] => 21648 
                    [stock_quantity] => 8.00 
                    [regular_price] => 29.99 
                ) 
            ) 
        ) 
    )
)

which is so close. There should be 8 variations though, and match the spec.

Alright I figured it out.

                    if (isset($buff_var[$rjw_variant[0]]))      // If one matches, test the other.
                    {
                        if (isset($buff_var[$rjw_variant[1]]))  //  If both match, update product.
                        {
                            // data[parentid]['id'] = parentid.
                            $rjw_data[$rjw_buffer[$rjw_csv[0]][0]]['id'] = $rjw_buffer[$rjw_csv[0]][0];
                            $rjw_data[$rjw_buffer[$rjw_csv[0]][0]]['title'] = $rjw_csv[0];

                            // Make sure variations is an array so array_push works.
                            if(!isset($rjw_data[$rjw_buffer[$rjw_csv[0]][0]]['variations']))
                                $rjw_data[$rjw_buffer[$rjw_csv[0]][0]]['variations'] = array();
                            // data[parentid]['variations'] new array with info.
                            array_push($rjw_data[$rjw_buffer[$rjw_csv[0]][0]]['variations'],array(
                                'id' => $buff_id,
                                'stock_quantity' => $rjw_csv[5],
                                'regular_price' => $rjw_csv[9])
                            );
                        }   // MATCH LOOP
                    }

...

$rjw_data = array('products' => array_values($rjw_data));

Makes the previous array match the spec for sending data (ie)

Array (
    'products' => array(
        array(
            'id' = 21648
            'variations' => array(
                array(
                    'id' = 21638
                    'stock_quantity' = 12.00
                    )
                array(
                    ...
                    )
            )
        )
        array(
            'id' = 21648
            'variations' = array(
                array(
                    ...
                )
            )
        )
        ...
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM