搜索多维数组

时间:2015-06-16 04:59:55

标签: php arrays multidimensional-array

不确定这是否正确,但我不得不问......

假设我的一位用户居住在美国,他的country_id是1.我可以根据用户ship_cost获得country_id吗?

Array
(
    [0] => Array
        (
            [ship_cost] => 1
            [country_id] => 1
            [country_code] => US
            [country_name] => United States
        )

    [1] => Array
        (
            [ship_cost] => 0
            [country_id] => 0
            [country_code] => 
            [country_name] => 
        )

)

我需要的html还包括SHIP PRICE

<?php if( in_array($user_country_id, $shipArr) && $itemData[0]['user_id'] != $user_id ) { ?>
    <ul class="list-group margin-top-3">
        <li class="list-group-item">
            <span class="badge">SHIP PRICE</span>
            Ships to <?php echo $itemTools->getLocationName($user_country_id); ?>
        </li>
    </ul>
<?php } ?>

3 个答案:

答案 0 :(得分:1)

试试这个会帮助你

<?php 
        $user_country_id = 1;
            $array = array(array('ship_cost' => 1,'country_id' => 1,'country_code' => 'US','country_name' => 'United States'),
                array('ship_cost' => 0,'country_id' => 0,'country_code' => 'Uk','country_name' => 'United Kingdom'));
            foreach ($array as $value) {
                if ($user_country_id == $value['country_id']) {
                    var_dump($value);
                }
            }

根据您的期望更改此代码

答案 1 :(得分:1)

尝试

$key = array_search(1, array_column($array,"country_id"));
if($key !== false)
{
    print "ship_cost : ".$array[$key]['ship_cost'];
}else
{
    print "ship_cost : unknown or not exists";
}

样本测试

[akshay@localhost tmp]$ cat test.php
<?php

function test($id, $array)
{
    $key = array_search($id, array_column($array,"country_id"));
    if($key !== false)
    {
        print "ship_cost : ".$array[$key]['ship_cost']."\n";
    }else
    {
        print "ship_cost : unknown or not exists\n";
    }
}

$array = array (
  array (
    'ship_cost' => '1',
    'country_id' => '1',
    'country_code' => 'US',
    'country_name' => 'United States',
  ),
  array (
    'ship_cost' => '0',
    'country_id' => '0',
    'country_code' => '',
    'country_name' => false,
  )
);

// Test1
test(1, $array);

// Test2
test(2, $array);
?>

<强>输出

[akshay@localhost tmp]$ php test.php
ship_cost : 1
ship_cost : unknown or not exists

答案 2 :(得分:0)

  

在此处查看新的更改

x <- "/Date(1391514600000)/"
x <- as.numeric(gsub("[^0-9]", "", x))
x
# [1] 1391514600000

# from milliseconds to seconds:
x <- x / 1000
as.POSIXct(x, origin="1970-01-01", tz="GMT")
# [1] "2014-02-04 11:50:00 GMT"

现在处理你的数组

$arr = array
    (
        "0" => array
            (
                "ship_cost" => 1,
                "country_id" => 1,
                "country_code" => "US",
                "country_name" => "United States",
            ),

        "1" => array
            (
                "ship_cost" => 0,
                "country_id" => 0,
                "country_code" => "",
                "country_name" => "",
            )
    );