PHP搜索多维数组的值

时间:2022-10-28 16:11:07

I have the following array:

我有以下数组:

Array ( 
    [0] => Array ( [Country] => Americas [Out_Count] => 14 ) 
    [1] => Array ( [Country] => Belgium [Out_Count] => 2 ) 
    [2] => Array ( [Country] => China [Out_Count] => 33 ) 
    [3] => Array ( [Country] => France [Out_Count] => 7 ) 
)

I have a variable as follows:

我有一个变量如下:

$los = 'Belgium';

What I'd like to do is search the array and bring back the value of Out_Count to a variable.

我要做的是搜索数组并将Out_Count的值返回给一个变量。

I can use the following:

我可以使用以下内容:

$key = array_search($los, array_column($outs, 'Country'));

This brings back the Key, in this case 1 for Belgium but I need the Out_Count value and I'm utterly stumped on how to achieve this.

这就带来了关键,在本例中是比利时的1,但是我需要Out_Count值,我完全不知道如何实现这一点。

Any ideas and thoughts welcomed.

任何想法和想法都欢迎。

3 个解决方案

#1


4  

Nice choice of array_column()! Just extract an array with Country as the key and Out_Count as the value:

array_column()的好选择!只需提取以国家为键的数组,以Out_Count为值:

$los = 'Belgium';
$result = array_column($outs, 'Out_Count', 'Country')[$los];

To do it your way:

按照你的方式去做:

$los = 'Belgium';
$key = array_search($los, array_column($outs, 'Country'));
$result = $outs[$key]['Out_Count'];

Or:

或者:

$result = $outs[array_search($los, array_column($outs, 'Country'))]['Out_Count'];

#2


1  

Try this:

试试这个:

$array = array(
  array('Country' => 'Americas', 'Out_Count' => 14),
  array('Country' => 'Belgium', 'Out_Count' => 2),
  array('Country' => 'China', 'Out_Count' => 33),
  array('Country' => 'France', 'Out_Count' => 7)
);


function search($array, $key, $value) {
$results = array();
if (is_array($array)) {
    if (isset($array[$key]) && $array[$key] == $value) {
        $results[] = $array;
    }
    foreach ($array as $subarray) {
        $results = array_merge($results, search($subarray, $key, $value));
    }
 }
 return $results;
}

Ouput:

输出:

$Out_Count = search($array, 'Country', 'Belgium');
echo $Out_Count[0]['Out_Count'];   //print 2

$Out_Count = search($array, 'Country', 'France');
echo $Out_Count[0]['Out_Count'];  //print 7

In this way,you have the complete array that you have searched and you can access it.

这样,您就有了已搜索的完整数组,并且可以访问它。

print_r($Out_Count);

Array
(
 [0] => Array
    (
        [Country] => Belgium
        [Out_Count] => 2
    )

 )

#3


0  

You can write your custom function like this

您可以这样编写自定义函数

function my_custom_array_search($array, $search)
{
   foreach($array as $single)
   {
     if($single['Country']==$search)
     {
        return $single['Out_Count'];
     }
   }
   return '';
}

#1


4  

Nice choice of array_column()! Just extract an array with Country as the key and Out_Count as the value:

array_column()的好选择!只需提取以国家为键的数组,以Out_Count为值:

$los = 'Belgium';
$result = array_column($outs, 'Out_Count', 'Country')[$los];

To do it your way:

按照你的方式去做:

$los = 'Belgium';
$key = array_search($los, array_column($outs, 'Country'));
$result = $outs[$key]['Out_Count'];

Or:

或者:

$result = $outs[array_search($los, array_column($outs, 'Country'))]['Out_Count'];

#2


1  

Try this:

试试这个:

$array = array(
  array('Country' => 'Americas', 'Out_Count' => 14),
  array('Country' => 'Belgium', 'Out_Count' => 2),
  array('Country' => 'China', 'Out_Count' => 33),
  array('Country' => 'France', 'Out_Count' => 7)
);


function search($array, $key, $value) {
$results = array();
if (is_array($array)) {
    if (isset($array[$key]) && $array[$key] == $value) {
        $results[] = $array;
    }
    foreach ($array as $subarray) {
        $results = array_merge($results, search($subarray, $key, $value));
    }
 }
 return $results;
}

Ouput:

输出:

$Out_Count = search($array, 'Country', 'Belgium');
echo $Out_Count[0]['Out_Count'];   //print 2

$Out_Count = search($array, 'Country', 'France');
echo $Out_Count[0]['Out_Count'];  //print 7

In this way,you have the complete array that you have searched and you can access it.

这样,您就有了已搜索的完整数组,并且可以访问它。

print_r($Out_Count);

Array
(
 [0] => Array
    (
        [Country] => Belgium
        [Out_Count] => 2
    )

 )

#3


0  

You can write your custom function like this

您可以这样编写自定义函数

function my_custom_array_search($array, $search)
{
   foreach($array as $single)
   {
     if($single['Country']==$search)
     {
        return $single['Out_Count'];
     }
   }
   return '';
}