循环处理多维PHP数组并显示所有对应的数组

时间:2019-04-01 20:41:26

标签: php associative-array

我在此主题[链接文本] Loop Multidimensional PHP arrays

上发现了几乎类似的问题

我的数组有些不同,但几乎相似:

> Array  
>      (    
>         [size] => int (995)
>         [data] => Array
>           (
>               [0] => Array
>                     (
>                       [service] => 8000
>                       [network] => xxx.xxx.xxx
>                     )
> 
>                [1] => Array
>                       (
>                           [service] => 9000
>                           [network] => xxx.xxx.xxx
>                       )
> 
>                [2] => Array
>                       (
>                            [service] => 9500
>                            [network] => xxx.xxx.xxx
>                       )
>              ) 
       )

我想检查所有[service]值,以查看用户输入的号码是否有效并存在,并显示相应的[network]

这是我天真的尝试:

$record = NULL;

// let's assume $x as this array here
foreach($record in $x['data']){
    if($record['service'] == $bus){
        break;
    }
}

if($record){
    // record found
    var_dump($record);
}else{
    echo "Not found";
}

5 个答案:

答案 0 :(得分:1)

只是为了好玩,假设service是唯一的:

$services = array_column($x['data'], null, 'service');

if(isset($services[$bus]) {
    echo $services[$bus]['network'];
} else {
    echo "Not found";
}
  • 提取数组并通过service对其进行索引
  • 现在您可以通过$services[$bus]来访问它了。 $services[8000]['network']

答案 1 :(得分:0)

最右边:

forEach语法则相反:forEach(<array> as $iterator)  哦,您需要处理对$ reslt变量(由于某种原因我将其重命名为$ found)的赋值有点不同

 $found=NULL;
    // let's assume $x as this array here
    foreach($x['data'] as $record ){
        if($record['service'] == $bus){
            $found=$record['service'];
        }
    }

    if($found != NULL){
        // record found
        var_dump($record);
    }else{
        echo "Not found";
    }

答案 2 :(得分:0)

如果我正确回答了您的问题,应该是这样的:

// For the sake of the example, first I reconstructed your array:
$ar1 = array("service" => 8000, "network" => "111.111.111");
$ar2 = array("service" => 9000, "network" => "222.222.222");
$ar3 = array("service" => 9500, "network" => "333.333.333");
$x = array("size" => 995,
        "data" => array($ar1,$ar2,$ar3));

$record = NULL;
$bus = 9000;

for($n = 0; $n < count($x["data"]); $n++){
    $checkService = $x["data"][$n];
    if($checkService["service"] == $bus){
        $record = $checkService["network"];
    }
}

if ($record) {
    // If record found:
    echo "Lookup Results for ".$bus.": ".$record;
    // Since we are searching for 9000 in this example, this should output -> Lookup Results for 9000: 222.222.222
} else {
    echo "Record not found";
}

此代码可以进一步简化,但我不确定您到底需要什么。

您可以通过this example检查最终结果。

我希望这会有所帮助。干杯!

答案 3 :(得分:0)

添加到Johnathon Heindl上:(对不起,我无法回复)。

在发现同一服务具有不同网络的情况下,使$ found为数组可能很有用。

    var combinedOrder = new[]
    {
        new Tuple<Expression<Func<Player, object>>, bool>(p => p.GenderTypeId, false),
        new Tuple<Expression<Func<Player, object>>, bool>(p => p.PlayingExperience, true),
        new Tuple<Expression<Func<Player, object>>, bool>(p => p.FirstName, false),
    };

答案 4 :(得分:-2)

$found_record = NULL;
// let's assume $x as this array here
foreach($record in $x['data']){
if($record['service'] == $bus){
     $found_record = $record['service'];
      break;
    }
  }
 if($found_record){
   // record found
   var_dump($record);
  }else{
    echo "Not found";
}