如何访问多维数组中的元素?

时间:2015-10-25 19:55:20

标签: php arrays multidimensional-array

我可能会遗漏一些显而易见的东西,但希望在多维数组中访问一个元素。

当我执行以下操作时

public void merg(Integer[] arr,int l,int h){
    Integer[] tarr = new Integer[h-l+1];
    for(int p=0;p<tarr.length;p++) // corrected the range of the loop
        tarr[p]=0;
    int m=(h+l)/2; // the same fix of m calculation as before
    ...

这是我的阵列。如何访问$rows = get_field('lineup_days_and_stages'); print_r($rows); &gt;中的值? stage_headliner,因此在此示例中为post_name

我尝试了以下和一些变体,但无处速度

Slipknot

数组

foreach($rows as $value){
     if (isset($value["stage_headliner"]){
            echo $value["stage_headliner"][0]->post_name;
     }
}

2 个答案:

答案 0 :(得分:1)

您可以直接在多维数组中访问post_name。

在您的情况下,您必须执行以下操作才能获得stage_headliner&gt; post_name($ array是你的多维数组):

echo $array[0]["stage_headliner"][0]->post_name;

这应该打印输出:slipknot

希望这会有所帮助!!

答案 1 :(得分:1)

要访问您的价值,您需要:

echo $array[0]["stage_headliner"][0]->post_name;

如果你有多个 stage_headliners (正如你在评论中提到的那样),你可以这样做:

for($i = 0; $i < count($array[0]["stage_headliner"]); $i++) {
  echo $array[0]["stage_headliner"][$i]->post_name;
}

如果阵列中有多个元素,则可以执行以下操作:

for($i = 0; $i < count($array); $i++) {
  echo $array[$i]["stage_headliner"][0]->post_name;
}
相关问题