php - Looping in a complex array -
hope fine today. have xml file online stored array using file_get_contents(), simplexml_load_string() json_encode , json_decode; using above methods gave array this:
[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 ) ) ) ) ) )
)
now want loop through array, want re-convert xml format, able access first level of array , orderitems -> item -> code client purchased 1 single item, not able loop inside deeper level client purchased more 1 item (second array)
here code
foreach($array['data']['transaction'] $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'] $multi_item){ echo "<--- multi ---><br>"; //tried many things , none has worked; //like $multi_item['code']; } } }
how access code of each item purchased john doe?
you can access data filtering array.
in php >= 5.5.0
can make search this;
$key = array_search('john doe', array_column($yourarray, 'cardholdersname'));
this method return array matched given criteria. http://php.net/manual/tr/function.array-search.php
Comments
Post a Comment