在不知道索引的情况下循环遍历foreach

时间:2017-11-17 17:27:30

标签: php

我想循环<% ArrayList<Blogs> blogs = (ArrayList<Blogs>) request.getAttribute("blogList"); for (Blogs blog : blogs) { %> <br> <h2> <%=blog.getTitle()%> </h2> <p> <%=blog.getContent()%> </p> <b> Written at : <%=blog.getPost_data().toString()%> </b> <br> <!-- <a href="/delete?id=">How to pass the id to link?</a> --> <hr> <% } %> ,但我不知道索引名称。它们是随机出现的,例如我得到services8,但我不知道。

9

3 个答案:

答案 0 :(得分:1)

假设你有json存储在$ json变量中。

$json = json_decode($json);
foreach($json as $entry) {
 foreach($entry['services'] as $services) {
   //$services['name']
   //and other data here
 }
}

使用foreach时不需要知道索引,但可以从中获取索引。

答案 1 :(得分:1)

从@Alive到Die回答,我做了一些更改,我认为无论索引如何,这段代码都会在你的服务中循环。

 $array = json_decode($json, true);
foreach ($array as $values) {
    foreach ($values as $keys => $value) {
        if (is_array($value)) {
            foreach ($value as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        echo $k . ":" . $v . "\n";
                    }
                }
            }
        }
    }
}

答案 2 :(得分:0)

以下是四个可用选项:

<?php
$arr = ["2" => [
          ["first_name"=> "khalfan",
           "last_name"=> "mussa",
           "date"=>"2017-06-06 09:21:36",
           "gender"=> "male"], 
          ["services" => 
             ["8" => ["name" => "See a Doctor","results"=> ""],
              "9" => ["name"=> "Kichocho","results"=> "FD- 73"]
             ]
          ]
]];

for ($i=0, $max=count($arr["2"]); $i < $max; $i++) {
      if ( isset( $arr["2"][$i]["services"])) {
        $a = $arr["2"][$i]["services"];
        foreach($a as $e) {
           echo $e["name"],"\t";
           echo $e["results"],"\n";
        }
      }
      continue;

}

请参阅live code

这里的优点是代码可以根据OP的请求使用foreach。但是代码是涉及的,并没有尽可能快,因为如果有条件的话。

另一种更快的解决方案:

<?php
$arr = ["2" => [
          ["first_name"=> "khalfan",
           "last_name"=> "mussa",
           "date"=>"2017-06-06 09:21:36",
           "gender"=> "male"], 
          ["services" => 
             ["8" => ["name" => "See a Doctor","results"=> ""],
              "9" => ["name"=> "Kichocho","results"=> "FD- 73"]
             ]
          ]
]];
$count = count($arr["2"]);
$last = $count - 1; // b/c of zero-based indexing

foreach ($arr as $e) {
      foreach($e[$last]["services"] as $a) {
           echo $a["name"],"\t";
           echo $a["results"],"\n";
      }
}

// Or an easier solution without resorting to foreach:
array_walk_recursive($arr,function($item,$key) {
          if ($key == "name") echo $item,"\t";
          if ($key == "results") echo $item,"\n";
});

请参阅live code

$ arr [“2”]是否包含两个或更多元素,只要最后一个是“services”关联数组,这个foreach代码就可以工作。但是,这种类型的“数组行走”最好使用为此目的而设计的内置函数array_walk_recursive来执行。有了这个功能,您不必担心如何构建完美的foreach;迭代发生在幕后,你需要的只是提供一个回调。 array_walk_recursive将向下钻取到“services”元素,如果有一个或多个具有“name”和“results”键的关联数组,则显示它们各自的值。

第四种可能性是“为什么你会”的情况之一。为什么需要一个数组和json_encode然后json_decode它回到一个数组然后应用array_walk_recursive?但是,代码确实有效:

<?php
$arr = ["2" => [
          ["first_name"=> "khalfan",
           "last_name"=> "mussa",
           "date"=>"2017-06-06 09:21:36",
           "gender"=> "male"], 
          ["services" => 
             ["8" => ["name" => "See a Doctor","results"=> ""],
              "9" => ["name"=> "Kichocho","results"=> "FD- 73"]
             ]
          ]
]];
$result=json_decode(json_encode($arr),true);
array_walk_recursive($result, function($value,$key){
   if ($key == "name" ) { echo $value,"\t";}
   if ($key == "results") { echo $value,"\n";} 
});

请参阅live code

相关问题