AL DI AL DI - 2 months ago 8
PHP Question

How to sort and merge a PHP array by a specific key?

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!

Answer

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