doujiao2014 2019-04-20 07:07
浏览 152
已采纳

确定数组中的空值并获取键数组

I had some problem when to post array data:

When I post the data it will be 2 array:

Array(1)[
[0]=>1
[1]=>2
[2]=>3
]

Array(2)[
[0]=>1
[1]=>
[2]=>3
]

Note that Array2 will be some blank data.

Now I am working until using array_filter(Array2) but the question is can i get the key /data from Array1 match to the not null data from Array2?

Maybe it will be some confusing on question, Sorry to my bad language...

  • 写回答

3条回答 默认 最新

  • dsafq2131321 2019-04-20 07:18
    关注

    If you want to filter $array1 based on the value of $array2, you can use filter with third parameter ARRAY_FILTER_USE_KEY to use the key.

    $array1 = [1,2,3];
    $array2 = [1,null,500];
    
    $newArray1 = array_filter($array1, function ($key) use ($array2) {
            return $array2[$key];
        },ARRAY_FILTER_USE_KEY
    );
    
    $newArray2 = array_filter($array2);
    
    echo "<pre>";
    print_r( $newArray1 );
    print_r( $newArray2 );
    echo "</pre>";
    

    This will result as:

    Array
    (
        [0] => 1
        [2] => 3
    )
    Array
    (
        [0] => 1
        [2] => 500
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等