1

I have two fetch arrays retrieved from the database, and try to compare two values until they have the matching, but it seems bit hard to figure out where I got wrong.

the two database have upto 2 jobrequestnumber so, the number of the matching is supposedly two, but it only counts one(indicated below).

 $value1=array();
 $value2 =array();

 $queryfordispatch = "select jobrequestnumber from dispatch";
 $resultfordispatch = mysql_query($queryfordispatch);
 $valuefordispatch =mysql_fetch_array($resultfordispatch);
 $value1 = $valuefordispatch['jobrequestnumber'];

 $queryforjobrequest = "select jobrequestnumber from jobrequest";
 $resultforjobrequest = mysql_query($queryfordispatch);
 $valueforjobrequest =mysql_fetch_array($resultforjobrequest);
 $value2 = $valueforjobrequest['jobrequestnumber'];

 $cfd=count($valuefordispatch);
 $cfj=count($valueforjobrequest);

//In this for loop, if I try to echo the value of $value1, it only produce the value of "1"

for($i=1; $i<=$cfd; $i++){
for($j=1; $j<=$cfj; $j++){
if ($value1 == $value2 ){

    $queryforupdate ="UPDATE jobrequest SET status = 'processed' where jobrequestnumber = $value2 ";
    mysql_query($queryforupdate);
  }
 }
}

So the result that I expect is that

jobnum

 1=1? yes --> update
 1=2? no  --> discard
 2=1? no  --> discard
 2=2? yes --> update
Giantrain
  • 19
  • 1
  • 7
  • What exactly do you want? Put values of `$valuefordispatch` and `$valueforjobrequest` here and tell us what should be the result. – u_mulder Jun 06 '13 at 16:34
  • You should use MySQLi or PDO instead of mysql_ functions, they've been deprecated. –  Jun 06 '13 at 16:34
  • Where do $value1 and $value2 get set to different values, at the moment although you iterate the count of the two returned arrays, the values never change. –  Jun 06 '13 at 16:37
  • so can I ask how I get different values? – Giantrain Jun 06 '13 at 16:39
  • Welcome to Stack Overflow! [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Jun 06 '13 at 16:39

2 Answers2

0

If you 're trying to see if array1 and array2 share values then use something like this.

$a = array('1','2','3','4','5','6','7','8','9');
$b = array('A','B','C','4','5','6','7','E','F');
$num_shared = 0;

foreach($a as $key => $val) {
    if (in_array($val, $b)) {
        echo $val . '<br />';
        $num_shared++;
    }
}

echo '<strong>'. $num_shared . '</strong>';
AJames
  • 64
  • 3
  • It will prompt with number of the same values I guess, but I want to select a index where it has the same value, or retrieve the same one. In addition, I should use fetch_array – Giantrain Jun 06 '13 at 16:46
  • Use an associative array and when you use array_intersect or in_array, just check for the index of that associative item – AJames Jun 06 '13 at 16:52
0

Let's start with this:

Welcome to Stack Overflow! Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

As for finding the difference between two arrays, one can always use array_diff:

<?php
//Example #1 array_diff() example
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output:

Array
(
    [1] => blue
)

However, to compare each value separately, you need to loop through both arrays at the same time (with a for loop, preferably), and compare each and every item. Which is kind of what you're already doing.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Thanks, I think I will help a lot for the future, but is there any way that I can retrieve the same values from the array ? – Giantrain Jun 06 '13 at 16:53
  • @Giantrain: That can only be done with a loop (which is kind of what you're doing already). This will give you the items which are found in `$array1` which aren't in `$array2`. – Madara's Ghost Jun 06 '13 at 16:56
  • Sorry, the same values from the two array*, so items are found in $array1 which are also in $array2. This is the expected result ;) – Giantrain Jun 06 '13 at 17:05