?
Solved

Php array intersection: how to combine two loops

Posted on 2011-02-22
9
Medium Priority
?
368 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 2000 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
Industry Leaders: 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!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
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 …
Suggested Courses

764 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