Solved

Php array intersection: how to combine two loops

Posted on 2011-02-22
9
359 Views
Last Modified: 2012-05-11
 Hi all,
 
  Here a learner, with the following question:
 
  I need to do:
 
   $intersection = array_intersect($array1, $array2);

Open in new window

 
   I obtain $array1:
   
          for( $i = 0; $i < count ($myarray1['data']); $i ++)
          {
               $array1 = $myarray1['data'][$i]['place']['id']; 
               
          } 

Open in new window

         
    And I obtain $array2 from database:
   
    while($row = mysql_fetch_array( $result )) {

	$array2 = $row['name'];

} 

Open in new window



  But when doing:
    $intersection = array_intersect($array1, $array2);

Open in new window

   outside of the loops, the variables contain the last record obtained in the loop ..
   
    What do I have to do for $array1 and $array2 to contain all the data looped?
   
    Thanks a ton
0
Comment
Question by:Dada44
  • 5
  • 4
9 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34950152
This

    while($row = mysql_fetch_array( $result )) {

      $array2 = $row['name'];

    }

Means that array2 only contains the last row retrieved by mysql_fetch_array. If you wanted to keep all the data retrieved by that loop you would need to modify it like so

    while($row = mysql_fetch_array( $result )) {

      $array2 [] = $row['name'];

    }

but this causes you a second problem because the PHP array functions usually only operate on single dimension arrays, not multi dimensional arrays. I think you need to rethink your appraoch to this problem. Can you tell us more about it?
0
 

Author Comment

by:Dada44
ID: 34950157
Thanks bportlock.

Here's what I'm trying to do:
I have two arrays.

$array1 contains the ids of the places the user has been to.

$array2 contains the ids of the places where the user can obtain one point just by visiting them

Now I need to know if the user has visited any of the places where he/she can get one point so I can grant it to him/her. How can I do that?

Tnaks a lot
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34950176
Actually, I think that last comment of min is incorrect. Try using

      $array2 [] = $row['name'];

how does that work out?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Dada44
ID: 34950206
It does not work, printing array2 in the loop without []  shows the values but with the []  it does not print anything :(
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34950239
OK, I have assumed from the names that $array1 and $array2 are actually arrays. That they have been declared somewhere as

$array1 = array();
$array2 = array();

so just before this

$intersection = array_intersect($array1, $array2);

add these lines like so

echo "<pre>";
print_r( $array1 );
print_r( $array2 );
echo "</pre>";
$intersection = array_intersect($array1, $array2);

What shows up? Part of my confusion comes from the fact that you are assigning values to the variables $array1 and array2 as if they were non-array variables.
0
 

Author Comment

by:Dada44
ID: 34950274
that is working great when the arrays are like:
$array1 = array('1','2','3','4','5');
$array2 = array('3','4','5');

But my arrays a formed:
I obtain $array1:
   
      

          for( $i = 0; $i < count ($myarray1['data']); $i ++)
          {
               $array1 = $myarray1['data'][$i]['place']['id'];
               
          }



         
    And I obtain $array2 from database:
   

  while($row = mysql_fetch_array( $result )) {

      $array2 = $row['name'];

}

If after the loops I do:
$intersection = array_intersect($array1, $array2);

$array1 and $array2 contains the last value looped so intersection idea does not work :(
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34950313
Do the print_r statements I outlined above. My concern is this

 $array1 = $myarray1['data'][$i]['place']['id'];

and

 $array2 = $row['name'];

These are assignments and as such they will overwrite the current values. You have these in loops which means that only the last value of the loop will be in array1 and array2. This is particularly true for the second loop because I know that $row['name']; is a value not an array. That is why I want to see the output from the print_r statements. I suspect that you may need [ ] after both array1 and array2 in those loops.
0
 

Author Closing Comment

by:Dada44
ID: 34950350
yes you're right!  [] is needed (sorry about before, I tried it but it seems the file was cached or something).

Thanks a lot!!!!!!!!
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34950353
Glad you're sorted.

Cheers!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question