为什么这些数组不匹配?

时间:2014-07-23 10:15:38

标签: php arrays laravel-4 phpunit

我正在尝试匹配数组元素,我意识到即使数组中的元素完全相同,它们也不会相互匹配。

第一个阵列:

  $json = '{ 
                "address1": "1818 Grove View", 
                "address2": "605", 
                "country": "USA", 
                "city": "San Diego", 
                "postcode": "91913",  
                "contact_name": "Natasha", 
                "contact_email": "nsr@yahoo.com", 
                "contact_number": "6198883849" 
             }';


    $data = json_decode($json, true);

    $address1 = $data['address1'];
    $address2 = $data['address2'];
    $country = $data['country'];
    $city = $data['city'];
    $postcode = $data['postcode'];
    $contactName = $data['contact_name'];
    $contactEmail = $data['contact_email'];
    $contactNumber = $data['contact_number'];

    $dataArray[] = array($postcode, $address1, $address2, $city, $country,$contactName, $contactEmail, $contactNumber);

第二阵列:

    $this->call('POST', 'address', array(), array(), array(), $json);

    $this->assertFalse($this->client->getResponse()->isOk());

    $decodedOutput = json_decode($this->client->getResponse()->getContent());

    $arrayOutputs[]= array();

    foreach($decodedOutput as $dOpt){
            $arrayOutputs[] = array($dOpt);
    }

为了解释上面的代码,我正在使用Laravel,我正在进行单元测试。我正在调用传递$json字符串的地址URI的POST路由,$decodedOutput接收内容并对其进行解码。我将$decodedOutput中的所有元素放入一个名为$arrayOutputs

的数组中

如果我var_dump两个数组,这是我得到的: 第一阵列:      .array(8){        [0] =>        string(6)“91913”        [1] =>        string(19)“1818 Grove View”        [2] =>        string(25)“605”        [3] =>        string(9)“USA”        [4] =>        string(9)“圣地亚哥”        [5] =>        字符串(9)“娜塔莎”        [6] =>        string(19)“nsr@yahoo.com”        [7] =>        string(8)“6138883849”      }

第二阵列:           array(10){        [0] =>        array(0){        }        [1] =>        array(1){          [0] =>          INT(74)        }        [2] =>        array(1){          [0] =>          string(6)“91913”        }        [3] =>        array(1){          [0] =>          string(6)“1818 Grove View”        }        [4] =>        array(1){          [0] =>          string(25)“605”        }        [5] =>        array(1){          [0] =>          string(9)“USA”        }        [6] =>        array(1){          [0] =>          string(9)“圣地亚哥”        }        [7] =>        array(1){          [0] =>          字符串(9)“娜塔莎”        }        [8] =>        array(1){          [0] =>          string(19)“nsr@yahoo.com”        }        [9] =>        array(1){          [0] =>          string(8)“6198883849”        }      }

现在显然看起来在格式化方面存在一些差异,但它们仍然是数组。所以我接下来要做的就是检查它们是否真的不同:

 if($arrayOutputs[2] === $dataArray[0])
    {
        echo "Match";
    }
    else
    {
        echo "Doesn't Match";
    }

我从每个数组中获取了两个元素,我知道它们具有相同的元素,当我运行代码时,我总是得到它们不匹配的元素。他们为什么不同?

2 个答案:

答案 0 :(得分:2)

构建第二个阵列时,请替换

foreach($decodedOutput as $dOpt){
    $arrayOutputs[] = array($dOpt);
}

foreach($decodedOutput as $dOpt){
    $arrayOutputs[] = $dOpt;
}

答案 1 :(得分:1)

$arrayOutputs[2]是一个数组本身

if($arrayOutputs[2][0] === $dataArray[0])
                   ^^^ add this
相关问题