PHP动态填充数组

时间:2016-05-19 17:28:44

标签: php arrays parsing csv

我有大量的产品数据组织为:

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 (
                    ...
                ...
            ...
        ...

我目前正在将此解析/翻转到以下“缓冲阵列”

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

我解析它的原因是,所以我可以循环使用一对isset 标题和属性的陈述。这让我快速通过了 用户提供了csv文件并检查匹配。

我遇到的问题是获取结果匹配并获取数组 匹配以下内容以发送回REST API处理 产品更新。

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

我受限于PHP 5.3.28具体问题是: 我可以翻转数组/ array_push,这可以让我添加匹配 变化没问题。但是,如果不添加新的,我怎么能这样做呢 每次产品元素(变化必须是他们的子数组) 母产品。)

现在我添加父产品8次,每次都有1个变化,我需要 扭转这些数字,因此它是1个产品,随着孩子的变化。

起初我以为我可以通过使用来添加变体作为孩子 产品ID是产品阵列的关键(稍后翻转),但这不符合发回批量产品数据的规范。

这是负责获取的块(在!feof中) 基于标题和2种变化的匹配。

            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]));

                        }
                    }
                }
            }

然后我将数组放入array_values。现在我正在制作

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 
                ) 
            ) 
        ) 
    )
)

这是如此接近。应该有8个变化,但匹配 规范。

1 个答案:

答案 0 :(得分:0)

好吧我明白了。

                    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));

使前一个数组与发送数据的规范匹配(即)

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