0

I have the following PHP array

    array (size=14)
  0 => 
    object(stdClass)[39]
      public 'department' => string 'BOOKS' (length=32)
      public 'dep_url' => string 'cheap-books' (length=32)
      public 'category' => string 'Sci-fi' (length=23)
      public 'cat_url' => string 'sci-fi' (length=23)
  1 => 
    object(stdClass)[40]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
      public 'category' => string 'Rings' (length=23)
      public 'cat_url' => string 'rings' (length=23)
  2 => 
    object(stdClass)[41]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
      public 'category' => string 'Earings' (length=23)
      public 'cat_url' => string 'cheap-earings' (length=23)

As you can see its an array of departments with their categories, how can i merge the array to get something like the following:

  array (size=14)
  0 => 
    object(stdClass)[39]
      public 'department' => string 'BOOKS' (length=32)
      public 'dep_url' => string 'cheap-books' (length=32)
        innerarray[0] = 
            public 'category' => string 'Sci-fi' (length=23)
            public 'cat_url' => string 'sci-fi' (length=23)
  1 => 
    object(stdClass)[40]
      public 'department' => string 'JEWELRY' (length=32)
      public 'dep_url' => string 'cheap-jewels' (length=32)
        innerarray[0] = 
                   public 'category' => string 'Rings' (length=23)
                   public 'cat_url' => string 'rings' (length=23)
        innerarray[1] = 
                  public 'category' => string 'Earings' (length=23)
                  public 'cat_url' => string 'cheap-earings' (length=23)

I want to merge the array by department with the least amount of loops.

I hope i am clear with my question, thanks for any help you can give!

scrowler
  • 24,273
  • 9
  • 60
  • 92
AL DI
  • 560
  • 6
  • 24
  • You have to redefine your object structure, to allow what you need. So instead of a category as string, it will become an array of strings. Then just loop through your current objects and compare if DEPARTMENT are equals and do the merge – JorgeeFG Sep 21 '16 at 23:37

1 Answers1

1

It would be best if you had a department ID (a primary key) to use to identify duplicates, but in lieu of that you should use the department name and URL together to match them.

Something like this should work:

$output = [];
foreach ($array as $entry) {
    // no department ID, so create one for indexing the array instead...
    $key = md5($entry->department . $entry->dep_url);

    // create a new department entry
    if (!array_key_exists($key, $output)) {
        $class = new stdClass;
        $class->department = $entry->department;
        $class->dep_url = $entry->dep_url;
        $class->categories = [];

        $output[$key] = $class;
    }

    // add the current entry's category data to the indexed department
    $category = new stdClass;
    $category->category = $entry->category;
    $category->cat_url = $entry->cat_url;

    $output[$key]->categories[] = $category;
}

This will give you an array of department objects which contains, each of which contains an array of category objects. It'll be indexed by a hash which you create manually in lieu of a department ID/primary key to use instead.

To remove those keys simply do:

$output = array_values($output);
scrowler
  • 24,273
  • 9
  • 60
  • 92