通过多维数组和打印循环 [英] Loop through Multi-Dimensional Array and Print

查看:112
本文介绍了通过多维数组和打印循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想拉某些信息和使用PHP / HTML(即游戏ID,等级)它的输出数组。我有基础知识之后,我虽然只得到从输出的第一阵列中的信息。我知道,循环正是我需要做的,虽然我不完全知道如何在同一时间返回多个记录。有些是在一个阵列中的信息可能不会在其他的,你可以看到没有[信息]中的第一个。

下面是我的code:

 < PHP$复习= $显象> API('/源');    函数D($ D){
        呼应'< pre>';
        的print_r($ D);
        呼应'< / pre>';
    }    D($检讨[数据] [0] [游戏]);
    D($检讨[数据] [0] [游戏] [评分]);
?>

这是输出:

 阵列([数据] =>数组([0] =>数组([ID] => 2010_1110 [从] =>阵列([名称] =&GT ; Pebkac [ID] => 11001010)[游戏] =>数组([ID] => 2112 [名] =>新建游戏[评分] =>数组([动作] => 9显卡] =→10))[评论] =>数组([数据] =>数组([0] =>数组([ID] => 2010_1111 [从] =>阵列([名称] = >我的朋友[ID] => 10100110)[消息] =>您好[CREATED_TIME] =方式>下午8:00)))))[分页] =>阵列([previous] =>为url1 [下一页] => URL2))阵列([数据] =>数组([1] =>数组([ID] => 2010_1112 [从] =>阵列([名称] => Pebkac [ID] => 11001010)[游戏] =>数组([ID] => 5050 [名] =>另一场比赛[评分] =>数组([动作] => 8 [显卡] => 8))[资讯] =&GT [CREATED_TIME] =>下午8时59分[拥有] =>数组([数据] =>数组([0] =>数组([ID] => 20100112 [名] =>朋友二)))[评论] =>数组([数据] =>数组([0] =>数组([ID] => 2010_1113 [从] =>阵列([名称] =>我的朋友[ID] => 10100110)[消息] =>您好[CREATED_TIME] =方式>下午9:00)))))


解决方案

根据你想要做什么,你可以做一个嵌套的循环,或者如果你拥有的维数n,您可以递归调用的函数,直到结果不再是一个数组。事情是这样的:

 函数printNode($节点){
    的foreach($节点作为$ nodeKey => $的nodeValue){
        如果(is_array($的nodeValue)){
            printNode($的nodeValue);
        }其他{
            打印$的nodeValue;
        }
    }
}

I have an array that I would like to pull certain information from and output it using php/html (i.e. game, id, rating). I have the basics down, although I only get the information from the first array in the output. I know that looping is what I need to do, although I'm not exactly sure how to return more than one record at a time. Some of the information that is in one array may not be in the other, as you can see there is no [info] in the first one.

Here's my code:

<?php

$review = $developer->api('/source');

    function d($d){
        echo '<pre>';
        print_r($d);
        echo '</pre>';
    }

    d($review[data][0][game]);
    d($review[data][0][game][rating]);
?>

This is the output:

Array( [data] => Array ( [0] => Array ( [id] => 2010_1110 [from] => Array ( [name] => Pebkac [id] => 11001010  ) [game] => Array ( [id] => 2112 [name] => New Game [rating] => Array ( [action] => 9 [graphics] => 10 ) ) [comments] => Array ( [data] => Array ( [0] => Array ( [id] => 2010_1111 [from] => Array ( [name] => My Friend  [id] => 10100110 ) [message] => hi there. [created_time] => 8:00 P.M. ) ) ) ) ) 

[paging] => Array ( [previous] => url1 [next] => url2 ))

Array( [data] => Array ( [1] => Array ( [id] => 2010_1112 [from] => Array ( [name] => Pebkac [id] => 11001010  ) [game] => Array ( [id] => 5050 [name] => Another Game [rating] => Array ( [action] => 8 [graphics] => 8 ) ) [info] => [created_time] => 8:59 P.M. [owns] => Array ( [data] => Array ( [0] => Array ( [id] => 20100112   [name] => Friend Two ) ) ) [comments] => Array ( [data] => Array ( [0] => Array ( [id] => 2010_1113 [from] => Array ( [name] => My Friend  [id] => 10100110 ) [message] => hi there. [created_time] => 9:00 P.M. ) ) ) ) ) 

解决方案

Depending on what you want to do, you can either do a nested for loop or if you have n number of dimensions, you can recursively call your function until the result is no longer an array. Something like this:

function printNode($node) {
    foreach ($node as $nodeKey => $nodeValue) {
        if (is_array($nodeValue)) {
            printNode($nodeValue);
        } else {
            print $nodeValue;
        }
    }
}

这篇关于通过多维数组和打印循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆