Extract collections of similar objects from an array in php

A co-worker was working on some stuff dealing with creating collections of objects within an array so I decided to play around with the idea. Basically, you start with a big array filled of objects and the objective is to create smaller arrays of similar objects. In this case we want collections of person objects who names are the same. Here is what I came up with:

<?php

class person
{
	public $id = null;
	public $name = '';

	public function __construct($id, $name)
	{
		$this->id = $id;
		$this->name = $name;
	}
}

$persons = array();
$persons[] = new person(1, 'bob');
$persons[] = new person(2, 'bob');
$persons[] = new person(3, 'moses');
$persons[] = new person(4, 'joey');
$persons[] = new person(5, 'bob');
$persons[] = new person(6, 'joey');
$persons[] = new person(7, 'bob');
$persons[] = new person(8, 'moses');
$persons[] = new person(9, 'joey');
$persons[] = new person(10, 'joey');

$collections = array();

// loop until there are no person left in array
while(count($persons) > 0)
{
	// new collection and insert first person from array
	$collection = array();
	$collection[] = $persons[0];

	// now remove person from array since already in collection
	array_shift($persons);

	// loop through each person in array
	for($x = 0; $x < count($persons); $x++)
	{
		// check if there is a match
		if($persons[$x]->name == $collection[0]->name)
		{
			// add person to this collection
			$collection[] = $persons[$x];

			// pop if last item
			if($x+1-count($persons) == 0)
			{
				array_splice($persons, $x);
			}
			else
			{
				array_splice($persons, $x, $x+1-count($persons));
			}
			// move back since array position gets deleted, else will skip over next index
			$x--;
		}
	}
	$collections[] = $collection;
}

debug($collections);
function debug($o)
{
	echo '<pre>';
	print_r($o);
	echo '</pre>';
}

And the result is:

Array
(
    [0] => Array
        (
            [0] => person Object
                (
                    [id] => 1
                    [name] => bob
                )

            [1] => person Object
                (
                    [id] => 2
                    [name] => bob
                )

            [2] => person Object
                (
                    [id] => 5
                    [name] => bob
                )

            [3] => person Object
                (
                    [id] => 7
                    [name] => bob
                )

        )

    [1] => Array
        (
            [0] => person Object
                (
                    [id] => 3
                    [name] => moses
                )

            [1] => person Object
                (
                    [id] => 8
                    [name] => moses
                )

        )

    [2] => Array
        (
            [0] => person Object
                (
                    [id] => 4
                    [name] => joey
                )

            [1] => person Object
                (
                    [id] => 6
                    [name] => joey
                )

            [2] => person Object
                (
                    [id] => 9
                    [name] => joey
                )

            [3] => person Object
                (
                    [id] => 10
                    [name] => joey
                )

        )

)

You start with 1 big array and end up with, in this case, 3 arrays of person whos names are ‘bob’, then ‘moses’, and finally ‘joey’.

Leave a Reply

Your email address will not be published. Required fields are marked *