1

Need to find if two arrays match, and then where they match pull data from the mysql row in which they match. Should I use

 $sql = "SELECT * FROM around";
 $resultsd = $conn->query($sql);
 foreach($resultsd as $rowd) {}

 if (array_intersect($ar1, $ar2)) {
    $sword[] = $rowd['TIM'];
    }

or should I use

 if (in_array($ar1, $ar2)) {
       $sword[] = $rowd['TIM'];
   }

Getting the arrays like:

   $ar1[] = $rowd['nim']; 
   $ar2[] = $rowd['nim']; 

Then how does one go about pulling the specific row that they match at?

I am seeing that they match, and printing out the array's okay:

Array ( [0] => dcbabcbded ) Array ( [0] => fafeafaebee [1] => afabfdefcbb [2] => dcbabcbded

But when I trying a echo the mysql data where they match I fail:

 Array ( )  

2 Answers 2

0

i would go with sql IN clause.

You have an array of customer names: $a = array('john','rob','paul');

implode array $nms = join(',',$a);

Make sql: 'SELECT * FROM tabl WHERE name IN ('.$nms.')';


do the array intersection first (or what you just have to..) to have an array of needed names.

5
  • I have to see where the two arrays match first, then get the mysql data from only that row. What you have provided will not do such, I do not believe. Commented Mar 24, 2015 at 7:00
  • yes you have to prepare array of matches to you tend to gather first, use in_array or intersect Commented Mar 24, 2015 at 7:00
  • I am sorry, but your answer is not helpful. It simply restates my question. Commented Mar 24, 2015 at 7:02
  • allright Show me both arrays and show desired result. This Just must be simple. Commented Mar 24, 2015 at 7:03
  • Getting both arrays with $ar1[] = $rowd['nim']; $ar2[] = $rowd['nim']; Commented Mar 24, 2015 at 7:04
0

use $new = array_intersect(Array ( [0] => dcbabcbded ), Array ( [0] => fafeafaebee [1] => afabfdefcbb [2] => dcbabcbded)) : then make valid sql as stated in my answer above.

If u are sure you've allways have array intersenct with one or none values, then make sql just with first array element: $ar[0] using sql WHERE clause.

2
  • I am sorry. This just is not clear. The arrays are stored as $variables. Also I am having trouble pulling the mysql data from the row that they match at. How is that accomplished? Commented Mar 24, 2015 at 7:10
  • no i lost you, SELECT * FROM table WHERE col = i have this value look up POD an use that ;) bye Commented Mar 24, 2015 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.