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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站