Solved

Php array intersection: how to combine two loops

Posted on 2011-02-22
9
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
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…

728 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