组合2个数组并在数组PHP中允许相同的值
问题描述:
I have this array:
$title = array('new year', 'cuti bersama', 'cuti bersama');
$date = array('2018-1-1', '2018-1-10', '2018-2-13');
I combined them into
$title_date = array_combine($title, $date);
What i get is only 1 cuti bersama
, it has same title but different date.
how to allow same title in combined array?
My expected output is like this:
array(
[new year] => '2018-1-1'
[cuti bersama] => '2018-1-10'
[cuti bersama] => '2018-2-13'
);
答
Since the date is unique you can have it the other way around.
$title = array('new year', 'cuti bersama', 'cuti bersama');
$date = array('2018-1-1', '2018-1-10', '2018-2-13');
$title_date = array_combine($date,$title);
Var_dump($title_date);
And you can use array_filter to find all cuti bersama.
$searchFor = 'cuti bersama';
$filteredArray = array_filter($title_date, function($item) use($searchFor){
return $item == $searchFor;
});
Var_dump($filteredArray);