Jump to content

[SOLVED] looping thru array like for loop


smilesmita

Recommended Posts

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

)

Link to comment
Share on other sites

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 ..?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.