N
N
Nikolay Matyushkin2018-06-26 20:15:08
PHP
Nikolay Matyushkin, 2018-06-26 20:15:08

How to loop through an array with foreach and output only unique values?

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 unique values ​​by key [PlaceObjectID], i.e. after going through it, I want to get "41132, 35777", but I get "41132, 41132, 41132, 35777, 41132". Help plz dude.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-06-26
@Yan-s

$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"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question