dtm37893 2015-02-05 22:00
浏览 87

注意:数组到字符串转换PHP

I am finding the intersection between two arrays $item and $query respectively:

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

Array ( [0] => dig [1] => twitter )

This is the code I have:

if (array_intersect ( $query, $item )) {
            $intersection [] = $item;
}

Somehow it's returning the notice as defined on the title of this question. Either I'm too tired to notice what's wrong or I may be going mad, shouldn't it return Array ( [0] => twitter )?

  • 写回答

1条回答 默认 最新

  • douyao2529 2015-02-05 22:28
    关注

    This is because you have an empty array at the end of your first array and array_intersect() is going to try to convert it to a string which gives you this error.

    But to get rid of this error you can use array_filter() like this:

    (Also you want to assign the output of array_intersect and then use this)

    <?php       
    
        $item = array("twitter", 1, 561522539340771328, array());
        $query = array("dig", "twitter");
    
        if ($intersect = array_intersect($query, array_filter($item))) {
                                               //^^^^^^^^^^^^ See here
            $intersection [] = $intersect;
        }
    
        print_r($intersection);
    
    ?>
    

    Output:

    Array ( [0] => Array ( [1] => twitter ) )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)