1

Hello i want to create array form one array. I am reading values form CSV files and create one Array First. After creating That Array then i grouping the Array based on 'parent_id'.

newcsv array output look like this:

array(148) {
  [0]=>
  array(4) {
    ["parent_id"]=>
    int(1554)
    ["id"]=>
    int(1556)
    ["sku"]=>
    string(11) "12000-Ash-S"
    ["qty"]=>
    string(3) "199"
  }
  [1]=>
  array(4) {
    ["parent_id"]=>
    int(1554)
    ["id"]=>
    int(1555)
    ["sku"]=>
    string(11) "12000-Ash-M"
    ["qty"]=>
    string(3) "691"
  }
  [2]=>
  array(4) {
    ["parent_id"]=>
    int(1554)
    ["id"]=>
    int(1557)
    ["sku"]=>
    string(11) "12000-Ash-L"
    ["qty"]=>
    string(3) "740"
  }
  [3]=>
  array(4) {
    ["parent_id"]=>
    int(1554)
    ["id"]=>
    int(1558)
    ["sku"]=>
    string(12) "12000-Ash-XL"
    ["qty"]=>
    string(3) "735"
  }

}

The following code using for Grouping Array

$result = array();
foreach ($newcsv as $element) {
    $result[$element['parent_id']][] = $element;

}

Grouping the Array based on 'parent_id' OUTPUT:

array(3) {
  [1554]=>
  array(48) {
    [0]=>
    array(4) {
      ["parent_id"]=>
      int(1554)
      ["id"]=>
      int(1556)
      ["sku"]=>
      string(11) "12000-Ash-S"
      ["qty"]=>
      string(3) "199"
    }
    [1]=>
    array(4) {
      ["parent_id"]=>
      int(1554)
      ["id"]=>
      int(1555)
      ["sku"]=>
      string(11) "12000-Ash-M"
      ["qty"]=>
      string(3) "691"
    }
    [2]=>
    array(4) {
      ["parent_id"]=>
      int(1554)
      ["id"]=>
      int(1557)
      ["sku"]=>
      string(11) "12000-Ash-L"
      ["qty"]=>
      string(3) "740"
    }
  }
  [1603]=>
  array(20) {
    [0]=>
    array(4) {
      ["parent_id"]=>
      int(1603)
      ["id"]=>
      int(1605)
      ["sku"]=>
      string(13) "12300-Black-S"
      ["qty"]=>
      string(4) "3000"
    }
    [1]=>
    array(4) {
      ["parent_id"]=>
      int(1603)
      ["id"]=>
      int(1604)
      ["sku"]=>
      string(13) "12300-Black-M"
      ["qty"]=>
      string(4) "3000"
    }
    [2]=>
    array(4) {
      ["parent_id"]=>
      int(1603)
      ["id"]=>
      int(1606)
      ["sku"]=>
      string(13) "12300-Black-L"
      ["qty"]=>
      string(4) "3000"
    }
  }
  [1624]=>
  array(80) {
    [0]=>
    array(4) {
      ["parent_id"]=>
      int(1624)
      ["id"]=>
      int(1626)
      ["sku"]=>
      string(13) "12500-White-S"
      ["qty"]=>
      string(4) "1858"
    }
    [1]=>
    array(4) {
      ["parent_id"]=>
      int(1624)
      ["id"]=>
      int(1625)
      ["sku"]=>
      string(13) "12500-White-M"
      ["qty"]=>
      string(4) "2295"
    }
    [2]=>
    array(4) {
      ["parent_id"]=>
      int(1624)
      ["id"]=>
      int(1627)
      ["sku"]=>
      string(13) "12500-White-L"
      ["qty"]=>
      string(4) "1974"
    }

  }
}

Now i want to create new array form above array. I want array like following.

$data = [ 'update' => [
        [
            'id' => 733,
            'sku' => '344'
        ],
        [
            'id' => 733,
            'sku' => '200'
        ]
    ]
];

I am using following code create array. But its not working For me.

$data = array();
foreach($result as $key => $value){
$data = ('update' =>array('id'=>$value['id'],'regular_price'=>$value['sku']));
echo"<pre>";
print_r($data);
echo"</pre>";
}
developerme
  • 1,845
  • 2
  • 15
  • 34

1 Answers1

1

As your data is layered under the parent_id, I've added another level of looping, so the first layer is for the parent_id, the second is for each item in there. As you seem to want everything under 'update', I add it to the point where the data is added to the array and use [] to say add the new item to this.

$data = array();
foreach($result as $key => $parent){
    foreach ( $parent as $item ) {
        $data[$key]['update'][] = array( 'id'=>$item['id'],'sku'=>$item['sku']);
    }
}
echo"<pre>";
print_r($data);
echo"</pre>";
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Based on your answer all values are stored on one array. I want to store the values into $data array based on parent_id. – developerme Sep 24 '18 at 10:44
  • I asked that earlier as a comment, but I've added in the parent_id as part of the structure. You can easily move it to after the `['update']` part if required. – Nigel Ren Sep 24 '18 at 10:46
  • Thank You for Your Answer It helped me a lot. Thanks – developerme Sep 24 '18 at 11:18