doulu2011 2018-05-03 03:16
浏览 40
已采纳

组合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'
);
  • 写回答

1条回答 默认 最新

  • doutangu4671 2018-05-03 03:45
    关注

    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);
    

    https://3v4l.org/j44qh

    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);
    

    https://3v4l.org/2tMiX

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?