在数组中搜索返回错误的结果

时间:2017-01-19 14:53:40

标签: php arrays

我收到了这样的数组

Array
(
    [hash] => 9761d3233f9cb256c0992be

    [total] => 2736712601    
    [received] => 2017-01-13T21:43:32.047Z
    [income] => Array
    (
        [0] => Array
            (
                [value] => 647262
                [addresses] => Array
                    (
                        [0] => Address_1
                    )
            )

        [1] => Array
            (
                [value] => 17200000
                [addresses] => Array
                    (
                        [0] => Address_2
                    )
            )

        [2] => Array
            (
                [value] => 3729034
                [addresses] => Array
                    (
                        [0] => Address_3
                    )
            )

        [3] => Array
            (
                [value] => 2414997500
                [addresses] => Array
                    (
                        [0] => Address_4
                    )
            )

        [4] => Array
            (
                [value] => 10856454
                [addresses] => Array
                    (
                        [0] => Address_5
                    )
            )
     )
)

所以在我的数据库中我存储了哈希(9761d3233f9cb256c0992be)。我哈希和地址。然后基于它们我匹配[income]中的正确数组。当我在income中找到正确的地址时,我会使用[value]并在页面上显示该地址。这是我如何做到的

$url=get_curl_content("https://example.com/".$order->hash);

$totala =json_decode($url,true);

....
$match = true;
foreach ($totala['income'] as $data) {
    if ($data['addresses'] == $order->address) {
            $match = $data;
            break;
    }
}                     
$price = $data['value'];

问题在于我期待Address_2,因为在数据库中我保存了Address_2我已经Address_3了。

当我var_dump($data['addresses'])获得Address_3时。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

试试这样。它将使用in_array函数

在数组中进行搜索和匹配
$match = true;

foreach ($totala['income'] as $data) {

    if (in_array($order->address, $data['addresses'])) {
            $match = $data;
            break;
    }                         

}                     
$price = $match['value'];