ad_aus,

If you set a variable equal to the array item, you can explode again on the variable. Example:

$counter=0;
$loader_array=array();
foreach ($result as $result_item)
{
$new_array=explode(" ",$result_item);
$loader_array[$counter]["name"]=$new_array[0];
$loader_array[$counter]["val1"]=$new_array[1];
$loader_array[$counter]["val2"]=$new_array[2];
$loader_array[$counter]["val3"]=$new_array[3];
$counter=$counter+1;
}

    Thanks guys, very handy info! I really appreciate.

    It turns out I was unable to use regex nor explode - rather, I had to revert to substr() function to extract fixed width strings. Here is an example:

    $counter=0;
    $loader_array=array();
    foreach ($result as $result_item)
    {
    //$new_array=explode(" ",$result_item); - can't use due to irregular naming conventions
    
    $new_array[0]=substr($result_item, 0, 10); 
    $new_array[1]=substr($result_item, 12, 4); 
    $new_array[2]=substr($result_item, 17, 5); 
    $new_array[3]=substr($result_item, 23, 2); 
    
    $loader_array[$counter]["name"]=$new_array[0];
    $loader_array[$counter]["var1"]=$new_array[1];
    $loader_array[$counter]["var2"]=$new_array[2];
    $loader_array[$counter]["var3"]=$new_array[3];
    $counter=$counter+1;
    }
    
    

    Next challenge is to do some more splits/reformats of the $new_array variables, eg:
    - remove extra spaces (eg "name " to "name")
    - convert string to a number (eg "3500" to -35.00)

    Only then I'll have the array in the fomat ready for import into MySQL. You've been most helpful so far so, any further pointers mych appreciated.

    Cheers!

      Removing the extra spaces is easy with [man]trim[/man]

      You could always use [man]sprintf[/man] or [man]printf[/man] to get the numbers in the format you want 😉

        5 days later

        I put this puzzle away only for a moment and... a week is gone!

        Thanks bpat, I'll explore these functions further. Meantime a queston on extracting specific info from the array (as per code above).

        If I want to loop only through a given names, not the entire list, how to set foreach () function?

        foreach( $loader_array as $counter) will loop through the whole list but I want to limit it only to say ["name'] = (name1, name5, name 45, etc...);

        Any suggestions much appreciated. Thanks!

          Use a for() loop instead. Just do something like:

          for($i=0, $max=count($array); $i<$max; $i++)
          {
            // Reference "name" like so: $array[$i]['name']
            // e.g.:
            echo $array[$i]['name'];
          }

            Thanks again for pointing me in the right direction!

            As a follow up, I only want to output records matching specific names. If there are a few I can use if() with "or" statement but what to do if there are many? Can I somehow include an array of names to refer to in if() function?

            for($i=0, $max=count($loader_array); $i<$max; $i++)
            { 
            
            if($loader_array[$i]["name"]=="name1"||"name2"){
            
            //  output the right stuff...
            
            }
            }
            

            I can't get my head around this array stuff and I could not find relevant examples either. Again, any pointers much appreciated!

              You would want to specify an array of matching names:

              $allowed = array('name1','name2');

              Then use the [man]in_array/man function to see if $loader_array[$i]['name'] is in the array $allowed 😉

                Easy 🆒

                Thanks bpat, I'm rolling again! I looked at in_array() function before but totally missed its relevance here... now to some "trimming and pruning" and one part of the puzzle will be solved (writing into a database yet to come...).

                Meantime, another question, how does server handle requests for a php file which is being opened (fopen) and written to? Does it wait for the file to be freed and then responds or does it thow an error?

                  PHP will process it. If it can't open the file, it will toss an error in your script, and if you catch that error, will keep going. Otherwise an warning is printed onto the screen and the script keeps rolling.

                  So php will wait for the current command to be completed before moving on to the next. It's very-much procedural in that it executes in a top-down fashion. One line executed at a time.

                    Write a Reply...