Jump to content

help me loop through arrays


aebstract

Recommended Posts

I have a text file, structured like this:

 

Array
(
    [0] => 201
    [userid] => 1
    [1] => OR_host
    [computed] => OR_host
    [2] => host
    [computed1] => host
    [3] => 
    [CreatedDate] => 
)

Array
(
    [0] => 202
    [userid] => 2
    [1] => OR_admin
    [computed] => OR_admin
    [2] => admin@outlawracing.com
    [computed1] => admin@outlawracing.com
    [3] => 1156790760
    [CreatedDate] => Aug 28 2006  1:46PM
)

Array
(
    [0] => 203
    [userid] => 3
    [1] => Tony Reece
    [computed] => Tony Reece
    [2] => tony@outlawracing.com
    [computed1] => tony@outlawracing.com
    [3] => 1156863060
    [CreatedDate] => Aug 29 2006  9:51AM
)

Array
(
    [0] => 204
    [userid] => 4
    [1] => Tony Reece_Admin
    [computed] => Tony Reece_Admin
    [2] => tony.reece@datavue.com
    [computed1] => tony.reece@datavue.com
    [3] => 
    [CreatedDate] => 
)

 

I need to read the text file, and be able to loop through every array. I'm not really sure how I would approach this since the arrays aren't labeled or anything.  :confused:

Link to comment
Share on other sites

I think what he posted is the text file. OP do you have to store the information like that in the text file? if so parsing the file and turning the contents into seperate arrays will be somewhat difficult. not impossible, but time consuming.

 

Perhaps the serialize() function would be better for storing the contents of whatever arrays into the file, and then you could use unserialize() to get the information from the text file back into a php array.

Link to comment
Share on other sites

The information is getting put there like this:

<?php

$server = 'ww26.empiredatatech.com';
$link = mssql_connect($server, 'outlaw', 'out98765law');




$myFile = "array.txt";
$fh = fopen($myFile, 'w') or die("can't open file");






$query = mssql_query("
SELECT ordnn_Users.userid, CAST(ordnn_Users.username as varchar), CAST(ordnn_Users.email as varchar), ordnn_UserPortals.CreatedDate
FROM ordnn_Users
LEFT JOIN ordnn_UserPortals
ON ordnn_UserPortals.UserID = ordnn_Users.UserID
");
if(mssql_num_rows($query) > 0){
while($r=mssql_fetch_array($query)){

$whattime = $r[3];
$whattime = explode(" ", $whattime);

if ($whattime[0] == Jan){
$whattime[0] = 1;
}
if ($whattime[0] == Feb){
$whattime[0] = 2;
}
if ($whattime[0] == Mar){
$whattime[0] = 3;
}
if ($whattime[0] == Apr){
$whattime[0] = 4;
}
if ($whattime[0] == May){
$whattime[0] = 5;
}
if ($whattime[0] == Jun){
$whattime[0] = 6;
}
if ($whattime[0] == Jul){
$whattime[0] = 7;
}
if ($whattime[0] == Aug){
$whattime[0] = 8;
}
if ($whattime[0] == Sep){
$whattime[0] = 9;
}
if ($whattime[0] == Oct){
$whattime[0] = 10;
}
if ($whattime[0] == Nov){
$whattime[0] = 11;
}
if ($whattime[0] == Dec){
$whattime[0] = 12;
}


$whattime2 = explode(":", $whattime[4]);
$whattime2[2] = substr("$whattime2[1]", -2);
$whattime2[1] = substr("$whattime2[1]", 0, 2);

if ($whattime2[2] == 'PM') {
$whattime2[0] = $whattime2[0]+12;
}

$r[3] = mktime($whattime2[0],$whattime2[1],0,$whattime[0],$whattime[1],$whattime[2],0);
$r[0] = $r[0]+200;
echo "<pre>";
print_r($r);
echo "</pre>";
$str = print_r($r, true) . PHP_EOL;
fwrite($fh, $str);
}
} else {
echo mssql_get_last_message();
}

fclose($fh);

?>



 

I don't care what I need to do, I just need to do it now. I'm running out of time on a deadline and need to figure something out. Running in to all kinds of problems trying to grab this information. I'm going to have to do it about 3-4 times (different files and whatnot) so I need to figure out a way to do it.

 

 

EDIT: I can easily store the information in any format in the text file, I just need to know what is easiest/best. Since I'm pulling it from the database, it'll be simple to format the file in any way. Just not sure how I need it to be. Thanks!

Link to comment
Share on other sites

i see you are using print_r to generate the string as I expected here:

$str = print_r($r, true) . PHP_EOL;

 

Try using serialize (check out the link from my post above) to generate the string version of the arrays to store. Then you could simply go through the file line by line, using unserialize to regenerate the PHP data. Alternatively, you could use json_decode/encode but serialize would probably be the better choice as what your doing probably has nothing to do with json

Link to comment
Share on other sites

This code will read the text file and convert it into an array variable.

 

<?php
  
$textData = file('array_data.txt');
  
$dataArray = array();
$tempArray = array();
$array_start = false;
  
foreach ($textData as $line)
{
   if ($array_start && preg_match("/\[([^\]]*)\] => (.*)/", $line, $keyValue)!= 0)
   {
      $tempArray[$keyValue[1]] = $keyValue[2];
   }
   
   if(substr($line, 0, 1)=='(')
    {
      $array_start = true;
   }
   
   if(substr($line, 0, 1)==')')
    {
      $array_start = false;
      $dataArray[] = $tempArray;
      $tempArray   = array();
   }
}
  
print_r($dataArray);
  
?>

Link to comment
Share on other sites

That gave me the first array results shown on the screen, but I'm needing a way to loop through all of them so I can do something with each piece of information. I'm trying something like

while($r = unserialize(file_get_contents('array.txt'))) {
echo $r;
}

 

Though, it's just returning: ArrayArrayArrayArray..

 

If I echo $r[0] it returns something like: 201201201201201201201..

Link to comment
Share on other sites

when you echo an array in PHP, all it does is echo the literal string 'Array'

 

judging by the fact that its actually saying array, the code is probably working pretty well. if you want to print the array in a human readable format, use print_r like ginger robot suggested

Link to comment
Share on other sites

I don't want to print it out just for reading. I have to be able to use each piece of information. Just like when you pull it from a database and can do something like this:

while($r=mssql_fetch_array($query)){
echo $r[3];
}

 

Except I don't know how to go about this, because that uses mssql_fetch_array and, we aren't fetching an array from a database at this point.

Link to comment
Share on other sites

This code will read the text file and convert it into an array variable.

 

<?php
  
$textData = file('array_data.txt');
  
$dataArray = array();
$tempArray = array();
$array_start = false;
  
foreach ($textData as $line)
{
   if ($array_start && preg_match("/\[([^\]]*)\] => (.*)/", $line, $keyValue)!= 0)
   {
      $tempArray[$keyValue[1]] = $keyValue[2];
   }
   
   if(substr($line, 0, 1)=='(')
    {
      $array_start = true;
   }
   
   if(substr($line, 0, 1)==')')
    {
      $array_start = false;
      $dataArray[] = $tempArray;
      $tempArray   = array();
   }
}
  
print_r($dataArray);
  
?>

 

I was giving the serialize a try, but I don't have the time to get it figure out. This worked and I can easily reference any value in the file now. A while loop will be easy to build with this. Thanks a lot.

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.