通过阵列迭代,并得到键和值 [英] Iterate through array and get key and value

查看:146
本文介绍了通过阵列迭代,并得到键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过一个动态数组进行迭代。该阵列将类似于以下内容:

I need to iterate through a dynamic array. The array will look something like the following:

Array
(
    [2010091907] => Array
        (
            [home] => Array
            (
                [score] => Array
                (
                        [1] => 7
                        [2] => 17
                        [3] => 10
                        [4] => 7
                        [5] => 0
                        [T] => 41
                    )

                [abbr] => ATL
                [to] => 2
            )

        [away] => Array
            (
                [score] => Array
                    (
                        [1] => 0
                        [2] => 7
                        [3] => 0
                        [4] => 0
                        [5] => 0
                        [T] => 7
                    )

                [abbr] => ARZ
                [to] => 2
            )

        [weather] => 
        [media] => Array
            (
                [tv] => FOX
                [sat] => 709
                [sathd] => 709
                [radio] => Array
                    (
                        [home] => 153
                        [away] => 90
                    )

            )

        [bp] => 13
        [yl] => 
        [qtr] => Final
        [down] => 0
        [togo] => 0
        [clock] => 00:26
        [posteam] => ARZ
        [note] => 
        [redzone] => 
        [stadium] => Georgia Dome
    )

我需要它是动态的,出于测试目的,我需要能够通过调用它:

I need it to be dynamic and for testing purposes, I need to be able to call it via:

回声键:$关键;价值:$值< BR /> \\ n;

我打算以后利用这个信息,并把它变成一个MySQL数据库,但现在,我需要刷上阵列,并找出如何格式化数据。

I'm going to later take this information and place it into a mysql database, but for now, I need to brush up on arrays and figure out how to format the data.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

我倒是去一个递归函数:如果一个值是一个数组,再次否则称它为显示键/值对

I´d go for a recursive function: If a value is an array, call it again and otherwise display the key / value pair.

喜欢的东西(未测试):

Something like (not tested):

function display_array($your_array)
{
    foreach ($your_array as $key => $value)
    {
        if is_array($value)
        {
            display_array($value);
        }
        else
        {
             echo "Key: $key; Value: $value<br />\n";
        }
    }
}

display_array($some_array);

这篇关于通过阵列迭代,并得到键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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