简体   繁体   English

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

[英]PHP, Get values from (Mutli) Multidimensional Array

I have a multi dimensional array from which I'm trying to pull values from using foreach . 我有一个多维数组,试图从中使用foreach提取值。

It's retrieved JSON format, so I've used json_decode to convert it to an associative array. 它检索为JSON格式,因此我使用json_decode将其转换为关联数组。

The array contains pricing information for various products. 该数组包含各种产品的价格信息。 The problem I'm struggling with is that it contains multiple nested arrays for each product. 我正在努力解决的问题是,每个产品都包含多个嵌套数组。

Because of this, I'm not sure how I integrate this into foreach . 因此,我不确定如何将其集成到foreach

Array $arr: 数组$ 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
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

The above section is repeated from [product] for each product ID ( [pid] ) - I couldn't fit the whole array in the post, so here's a link to it - http://pastebin.com/0qgh5scG 对于每个产品ID( [pid] ),从[product]重复了以上部分-我无法在帖子中容纳整个数组,因此这里是指向该产品的链接-http://pastebin.com/0qgh5scG

What I want to achieve is pulling the name description, and EACH Monthly* price for each product ID [pid] in the array, into an array of it's own with that arrays variable name being the associated [pid] . 我要实现的是将数组中每个产品ID [pid]的名称描述和每个月的价格*放入其自己的数组中,并将该数组变量名关联为[pid]

*(The monthly price is actually an annual price, it's just a weird manner in which the module stores the data in the database) *(每月价格实际上是每年的价格,这只是模块将数据存储在数据库中的一种怪异方式)

I've experimented with foreach : 我已经尝试过foreach

$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;
 }

The product ID, retrieves ok, but how on earth do I pull the multiple [name] and related multiple [monthly] values for each product ID [pid] ? 产品ID可以正常获取,但是究竟如何为每个产品ID [pid]提取多个[name]和相关的多个[monthly]值?

Do I require a foreach within in my existing foreach ? 我现有的foreach内是否需要一个foreach

Eventually I want to pass these values to new arrays, with each array named after it's corresponding [pid] value. 最终,我想将这些值传递给新数组,每个数组都以其对应的[pid]值命名。 But I think that's a separate question/challenge for me. 但是我认为这对我来说是一个单独的问题/挑战。

I hope I've made sense above. 我希望以上已讲得通。 I'm new to arrays, and in searching through quite a few examples didn't find any that had an array as complex as the one above, or that had sections that are uniquely named. 我是数组的新手,在搜索大量示例时,没有发现具有与上述数组一样复杂的数组或具有唯一命名的节的数组。

First off, where did this $pricing variable come from? 首先,这个$ pricing变量是从哪里来的? You were using $num at first. 您最初使用的是$ num。

And yes, just a nested foreach loop will do the trick like so: 是的,只是一个嵌套的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;
    }

}

First off, looking at the array structure, 'CUSTOMFIELDS' is just an empty key, it doesn't contain 'CONFIGOPTION'. 首先,看一下数组结构,“ CUSTOMFIELDS”只是一个空键,它不包含“ CONFIGOPTION”。

So this: 所以这:

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

Needs to look like this: 需要看起来像这样:

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

I think I'd probably try to first fix all the names of the indexes and make my life easy from there. 我想我可能会尝试首先修复所有索引名称,然后从那里使我的生活变得轻松。 I would traverse the whole array with this function: 我将使用此函数遍历整个数组:

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;
        } 
    }
}

This function (Found it here: http://snipplr.com/view/10200/ and modified it for the current question) will go through the whole array, each level of it, and remove numbers from end of array keys IF the key without the number doesn't exist. 此函数(在此处找到: http//snipplr.com/view/10200/ ,并针对当前问题进行了修改)将遍历整个数组,包括每个级别,并且如果该键从数组末尾删除数字没有数字不存在。 Reason for that is some of those arrays contain multiple arrays with keys like OPTION, OPTION12, OPTION43, etc. If the key without the number within the same level exists, it will leave it alone. 原因是其中的某些数组包含多个带有键的数组,如OPTION,OPTION12,OPTION43等。如果存在同一级别中没有数字的键,则将其保留。 Now that takes all the numbers away from keys like PRICING, GBP, etc. We're only gonna be left with some OPTION keys that will have numbers at the end. 现在,所有数字都从诸如PRICING,GBP等键中移走了。我们只剩下一些OPTION键,这些键的末尾都有数字。 Then we will be able to go: 然后,我们将能够:

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'];

}

Try it out. 试试看。 I think that should work. 我认为应该可以。

PS: They should really figure something out on their end about that API because that's a horrible array to work with. PS:他们真的应该对API有所了解,因为这是一个可怕的数组。 There's no reason for that. 没有任何理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM