Solved

Php array intersection: how to combine two loops

Posted on 2011-02-22
9
354 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
Comment Utility
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
Comment Utility
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
Comment Utility
Actually, I think that last comment of min is incorrect. Try using

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

how does that work out?
0
 

Author Comment

by:Dada44
Comment Utility
It does not work, printing array2 in the loop without []  shows the values but with the []  it does not print anything :(
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad you're sorted.

Cheers!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now