duandangqin0559 2018-02-09 17:08
浏览 13
已采纳

PHP搜索多重阵列功能始终给出TRUE值

I am a newbie about this php stuff. The function below always returns true. Can you explain how this happens? My purpose was to write a general code for searching in a multiarray however I came across this problem and wondered how this can be. Kind Regards.

<?php

    function existInMultiArray ($demand, $multiarray)
    {
        foreach ($multiarray as $key => $val)
        {
            foreach($val as $key1 => $val1)
            {
                foreach($val1 as $key2 => $val2)
                {
                    if ($key == $demand)
                    {
                        return TRUE;
                    }
                    if($key1 == $demand)
                    {
                        return TRUE;
                    }
                    if ($key2 == $demand)
                    {
                        return TRUE;
                    }
                }   
            }
        }
    return FALSE;
    }


    $demand = 'asdf';
    $multiarray = array
(
    0 => array ('A' => array( 'B' => 'C')),

    1 => array ('D' => array ('E' => 'F')),

    2 => array ('G' => array ('H' => 'J')),

    3 => array ('K' => array ('L' => 'M')),

    4 => array ('N' => array ('O' => 'P')),

    5 => array ('R' => array ('S' => 'T'))
);

var_dump(existInMultiArray($demand, $multiarray)); exit;
  • 写回答

1条回答 默认 最新

  • douyuan1049 2018-02-09 17:19
    关注

    This is because $key, $key1, $key2 have integer datatype and $demand have string datatype. If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

    In PHP, the empty string, and the string "0" will both be equal to zero with the equality operator (==).

    and in your case, you use === with strict type checking. It means it will check data type as well as value.

    <?php
    $demand = 'asdf';
    $multiarray = array
        (
        0 => array ('A' => array( 'B' => 'C')),
        1 => array ('D' => array ('E' => 'F')),
        2 => array ('G' => array ('H' => 'J')),
        3 => array ('K' => array ('L' => 'M')),
        4 => array ('N' => array ('O' => 'P')),
        5 => array ('R' => array ('S' => 'T'))
    );
    
    function existInMultiArray ($demand, $multiarray)
    {
        foreach ($multiarray as $key => $val)
        {
            foreach($val as $key1 => $val1)
            {
                foreach($val1 as $key2 => $val2)
                {
                    if ($key === $demand)
                    {
                        return TRUE;
                    }
                    if($key1 === $demand)
                    {
                        return TRUE;
                    }
                    if ($key2 === $demand)
                    {
                        return TRUE;
                    }
                }   
            }
        }
        return FALSE;
    }
    
    var_dump(existInMultiArray($demand, $multiarray)); exit;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?