Removing duplicate values in Multi-Dimensional Array?

Hi guys,

I’m trying to remove duplicate values from multi-dimentional array.

Here are the test codes,


	function rdv() {
		$target_array = array(
							array("lessonid" => "orb", "classid" => 34),
							array("lessonid" => 43, "classid" => 53),
							array("lessonid" => "orb", "classid" => 13),
						);
		
		echo'<h3>Before:</h3>';
                print_r($target_array);
		
		echo'<h3>After:</h3>';
		$input = array_map("unserialize", array_unique(array_map("serialize", $target_array)));
		
		print_r($input);
	}	

The problem is it did not work, I go this snippet codes from here,
http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php

So what is my mistakes?

Thanks in advance.

Yours aren’t duplicates. The classid’s differ which would cause them to be considered unique.

Proof of Concept:

<?php
function rdv() {
	$target_array = array(
		array("lessonid" => "orb", "classid" => 34),
		array("lessonid" => 43, "classid" => 53),
		array("lessonid" => "orb", "classid" => 13),
		array("lessonid" => "orb", "classid" => 34),
	);

	echo'<h3>Before:</h3>';
	print_r($target_array);

	echo'<h3>After:</h3>';
	$input = array_map("unserialize", array_unique(array_map("serialize", $target_array)));

	print_r($input);
}

rdv();

@cpradio ;
Thanks dude.
Can you help me tweak that codes so it will find duplicate values in lessonid only?

But which one would you want to keep and why? Why would you want classid of 34 versus classid 13?

It should choose the smallest number of classid and remove the (duplicate) of lessonid.

For example if I have this array below.


    $target_array = array(
        array("lessonid" => "orb", "classid" => 27),
        array("lessonid" => 43, "classid" => 53),
        array("lessonid" => "orb", "classid" => 7),
        array("lessonid" => "orb", "classid" => 15,
    );

The result should be like this.


    $target_array = array(
        array("lessonid" => 43, "classid" => 53),
        array("lessonid" => "orb", "classid" => 7),
    );

Thanks in advance, who ever will help me for this.

K, I hope to work on a possible solution for this later. I think it shouldn’t be “too hard”

Okay, here is what I came up with:

<?php
function distinct($items) {
	$result = [];
	for($i = 0; $i < sizeof($items); $i++)
		if (!array_key_exists($items[$i]['lessonid'], $result)
			|| $result[$items[$i]['lessonid']]['classid'] > $items[$i]['classid'])
			$result[$items[$i]['lessonid']] = $items[$i];

	return $result;
}

function rdv() {
	$target_array = array(
		array("lessonid" => "orb", "classid" => 27),
		array("lessonid" => 43, "classid" => 53),
		array("lessonid" => "orb", "classid" => 7),
		array("lessonid" => "orb", "classid" => 15)
	);

	echo'<h3>Before:</h3>';
	print_r($target_array);

	echo'<h3>After:</h3>';
	$input = distinct($target_array);

	print_r($input);

	/*
		Array
		(
		    [orb] => Array
		        (
		            [lessonid] => orb
		            [classid] => 7
		        )

		    [43] => Array
		        (
		            [lessonid] => 43
		            [classid] => 53
		        )

		)
	 */
}

rdv();

Now mine provides one subtle difference, it places the lessonid as the key in the result array too (that was for quicker lookup in the distinct function). If that harms you ability to use it, then you simply need to just iterate over the $result array again and move each item into a new index based array.

@cpradio ;
Thanks for that codes dude,

By the way what is this codes?,


$result = [];

I think you are trying to use javascript array asignment? lol
Or did you do that on purpose to play with me? lol

I love you codes dude, it’s short.
Anyway thanks again.

$results = [];

is a shorthand for

$results = array();

Perfectly legal PHP, it simply creating an empty array.