PHP,从(多)多维数组中获取值

时间:2013-12-12 02:18:38

标签: php arrays multidimensional-array foreach

我有一个多维数组,我试图从中使用foreach来提取值。

它检索了JSON格式,因此我使用json_decode将其转换为关联数组。

该数组包含各种产品的定价信息。我正在努力解决的问题是它为每个产品包含多个嵌套数组。

因此,我不确定如何将其整合到foreach

数组$ arr:

Array
(
    [result] => success
    [totalresults] => 3
    [products] => Array
        (
            [product] => Array
                (
                    [0] => Array
                        (
                            [pid] => 2
                            [gid] => 2
                            [type] => other
                            [name] => Shared Hosting
                            [description] => Shared Cloud
                            [module] => custom server
                            [paytype] => onetime
                            [pricing] => Array
                                (
                                    [GBP] => Array
                                        (
                                            [prefix] => £
                                            [suffix] =>  GBP
                                            [msetupfee] => 0.00
                                            [qsetupfee] => 0.00
                                            [ssetupfee] => 0.00
                                            [asetupfee] => 0.00
                                            [bsetupfee] => 0.00
                                            [tsetupfee] => 0.00
                                            [monthly] => 0.00
                                            [quarterly] => -1.00
                                            [semiannually] => -1.00
                                            [annually] => -1.00
                                            [biennially] => -1.00
                                            [triennially] => -1.00
                                        )

                                )

                            [customfields] => Array
                                (
                                    [customfield] => Array
                                        (
                                        )

                                )

                            [configoptions] => Array
                                (
                                    [configoption] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 2
                                                    [name] => Years
                                                    [type] => 2
                                                    [options] => Array
                                                        (
                                                            [option] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [id] => 2
                                                                            [name] => 1 Year
                                                                            [recurring] =>
                                                                            [pricing] => Array
                                                                                (
                                                                                    [GBP] => Array
                                                                                        (
                                                                                            [msetupfee] => 0.00
                                                                                            [qsetupfee] => 0.00
                                                                                            [ssetupfee] => 0.00
                                                                                            [asetupfee] => 0.00
                                                                                            [bsetupfee] => 0.00
                                                                                            [tsetupfee] => 0.00
                                                                                            [monthly] => 69.00
                                                                                            [quarterly] => 0.00
                                                                                            [semiannually] => 0.00
                                                                                            [annually] => 0.00
                                                                                            [biennially] => 0.00
                                                                                            [triennially] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [id] => 5
                                                                            [name] => 2 Years
                                                                            [recurring] =>
                                                                            [pricing] => Array
                                                                                (
                                                                                    [GBP] => Array
                                                                                        (
                                                                                            [msetupfee] => 0.00
                                                                                            [qsetupfee] => 0.00
                                                                                            [ssetupfee] => 0.00
                                                                                            [asetupfee] => 0.00
                                                                                            [bsetupfee] => 0.00
                                                                                            [tsetupfee] => 0.00
                                                                                            [monthly] => 138.00
                                                                                            [quarterly] => 0.00
                                                                                            [semiannually] => 0.00
                                                                                            [annually] => 0.00
                                                                                            [biennially] => 0.00
                                                                                            [triennially] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [id] => 8
                                                                            [name] => 3 Years
                                                                            [recurring] =>
                                                                            [pricing] => Array
                                                                                (
                                                                                    [GBP] => Array
                                                                                        (
                                                                                            [msetupfee] => 0.00
                                                                                            [qsetupfee] => 0.00
                                                                                            [ssetupfee] => 0.00
                                                                                            [asetupfee] => 0.00
                                                                                            [bsetupfee] => 0.00
                                                                                            [tsetupfee] => 0.00
                                                                                            [monthly] => 276.00
                                                                                            [quarterly] => 0.00
                                                                                            [semiannually] => 0.00
                                                                                            [annually] => 0.00
                                                                                            [biennially] => 0.00
                                                                                            [triennially] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                    [3] => Array
                                                                        (
                                                                            [id] => 11
                                                                            [name] => 4 Years
                                                                            [recurring] =>
                                                                            [pricing] => Array
                                                                                (
                                                                                    [GBP] => Array
                                                                                        (
                                                                                            [msetupfee] => 0.00
                                                                                            [qsetupfee] => 0.00
                                                                                            [ssetupfee] => 0.00
                                                                                            [asetupfee] => 0.00
                                                                                            [bsetupfee] => 0.00
                                                                                            [tsetupfee] => 0.00
                                                                                            [monthly] => 552.00
                                                                                            [quarterly] => 0.00
                                                                                            [semiannually] => 0.00
                                                                                            [annually] => 0.00
                                                                                            [biennially] => 0.00
                                                                                            [triennially] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                    [4] => Array
                                                                        (
                                                                            [id] => 14
                                                                            [name] => 5 Years
                                                                            [recurring] =>
                                                                            [pricing] => Array
                                                                                (
                                                                                    [GBP] => Array
                                                                                        (
                                                                                            [msetupfee] => 0.00
                                                                                            [qsetupfee] => 0.00
                                                                                            [ssetupfee] => 0.00
                                                                                            [asetupfee] => 0.00
                                                                                            [bsetupfee] => 0.00
                                                                                            [tsetupfee] => 0.00
                                                                                            [monthly] => 1104.00
                                                                                            [quarterly] => 0.00
                                                                                            [semiannually] => 0.00
                                                                                            [annually] => 0.00
                                                                                            [biennially] => 0.00
                                                                                            [triennially] => 0.00
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

对于每个产品ID([product]),[pid]重复上述部分 - 我无法将整个数组放入帖子中,因此这里是指向它的链接 - http://pastebin.com/0qgh5scG

我想要实现的是将数组中每个产品ID [pid]的名称描述和每个月*价格拉到它自己的数组中,该数组变量名称是关联的{{1} }。

*(每月价格实际上是年度价格,只是模块将数据存储在数据库中的奇怪方式)

我已尝试[pid]

foreach

产品ID,检索正常,但我如何为每个产品ID $arr = json_decode($jsondata, true); # Decode JSON String foreach ($arr['products']['product'] as $num) { $pid = $num['pid']; $yearlycosts = $arr['configoptions']['configoption']['0']['options']['option'][0]['pricing']['GBP']['monthly']; echo $pid; echo $yearlycosts; } 提取多个[name]和相关的多个[monthly]值?

我现有[pid]内是否需要foreach

最终我想将这些值传递给新数组,每个数组都以相应的foreach值命名。但我认为这对我来说是一个单独的问题/挑战。

我希望我上面有道理。我是数组的新手,在搜索相当多的例子时,没有找到任何像上面那样复杂的数组,或者有唯一命名的部分。

2 个答案:

答案 0 :(得分:1)

首先,这个$ price变量来自哪里?你最初使用$ num。

是的,只是一个嵌套的foreach循环会像这样做:

$arr = json_decode($jsondata, true); # Decode JSON String

foreach ($arr['products']['product'] as $num) {

    $pid = $num['pid'];

    echo "Product ID: ".$pid."\n";
    echo "Options: \n"

    $i = 1;

    foreach($num['configoptions']['configoption']['0']['options']['option'] as $option)
    {
        $name = $option['name'];
        $yearlycosts = $option['pricing']['GBP']['monthly'];

        echo " - Option ".$i.": ".$name." ($".$yearlycosts.")\n";

        ++$i;
    }

}

答案 1 :(得分:0)

首先,查看数组结构,'CUSTOMFIELDS'只是一个空键,它不包含'CONFIGOPTION'。

所以这个:

echo $k['CUSTOMFIELDS'][CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][NAME];
echo $k['CUSTOMFIELDS'][CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][MONTHLY];

需要看起来像这样:

echo $k[CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][NAME];
echo $k[CONFIGOPTIONS][CONFIGOPTION][OPTIONS][OPTION][MONTHLY];

我想我可能会先尝试修复索引的所有名称,然后让我的生活变得轻松。我会用这个函数遍历整个数组:

function traverseArray($array)
{ 
    // Loops through each element. If element again is array, function is recalled. If not, result is echoed.
    foreach($array as $key=>$value)
    { 
        if (is_array($value))
        { 
            traverseArray($value); 
        } else {
            if (preg_match("/[0-9]/", $key) && !isset($array[preg_replace("[0-9]", "", $key)])) 
                        $array[preg_replace("[0-9]", "", $key)] = $value;
        } 
    }
}

这个函数(在这里找到:http://snipplr.com/view/10200/并为当前问题修改它)将遍历整个数组,每个级别,并从数组键的末尾删除数字如果没有数字的键没有不存在。原因是这些数组中的一些包含多个数组,其中包含OPTION,OPTION12,OPTION43等键。如果存在同一级别中没有数字的键,则会使其保持不变。现在,它将所有数字从PRICING,GBP等密钥中移除。我们只剩下一些OPTION键,最后会有数字。然后我们就可以去了:

traverseArray($arr); // Goes through arrray and removes random numbers unless not possible

foreach($k['CONFIGOPTIONS']['CONFIGOPTION']['OPTIONS'] as $option) {

    echo $option['NAME'];
    echo $option['PRICING']['GBP']['MONTHLY'];

}

试一试。我认为这应该有效。

P.S。:他们应该在他们的结尾处找到关于那个API的东西,因为这是一个可怕的阵列。这没有理由。