How to iterate the array using foreach and display only unique values?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
14 views
There is such an array

$date = Array
(
[0] => Array
(
[PlaceObjectID] => 41132
[DateTime] => 2018-06-26 21:15
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 120
)

[1] => Array
(
[PlaceObjectID] => 41132
[DateTime] => 2018-06-26 23:30
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 120
)

[2] => Array
(
[PlaceObjectID] => 41132
[DateTime] => 2018-06-27 10:30
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 100
)

[3] => Array
(
[PlaceObjectID] => 41132
[DateTime] => 2018-06-27 12:45
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 150
)

[4] => Array
(
[PlaceObjectID] => 35777
[DateTime] => 2018-06-27 19:00
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 250
)

[5] => Array
(
[PlaceObjectID] => 41132
[DateTime] => 2018-06-27 22:30
[Format] =>
[IsSaleAvailable] => 1
[IsReservationAvailable] =>
[IsWithoutSeats] =>
[MinPrice] => 250
)

I need a unique value for key [PlaceObjectID], i.e. sorting through it I want to "41132, 35777," and get "41132, 41132, 41132, 35777, 41132". Pliz help guys.
by | 14 views

2 Answers

0 like 0 dislike
$input = [ ['id' => 41132], ['id' => 41132], ['id' => 35777], ['id' => 41132], ['id' => 41132], ['id' => 35777], ]; $uniqIds = []; foreach ($input as $item) { $uniqIds[$item['id']] = null; } $uniqIds = implode(', ', array_keys($uniqIds)); var_dump($uniqIds); // string(12) "41132, 35777"
by
0 like 0 dislike
$columns = array_column($array, 'PlaceObjectID'); $result = array_values(array_combine($columns, $columns));
by

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
36,635 users