I have this code below which will get the array object.
// fetch all circuits
$circuits = $Tools->fetch_all_circuits();
// check locations
if($circuits!==false) {
// all locations
$all_locations = array ();
// loop
foreach ($circuits as $circuit) {
// format points
$locationA = $Tools->reformat_circuit_location ($circuit->device1, $circuit->location1);
$locationB = $Tools->reformat_circuit_location ($circuit->device2, $circuit->location2);
if($locationA['location']!="" && $locationB['location']!="") {
$locA = $Tools->fetch_object ("locations", "id", $locationA['location']);
$locB = $Tools->fetch_object ("locations", "id", $locationB['location']);
// save to all_locations array
if ($locA!==false && $locB!==false) {
$all_locations[] = $locA;
$all_locations[] = $locB;
}
}
}
}
var_dump ($all_locations);
How can i get the unique object from this array?
i var_dump($all_locations), i have this array output (i deleted some object, to lessen the output).
array(28) { [0]=> object(stdClass)#51 (6) { ["id"]=> string(3) "950"
["name"]=> string(4) "5829" ["description"]=> string(4) "Null"
["address"]=> string(4) "Null" ["lat"]=> string(7) "21.5336"
["long"]=> string(7) "39.2178" } [1]=> object(stdClass)#52 (6) {
["id"]=> string(3) "987" ["name"]=> string(4) "5765" ["description"]=>
string(4) "Null" ["address"]=> string(4) "Null" ["lat"]=> string(7)
"21.5439" ["long"]=> string(7) "39.2243" } [2]=> object(stdClass)#54
(6) { ["id"]=> string(4) "1016" ["name"]=> string(4) "2228"
["description"]=> string(4) "Null" ["address"]=> string(4) "Null"
["lat"]=> string(7) "21.5447" ["long"]=> string(7) "39.2301" } [27]=>
object(stdClass)#84 (6) { ["id"]=> string(4) "2536" ["name"]=>
string(4) "2389" ["description"]=> string(4) "Null" ["address"]=>
string(4) "Null" ["lat"]=> string(6) "21.495" ["long"]=> string(6)
"39.966" } }
array_unique() will help you in that. http://php.net/manual/en/function.array-unique.php
array_unique() sorts the values treated as a string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.