循环复杂的数组

时间:2017-04-07 10:59:55

标签: php arrays xml

希望你今天没事。 我有一个在线的xml文件,我使用file_get_contents(),simplexml_load_string()json_encode和json_decode将它存储到一个数组中;使用上面的方法给出了这样的数组:

[data] => Array
    (
        [transaction] => Array
            (
                [0] => Array
                    (
                        [fields] => Array
                            (
                                [transactionid] => 90397725
                                [transactionreference] => 90397725
                                [transactiontime] => 14:34:56
                                [transactiondate] => 2016-04-08
                                [upgauthcode] => 649192
                                [cardnumber] => ***************5104
                                [cardholdersname] => MISS Jane K Doe
                                [switchnumber] => Array
                                    (
                                    )

                                [cardstartyear] => 15
                                [cardstartmonth] => 02
                                [cardexpireyear] => 20
                                [cardexpiremonth] => 02
                                [transactionamount] => 108.00
                                [cardtype] => VISADEBIT
                                [baskettype] => order
                                [regisalu] => Miss
                                [regifnam] => Jane
                                [regilnam] => Doe
                                [regiadd1] => 14 Test Street
                                [regiadd4] => Test
                                [regiadd5] => West Test
                                [regiadd6] => fg28 5nZ
                                [regiadd7] => United Kingdom
                                [regidnum] => 07464000000
                                [regienum] => 07464000000
                                [regimobi] => 07464000000
                                [maddsalu] => Miss
                                [maddfnam] => Jane
                                [maddlnam] => Doe
                                [maddadd1] => 14 Test Street
                                [maddadd4] => Test
                                [maddadd5] => West Test
                                [maddadd6] => fg28 5nZ
                                [maddadd7] => United Kingdom
                                [madddnum] => 07464000000
                                [maddenum] => 07464000000
                                [maddidel] => Deliver
                                [baskettotal] => 90.00
                                [hirewaivertotal] => 0.00
                                [deliverytotal] => 0.00
                                [collectiontotal] => 0.00
                                [cardholderaddr1] => 14 Test Street
                                [cardholderaddr2] => Array
                                    (
                                    )

                                [cardholdercity] => Test
                                [cardholderstate] => West Test
                                [cardholderpostcode] => fg28 5nZ
                                [cardholdercountry] => United Kingdom
                                [regiuser] => jane.doe@gmail.com
                                [regihtml] => 0
                                [regigrde] => 0
                                [regigrdn] => 0
                                [regipopt] => 0
                                [maddcoll] => 1
                                [maddgrde] => 0
                                [maddgrdn] => 0
                            )

                        [orderitems] => Array
                            (
                                [item] => Array
                                    (
                                        [code] => DL008
                                        [sku] => Array
                                            (
                                            )

                                        [desc] => 22M LED Festoon (230v)
                                        [description] => DL008, 22M LED Festoon (230v)
                                        [price] => 45.00
                                        [qty] => 2
                                        [totalprice] => 90.00
                                    )

                            )

                    )

                    [45] => Array
                    (
                        [fields] => Array
                            (
                                [transactionid] => 93645131
                                [transactionreference] => 93645131
                                [transactiontime] => 14:15:16
                                [transactiondate] => 2016-07-07
                                [upgauthcode] => 085526
                                [cardnumber] => ***************0103
                                [cardholdersname] => John Doe
                                [switchnumber] => Array
                                    (
                                    )

                                [cardstartyear] => 15
                                [cardstartmonth] => 12
                                [cardexpireyear] => 18
                                [cardexpiremonth] => 11
                                [transactionamount] => 588.00
                                [cardtype] => MASTERCARD
                                [baskettype] => order
                                [regisalu] => Mr
                                [regifnam] => John
                                [regilnam] => Doe
                                [regiadd1] => 39-45 TEST SQUARE
                                [regiadd2] => CITY Test Aquarium
                                [regiadd4] => London
                                [regiadd6] => bb2A 1op
                                [regiadd7] => United Kingdom
                                [regidnum] => 074640000000
                                [regienum] => 074640000000
                                [regimobi] => 07464000000
                                [maddsalu] => Mr
                                [maddfnam] => John
                                [maddlnam] => Doe
                                [maddadd1] => 39-45 TEST SQUARE
                                [maddadd2] => CITY Test AQUARIUM
                                [maddadd4] => London
                                [maddadd6] => bb2A 1op
                                [maddadd7] => United Kingdom
                                [madddnum] => 07464000000
                                [maddenum] => 07464000000
                                [maddidel] => Deliver
                                [baskettotal] => 490.00
                                [hirewaivertotal] => 0.00
                                [deliverytotal] => 0.00
                                [collectiontotal] => 0.00
                                [cardholderaddr1] => 39-45 TEST SQUARE
                                [cardholderaddr2] => CITY TEST AQUARIUM
                                [cardholdercity] => London
                                [cardholderpostcode] => bb2A 1op
                                [cardholdercountry] => United Kingdom
                                [regiuser] => john.doe@bloomberg.net
                                [regihtml] => 0
                                [regipopt] => 1
                                [regigrde] => 0
                                [regigrdn] => 0
                                [maddcoll] => 1
                                [maddgrde] => 0
                                [maddgrdn] => 0
                            )

                        [orderitems] => Array
                            (
                                [item] => Array
                                    (
                                        [0] => Array
                                            (
                                                [code] => PK289
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => EvoLite Helmet STD Peak (white)
                                                [description] => PK289, EvoLite Helmet STD Peak (white)
                                                [price] => 9.00
                                                [qty] => 6
                                                [totalprice] => 54.00
                                            )

                                        [1] => Array
                                            (
                                                [code] => GGE3
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Safety Spectacle
                                                [description] => GGE3, Graft Gear Safety Spectacle
                                                [price] => 2.00
                                                [qty] => 6
                                                [totalprice] => 12.00
                                            )

                                        [2] => Array
                                            (
                                                [code] => GG025
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Hi-Vis Vest (M)
                                                [description] => GG025, Graft Gear Hi-Vis Vest (M)
                                                [price] => 5.00
                                                [qty] => 2
                                                [totalprice] => 10.00
                                            )

                                        [3] => Array
                                            (
                                                [code] => GG027
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Hi-Vis Vest (XL)
                                                [description] => GG027, Graft Gear Hi-Vis Vest (XL)
                                                [price] => 5.00
                                                [qty] => 4
                                                [totalprice] => 20.00
                                            )

                                        [4] => Array
                                            (
                                                [code] => GG029
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Hi-Vis Vest (3XL)
                                                [description] => GG029, Graft Gear Hi-Vis Vest (3XL)
                                                [price] => 5.00
                                                [qty] => 4
                                                [totalprice] => 20.00
                                            )

                                        [5] => Array
                                            (
                                                [code] => GG030
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Hi-Vis Vest (4XL)
                                                [description] => GG030, Graft Gear Hi-Vis Vest (4XL)
                                                [price] => 5.00
                                                [qty] => 2
                                                [totalprice] => 10.00
                                            )

                                        [6] => Array
                                            (
                                                [code] => GG037
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Bomber (M)
                                                [description] => GG037, Graft Gear Bomber (M)
                                                [price] => 23.00
                                                [qty] => 1
                                                [totalprice] => 23.00
                                            )

                                        [7] => Array
                                            (
                                                [code] => GG039
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Bomber (XL)
                                                [description] => GG039, Graft Gear Bomber (XL)
                                                [price] => 23.00
                                                [qty] => 2
                                                [totalprice] => 46.00
                                            )

                                        [8] => Array
                                            (
                                                [code] => GG041
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Graft Gear Bomber (XXXL)
                                                [description] => GG041, Graft Gear Bomber (XXXL)
                                                [price] => 23.00
                                                [qty] => 3
                                                [totalprice] => 69.00
                                            )

                                        [9] => Array
                                            (
                                                [code] => GG073
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Nubuck Mid-Cut Safety Boot
                                                [description] => GG073, Nubuck Mid-Cut Safety Boot
                                                [price] => 39.00
                                                [qty] => 1
                                                [totalprice] => 39.00
                                            )

                                        [10] => Array
                                            (
                                                [code] => GG071
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Nubuck Mid-Cut Safety Boot
                                                [description] => GG071, Nubuck Mid-Cut Safety Boot
                                                [price] => 39.00
                                                [qty] => 2
                                                [totalprice] => 78.00
                                            )

                                        [11] => Array
                                            (
                                                [code] => GG070
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Nubuck Mid-Cut Safety Boot
                                                [description] => GG070, Nubuck Mid-Cut Safety Boot
                                                [price] => 39.00
                                                [qty] => 2
                                                [totalprice] => 78.00
                                            )

                                        [12] => Array
                                            (
                                                [code] => PK454
                                                [sku] => Array
                                                    (
                                                    )

                                                [desc] => Ladies Black Hiker Boot
                                                [description] => PK454, Ladies Black Hiker Boot
                                                [price] => 31.00
                                                [qty] => 1
                                                [totalprice] => 31.00
                                            )

                                    )

                            )

                    )

            )
    )       

现在我想循环遍历该数组,因为我想将其重新转换为我的XML格式,我能够访问数组的第一级和orderitems - > item - >客户购买一个项目的代码,但我无法在客户购买多个项目(第二个数组)的更深层次内循环

这是我的代码

foreach($array['data']['transaction'] as $transaction){
    //echo $transaction['fields']['transactionid']."<br>";
        //echo $transaction['orderitems']['item']['code']."<br>";
    if(isset($transaction['orderitems']['item']['code'])){
        echo $transaction['orderitems']['item']['code']."<br>";
    } else {
        foreach($transaction['orderitems']['item'] as $multi_item){
            echo "<--- MULTI ---><br>";
            //tried so many things and none has worked;
            //like $multi_item['code'];
        }
    }

} 

如何访问john doe购买的每件商品的代码?

1 个答案:

答案 0 :(得分:1)

您可以通过过滤数组来访问此数据。

PHP >= 5.5.0中,您可以进行这样的搜索;

$key = array_search('John Doe', array_column($yourArray, 'cardholdersname'));

此方法将返回与给定条件匹配的数组。 http://php.net/manual/tr/function.array-search.php