smilesmita Posted August 17, 2007 Share Posted August 17, 2007 hi, i want to loop thru this array so that i can read the tagname from 0_filename and put records till 1_filename to insert query and again read from 1_filename to 2_filename and call query again and keep on doing that till the time i see the last n_filename tag Array( [0_filename] => 070806_121635001first [0_statustype_code] => U [0_statustype_description] => Unreadfirst [0_manifest_shipper_name] => E-Z LINE PIPE SUPPORfirstT [0_manifest_shipper_shippernumber] => F6792Wfirst [0_manifest_shipper_address_addressline1] => 21340 HIGHWAY 6first [0_manifest_shipper_address_city] => MANVELfirst [0_manifest_shipper_address_stateprovincecode] => TXfirst [0_manifest_shipper_address_postalcode] => 77578-3832first [0_manifest_shipper_address_countrycode] => US [0_manifest_shipto_attentionname] => RON HANSONfirst [0_manifest_shipto_address_consigneename] => CENTERPOINT ENERGYfirst [0_manifest_shipto_address_addressline1] => 4011 TOWNSHIP LINE RDfirst [0_manifest_shipto_address_city] => POPLAR BLUFFfirst [0_manifest_shipto_address_stateprovincecode] => MOfirst [0_manifest_shipto_address_postalcode] => 63901first [0_manifest_shipto_address_countrycode] => USfirst [0_manifest_referencenumber_0_code] => JNfirst [0_manifest_referencenumber_0_value] => E-Z 707176first [0_manifest_referencenumber_1_code] => PNfirst [0_manifest_referencenumber_1_value] => 4502225369first [0_manifest_service_code] => 003first [0_manifest_pickupdate] => 20070806first [0_manifest_scheduleddeliverydate] => 20070809first [0_manifest_package_activity_date] => 20070806first [0_manifest_package_activity_time] => 113611first [0_manifest_package_trackingnumber] => 1ZF6792W0341116970first [0_manifest_package_referencenumber_0_code] => JNfirst [0_manifest_package_referencenumber_0_value] => E-Z 707176first [0_manifest_package_referencenumber_1_code] => PNfirst [0_manifest_package_referencenumber_1_value] => 4502225369first [0_manifest_billtoaccount_option] => 04first [0_manifest_billtoaccount_number] => W0783Xfirst ///till here it shud input all the above records where insert statement would look like this: insert into table (0_filename,0_statustype_code.....till 0_manifest_billaccount_number) values(070.......,..,..,); and now the loop shud start again from here.something like a for loop which goes on till it finds the last set of filename tag and its data beneath it. [1_filename] => 070806_073031001second [1_statustype_code] => U [1_statustype_description] => Unreadsecond [1_origin_packagereferencenumber_value] => 0012441second [1_origin_shipmentreferencenumber_value] => 0012second [1_origin_shippernumber] => 256YW5second [1_origin_trackingnumber] => 1Z256YW50341121213second [1_origin_date] => 20070806second [1_origin_time] => 042400second [1_origin_activitylocation_addressartifactformat_politicaldivision2] => SAINT PAULsecond [1_origin_activitylocation_addressartifactformat_politicaldivision1] => MNsecond [1_origin_activitylocation_addressartifactformat_countrycode] => USsecond [1_origin_billtoaccount_option] => 04second [1_origin_billtoaccount_number] => W0783Xsecond [1_origin_1_packagereferencenumber_value] => 01second [1_origin_1_shipmentreferencenumber_value] => 0441second [1_origin_1_shippernumber] => 256YW5second [1_origin_1_trackingnumber] => 1Z256YW50342133422second [1_origin_1_date] => 20070806second [1_origin_1_time] => 042400second [1_origin_1_activitylocation_addressartifactformat_politicaldivision2] => SAPAULsecond [1_origin_1_activitylocation_addressartifactformat_politicaldivision1] => Msecond [1_origin_1_activitylocation_addressartifactformat_countrycode] => USsecond [1_origin_1_billtoaccount_option] => 04second [1_origin_1_billtoaccount_number] => W0783Xsecond Loop again from here: [2_filename] => 070806_073031001second [2_statustype_code] => U [2_statustype_description] => Unreadsecond [2_origin_packagereferencenumber_value] => 0012441second [2_origin_shipmentreferencenumber_value] => 0012second [2_origin_shippernumber] => 256YW5second [2_origin_trackingnumber] => 1Z256YW50341121213second [2_origin_date] => 20070806second [2_origin_time] => 042400second [2_origin_activitylocation_addressartifactformat_politicaldivision2] => SAINT PAULsecond [2_origin_activitylocation_addressartifactformat_politicaldivision1] => MNsecond [2_origin_activitylocation_addressartifactformat_countrycode] => USsecond [2_origin_billtoaccount_option] => 04second [2_origin_billtoaccount_number] => W0783Xsecond [2_origin_1_packagereferencenumber_value] => 01second [2_origin_1_shipmentreferencenumber_value] => 0441second [2_origin_1_shippernumber] => 256YW5second [2_origin_1_trackingnumber] => 1Z256YW50342133422second [2_origin_1_date] => 20070806second [2_origin_1_time] => 042400second [2_origin_1_activitylocation_addressartifactformat_politicaldivision2] => SAPAULsecond [2_origin_1_activitylocation_addressartifactformat_politicaldivision1] => Msecond [2_origin_1_activitylocation_addressartifactformat_countrycode] => USsecond [2_origin_1_billtoaccount_option] => 04second [2_origin_1_billtoaccount_number] => W0783Xsecond ) Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/ Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 what code do you have so far ? surely something like this $total = (count($therray) /33) for($n=0,$n<$total,$n++) { echo $therray[$n."_filename"]; echo $therray[($n+1)."_filename"]; } would work! Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326663 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 thats one of the problem ..when u say divide by 33...its not always 33 elements you would find till you encounter the next filename tag. if you count the tags from next filename ...they are 26 and i can have multiple such filename tags in the array.i have just kept it to 2 iterations in the array i submitted but it could be somtime 4 or 5 or anything..? any idea ..? Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326676 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 ok use array_key_exists instead $n=0; $key = "_filename"; while ( array_key_exists($therray[($n+1).$key]) ) { echo $therray[($n).$key]; echo $therray[($n+1).$key]; $n++ } Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326693 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 its giving me this error: Warning: Wrong parameter count for array_key_exists() in.... Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326699 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 lol, my bad try this $n=0; $key = "_filename"; while ( array_key_exists(($n+1).$key, $therray) ) { echo $therray[($n).$key]; echo $therray[($n+1).$key]; $n++ } i really should review my code before posting it, Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326703 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 no error..but there is no output on screen..screen comes as blank? am i missing anything? Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326725 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 can you post you code Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326743 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 guess its my mistake...the array is like this instead of shown above.we will have to get rid of the string subscriptioevents_subscriptionfile from the key elment and put this key element along with its value into new array adn then apply the loop which you gave...then the results will show up..guess soo.. any idea how i could acheive this Array ( [subscriptionevents_subscriptionfile_0_filename] => 070806_121635001first [subscriptionevents_subscriptionfile_0_statustype_code] => U [subscriptionevents_subscriptionfile_0_statustype_description] => Unreadfirst [subscriptionevents_subscriptionfile_0_manifest_shipper_name] => E-Z LINE PIPE SUPPORfirstT [subscriptionevents_subscriptionfile_0_manifest_shipper_shippernumber] => F6792Wfirst [subscriptionevents_subscriptionfile_0_manifest_shipper_address_addressline1] => 21340 HIGHWAY 6first [subscriptionevents_subscriptionfile_0_manifest_shipper_address_city] => MANVELfirst [subscriptionevents_subscriptionfile_0_manifest_shipper_address_stateprovincecode] => TXfirst [subscriptionevents_subscriptionfile_0_manifest_shipper_address_postalcode] => 77578-3832first [subscriptionevents_subscriptionfile_0_manifest_shipper_address_countrycode] => US [subscriptionevents_subscriptionfile_0_manifest_shipto_attentionname] => RON HANSONfirst [subscriptionevents_subscriptionfile_0_manifest_shipto_address_consigneename] => CENTERPOINT ENERGYfirst [subscriptionevents_subscriptionfile_0_manifest_shipto_address_addressline1] => 4011 TOWNSHIP LINE RDfirst [subscriptionevents_subscriptionfile_0_manifest_shipto_address_city] => POPLAR BLUFFfirst [subscriptionevents_subscriptionfile_0_manifest_shipto_address_stateprovincecode] => MOfirst [subscriptionevents_subscriptionfile_0_manifest_shipto_address_postalcode] => 63901first [subscriptionevents_subscriptionfile_0_manifest_shipto_address_countrycode] => USfirst [subscriptionevents_subscriptionfile_0_manifest_referencenumber_0_code] => JNfirst Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326749 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 updated $n=0; $prefixkey = "subscriptionevents_subscriptionfile_"; $surfixkey= "_filename"; $NewKey= $prefixkey.($n+1).$surfixkey; while ( array_key_exists($NewKey, $therray) ) { echo $therray[($n).$key]; echo $therray[($n+1).$key]; $n++ $NewKey= $prefixkey.($n+1).$surfixkey; } Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326757 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 thanks! this is wht i am getting: Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326775 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 the $therray has to be the name of your array/object Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326788 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 ohh...so very stupid of me:) thanks a ton! Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326791 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 remember to click topic solved if all is working Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326793 Share on other sites More sharing options...
smilesmita Posted August 17, 2007 Author Share Posted August 17, 2007 i get a blank screen still? this is the code: conversion of this xml file to array is done by downloading the program online xml2 array found here: http://www.codewalkers.com/c/a/Database-Code/Improved-XML-To-Array/ attatched is the xml file. function start() { $this->Parse_File(); } //start Function Parse_File() { global $post; $fp = fopen("xmlfortry.xml", "r"); $buff = ''; while (!feof($fp)) { $buff .= fread($fp, 4096); } fclose($fp); $xmldata=xmlFileToArray("xmlfortry.xml", $includeTopTag = false, $lowerCaseTags = true); $marray=$this->final_paths($xmldata); $n=0; $prefixkey = "subscriptionevents_subscriptionfile_"; $surfixkey= "_filename"; $NewKey= $prefixkey.($n+1).$surfixkey; while (array_key_exists($NewKey,$marray)) { echo $marray[($n).$key]; echo $marray[($n+1).$key]; $n++; $NewKey= $prefixkey.($n+1).$surfixkey; } } function final_paths($array, $splitter = '_', $prefix = '') { $return_array = array(); if( $prefix != '' ) { $prefix .= $splitter; } foreach($array as $key => $val) { if( is_array($val) ) { $return_array = array_merge($return_array,$this->final_paths($val,$splitter, $prefix . $key)); } else { $return_array[$prefix . $key] = $val; } } return $return_array; } [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326815 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 ok, i took the time to test this one $n=0; $prefixkey = "subscriptionevents_subscriptionfile_"; $surfixkey= "_filename"; $NewKey= $prefixkey.($n+1).$surfixkey; $OldKey= $prefixkey.($n).$surfixkey; while (array_key_exists($NewKey,$marray)) { echo $marray[$OldKey]; echo " => "; echo $marray[$NewKey]; echo "<br>"; $n++; $OldKey= $prefixkey.($n).$surfixkey; $NewKey= $prefixkey.($n+1).$surfixkey; } Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326839 Share on other sites More sharing options...
sasa Posted August 17, 2007 Share Posted August 17, 2007 you can rearange your array try <?php $new_arr = array(); foreach ($marray as $key => $value) { preg_match('/subscriptionevents_subscriptionfile_([0-9]+)_(.*)/', $key, $b); $new_arr[$b[1]][$b[2]] = $value; } //print_r($new_arr); foreach ($new_arr as $value) echo $value['filename'], "\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65415-solved-looping-thru-array-like-for-loop/#findComment-326956 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.