PHP - array "reversal"

  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

I have a problem. I'm using a foreach loop to step through an array and parse the contents using the code below:

Code: [ Select ]
// Put data into array
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $array[$line["id"]] = $line["name"];
}

//Process data
foreach ($array as $procdata) {
  echo $procdata;

}
  1. // Put data into array
  2. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  3.   $array[$line["id"]] = $line["name"];
  4. }
  5. //Process data
  6. foreach ($array as $procdata) {
  7.   echo $procdata;
  8. }


Inside that foreach loop I need to be able to know what the value of id was when it came back from MySQL.

I hope I made sense. If you need clarification just ask.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • bob9999
  • Newbie
  • Newbie
  • bob9999
  • Posts: 11

Post 3+ Months Ago

I may not understand the problem.

If the 'id' is a column name in your table then the code is simply:

Code: [ Select ]
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
     $this_id = $line["id"];
}
  1. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2.      $this_id = $line["id"];
  3. }


Am I missing something here?

Bob[/code]
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

The problem is that I cannot process the data as it comes back from mysql. I have to put it in an array and process it later in the sccript. You see that foreach loop ? That's what I"m using to process my array.
  • bob9999
  • Newbie
  • Newbie
  • bob9999
  • Posts: 11

Post 3+ Months Ago

I see what you mean but why not process the array inside the while loop?

Code: [ Select ]
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    //Process data
    # do some stuff here etc.
    # do more processing here etc.    
}
  1. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  2.     //Process data
  3.     # do some stuff here etc.
  4.     # do more processing here etc.    
  5. }


If you really have to save it to another array you can do something ugly like:


Code: [ Select ]
# UGLY CODE FOLLOWS:
$counter = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $arr_save[$counter]["name"] = $line["name"];
    $arr_save[$counter]["id"] = $line["id"];
    $counter++;
}

# later loop through the $arr_save array & do your processing
  1. # UGLY CODE FOLLOWS:
  2. $counter = 0;
  3. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  4.     $arr_save[$counter]["name"] = $line["name"];
  5.     $arr_save[$counter]["id"] = $line["id"];
  6.     $counter++;
  7. }
  8. # later loop through the $arr_save array & do your processing


The first example is cleaner but I _think_ the second example will do what you want to do.

I hope this helps,

Bob


rjstephens wrote:
The problem is that I cannot process the data as it comes back from mysql. I have to put it in an array and process it later in the sccript. You see that foreach loop ? That's what I"m using to process my array.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

I'm afraid I don't quite understand what that code does. I have solved my problem using a rather unsatisfactory solution:
Code: [ Select ]
// Put data into array
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $array[$line["id"]] = $line["name"];
  $array2[$line["name"]] = $line["id"];
}

//Process data
foreach ($array as $procdata) {
  echo $procdata;
  echo $array2[$procdata];

}
  1. // Put data into array
  2. while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  3.   $array[$line["id"]] = $line["name"];
  4.   $array2[$line["name"]] = $line["id"];
  5. }
  6. //Process data
  7. foreach ($array as $procdata) {
  8.   echo $procdata;
  9.   echo $array2[$procdata];
  10. }


Although this code does exactly what I want, i think that the amount of extra CPU consumed is unsatisfactory.

I can't process the data inside the loop because I have removed a lot from the code. Inside the processing loop, I query the database again three times (per loop), and I can't do this if I am currently getting data, can I?
  • bob9999
  • Newbie
  • Newbie
  • bob9999
  • Posts: 11

Post 3+ Months Ago

You can nest queries to the database, be sure to give each one its own name

In pseudo code:

Code: [ Select ]
$search_string_1 = 'SELECT.1..';
$search_string_2 = 'SELECT.2.';


$result_1 = query ($search_string_1)

While ($result_1) {
    #do something
    
    $result_2 = query ($search_string_2)
    
    While ($result_2) {
    # do something w/ result 2
    
    )    # end result 2
    
}    # end result 1
  1. $search_string_1 = 'SELECT.1..';
  2. $search_string_2 = 'SELECT.2.';
  3. $result_1 = query ($search_string_1)
  4. While ($result_1) {
  5.     #do something
  6.     
  7.     $result_2 = query ($search_string_2)
  8.     
  9.     While ($result_2) {
  10.     # do something w/ result 2
  11.     
  12.     )    # end result 2
  13.     
  14. }    # end result 1


That's the cleaner way to do things, I hope it does the job for you.

Bob


rjstephens wrote:
<SNIP>


Although this code does exactly what I want, i think that the amount of extra CPU consumed is unsatisfactory.

I can't process the data inside the loop because I have removed a lot from the code. Inside the processing loop, I query the database again three times (per loop), and I can't do this if I am currently getting data, can I?

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 94 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.