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>";
}
7
  • How do you want to cope with the items being layered under 'parent_id' - i.e. 1554? Commented Sep 24, 2018 at 10:20
  • Are you trying to insert new data on existing array? in this case you must use push_array and specify where to add new data on existing array index. Take a look at this post may be can help clearify your needs. stackoverflow.com/questions/52368826/… Commented Sep 24, 2018 at 10:20
  • Post your first array properly. Commented Sep 24, 2018 at 10:21
  • @TrickStar csv array ? Commented Sep 24, 2018 at 10:22
  • @Sigma No i am trying to insert same array data into new array Commented Sep 24, 2018 at 10:23

1 Answer 1

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>";
Sign up to request clarification or add additional context in comments.

3 Comments

Based on your answer all values are stored on one array. I want to store the values into $data array based on parent_id.
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.
Thank You for Your Answer It helped me a lot. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.