0

I'm trying to make a parent child like array out of a previous array of things, some are duplicates, some are not.

I have an array that spits out like so: [0] => Array ( [parent] => dogs [child_cat_1] => category one [child_cat_2] => category two [title] => Title [price] => 9.49 [sku] => 3558505550 [old_post_id] => 110 )

[1] => Array
    (
        [parent] => cats
        [child_cat_1] => category one
        [child_cat_2] => category six
        [title] => Title
        [price] => 16.49
        [sku] => 2251752419
        [old_post_id] => 113
    )

[2] => Array
    (
        [parent] => cats
        [child_cat_1] => category three
        [child_cat_2] => category nine
        [title] => Title
        [price] => 59.99
        [sku] => 7944100467
        [old_post_id] => 114
    )

[3] => Array
    (
        [parent] => dogs
        [child_cat_1] => category one
        [child_cat_2] => category two
        [title] => Title
        [price] => 69.99
        [sku] => 85932810243
        [old_post_id] => 117
    )

I'm having a real hard time creating a new array based off these arrays, and turning it into an array of parents and children.

I tried doing what this post said, but I couldn't get it to work as I'm not entirely sure of what the 'parent' is going to be.

I tried doing this, but I can't figure out how to factor in child_cat_2, and also remove the duplicates. I've also read pretty much every "Building Hierarchy out of array" on Stackoverflow, but I just can't figure this one out.

foreach($new_array as $k => $v){
    $array[$v['parent']] = $v['child_cat_1'];
}

Ideally, what I'm trying to accomplish is arranging each child_cat_2 under child_cat_1, and then both under parent, but then also remove the duplicates. I think the parent only has 11 types, but the two children have up to 100 each.

Can someone point me in the right direction to get these arrays sorted out.

Community
  • 1
  • 1
David
  • 2,094
  • 3
  • 30
  • 47

1 Answers1

0

What you've described is a 3-level nested array. The code you've posted certainly won't accomplish that. You have nodes with four properties other than your array structure, so that is what you should build in a loop. Then you place them into your structure depending on the other three properties.

$results = array();
foreach ($data as $datum){
  $node = array(
    'title' => $datum['title']
    'price' => $datum['price']
    'sku' => $datum['sku']
    'old_post_id' => $datum['old_post_id']  
  );

  if (!is_array($results[$datum['parent']])) {
    $results[$datum['parent']] = array();
  }
  if (!is_array($results[$datum['parent']][$datum['child_cat_1']])) {
    $results[$datum['parent']]
      [$datum['child_cat_1']] = array();
  }
  if (!is_array($results[$datum['parent']][$datum['child_cat_1']][$datum['child_cat_2']])) {
    $results[$datum['parent']]
      [$datum['child_cat_1']]
      [$datum['child_cat_2']] = array();
  }

  $results[$datum['parent']]
    [$datum['child_cat_1']]
    [$datum['child_cat_2']][] = $node;
}
Seth Battin
  • 2,811
  • 22
  • 36
  • $data => $datum? Can you please explain as I don't understand how to get my data into this foreach loop – David Feb 21 '14 at 03:08
  • That was a typo. It was meant to be a standard php foreach loop. Feel free to correct that line; it has no bearing on the core of problem or my solution. – Seth Battin Feb 21 '14 at 03:39