如何在php中的二维数组中按值搜索索引

时间:2015-09-30 06:12:58

标签: php

我有一个函数来获取courier的描述,所有关于快递的记录都存储在$couriers中。 $ courier是二维数组,因为它包含所有行的表信使。

[
{
"id":"1",
"name":"DTDC",
"description":"Automatically inserted by application ",
"bloked":"false"
},
{
"id":"2",
"name":"Ecomm",
"description":"Nothing",
"bloked":"false"
},
{
"id":"3",
"name":"MarginPrice",
"description":"Local only",
"bloked":"false"
}
]
  

现在,我必须取得对其身份证明的快递的解释。对于   这个目的,我必须知道记录的索引..我试过了   使用array_search但“困难时期”。所以,我请求帮助提出想法   知道数组中记录的索引

 function getCourierDescriptionById($id)
    {   global $couriers;
        if($couriers==null)
        {
            loadCourier($id);
        }
        $index=array_search($id,$couriers);// Here is the problem
        return isset($couriers[$index]['description'])?
                                            $couriers[$index]['description']:null;      
    }

3 个答案:

答案 0 :(得分:1)

  

尝试这种方式易于使用

$j = '[
    {
    "id":"1",
    "name":"DTDC",
    "description":"Automatically inserted by application ",
    "bloked":"false"
    },
    {
    "id":"2",
    "name":"Ecomm",
    "description":"Nothing",
    "bloked":"false"
    },
    {
    "id":"3",
    "name":"MarginPrice",
    "description":"Local only",
    "bloked":"false"
    }
    ]';
    $arr = json_decode($j,true);
    $courier =array();
    foreach($arr as $sample){
        $courier[$sample['id']] = $sample;
    }
    //make sure your courier variable has id which you want to find "Its easy to search"

    function getCourierDescriptionById($id)
        {   global $courier;

            return isset($courier[$id])? $courier[$id]:null;      
        }
        print_r(getCourierDescriptionById(3));//function call which want to find

你的$ courier变量有易于查找的变化,其中包含类似的数组:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => DTDC
            [description] => Automatically inserted by application 
            [bloked] => false
        )

    [2] => Array
        (
            [id] => 2
            [name] => Ecomm
            [description] => Nothing
            [bloked] => false
        )

    [3] => Array
        (
            [id] => 3
            [name] => MarginPrice
            [description] => Local only
            [bloked] => false
        )

)

答案 1 :(得分:0)

使用此

$jsonArray = '[
    {
    "id":"1",
    "name":"DTDC",
    "description":"Automatically inserted by application ",
    "bloked":"false"
    },
    {
    "id":"2",
    "name":"Ecomm",
    "description":"Nothing",
    "bloked":"false"
    },
    {
    "id":"3",
    "name":"MarginPrice",
    "description":"Local only",
    "bloked":"false"
    }
    ]';
    $arrData = json_decode($jsonArray,true);
    print_r(searchForId('3',$arrData,'id'));

function searchForId($id, $array,$field) {
   foreach ($array as $key => $val) {
       if ($val[$field] === $id) {
           return $val['description'];
       }
   }
   return null;
}

答案 2 :(得分:0)

你$ courier变量不是一个数组,它是一个json字符串,所以首先对其进行dcode,然后将其转换为php数组。

$couriers = json_decode($couriers,true);

现在,您的功能可以使用array_map

定义如下
 function getCourierDescriptionById($id)
    {   global $couriers;
        $args=func_num_args();
        if($couriers==null)
        {
            loadCourier($id);
        }
        $index=array_search($id,array_map("getAllCourierId",$couriers));
        return isset($couriers[$index]['description'])?
                   $couriers[$index]['description']:null;       
    }

现在使用array_map绑定的函数如下所示

 function getAllCourierId($arr)
        {
            return $arr['id'];
        }