Hi,

I have an array which I loop through and within that loop look for results that match the first's values. I need to make sure that all of the matches to the first array are there and if so then write a status to another table. I don't know how many values there would be in the first array and the second array won't be in the same order. I am wondering if there is a better solution to this matching process, and even if this approach below may miss some iteration in the loop causing incorrect results?

    while($get_duty_v_row = $get_duty_v->fetch(PDO::FETCH_ASSOC)){

//get the first record
$value1 = $get_duty_v_row['value1'];

while($and_duties_row = $and_duties->fetch(PDO::FETCH_ASSOC)){

$value2 = $and_duties_row['value2'];

if($value1 !== $value2){
//values don't match, move on
$error[] = "set";
}
}
if(empty($error)){
write to table
}
}

Thanks,

G

    I think you'll need to be a bit more specific about what succeeds and what fails. If $value2 has more entries than $value1 (i.e., $value2 is a superset of $value1) then does that mean success or failure? Are these associative arrays I.e.:

    $value1["some_key"] = "some value");

    or are they just numerically indexed arrays that may not be in the same order?

    You may find the array_diff function useful.

      Thanks for the reply, I am trying to establish if all of the second results set match all of the first set. I am trying to establish after looping through all of the results. Get the first, see if it exists in the second, if no stop and move on. If yes and they all match then I want to update another table.

      I have tried pushing all of the results in the first array into an imploded value set for the second query to get one matching result set, but the code is getting out of hand, checking if the number of values from the first match the second. I just wonder if there is something simple to see if all of the values in the first match all of the second.

      G

        I was trying to clarify that one array might have more values than the other. It's entirely different to say that $value2 has all of the elements in $value1 than to say that $value1 has all the elements in $value2 which is not the same thing as $value1 AND $value2 contain exactly the same elements.

        More precisely, $value2 might contain every item in $value1 but also extra values that are not in $value1.

        And you didn't answer my question as to whether you are checking associative arrays (i.e., arrays with textual key names) or whether you are checking just array values or numerically indexed arrays.

        array_diff is very handy if you don't care about the array keys:

        $v1 = [
          "key1" => "value 1",
          "key2" => "value 2",
        ];
        
        $v2 = [
          "key1" => "value 1",
          "key3" => "value 3",
          "key2" => "value 2",
        ];
        
        $v3 = [
          "alt1" => "value 1",
          "alt2" => "value 2",
          "alt3" => "value 3",
        ];
        
        var_dump(array_diff($v1, $v2));
        var_dump(array_diff($v2, $v1));
        
        var_dump(array_diff($v2, $v3));
        var_dump(array_diff($v3, $v2));

        output:

        array(0) {
        }
        array(1) {
          ["key3"]=>
          string(7) "value 3"
        }
        array(0) {
        }
        array(0) {
        }

        If you want to check and make sure that all of the array keys in $value1 are also set in $value2 then you could do something like this:

        $result = true; // assume everything is cool
        foreach(array_keys($v1) as $key) {
          if (!isset($v2[$key]) || $v1[$key] !== $v2[$key]) {
            $result = false; // missing or ummatched element!
          }
        }
        var_dump($result);

          Thanks for the reply, and to answer your questions (properly this time):

          1. Array 2 cannot have more than 1, but if it has less then I need to move on, if they are the same I update a table
          2. I am just checking values, the second array is from a query where I am checking that the new value matches that from array 1. select * from table1 where name = :name. :name being the value from the first result set.

          Does it look like your last piece of code might be the answer?

          DesignerSeries I honestly don't know. You said you are "just checking values" but then decided that the last code I offered -- which is checking array keys as well -- looked like it might solve your problem. You haven't given any examples of the data you are checking.

          I hope you will look at the code I've offered and decide yourself what you need to check. HINT: array_diff, which I've already mentioned twice, totally ignores the array keys and just checks the values. Think of it as substracting one array from another.

            Thanks, its a very simple routine:

              while($get_duty_v_row = $get_duty_v->fetch(PDO::FETCH_ASSOC)){

            $item1 = $get_duty_v_row;

            //SQL to get results where item = $item1

              while($get_duty_v2_row = $get_duty_v2->fetch(PDO::FETCH_ASSOC)){

            $item2 = $get_duty_v2_row['item'];

            }
            }
            If all of these loops match I want to update a table. To your point, you are right, I am not looking at the keys too. On array_diff would I just check if it returns true, that there are differences? Or am I mistaken?



              Try reading the documentation on array_diff then try some experiments yourself. In addition to the code samples I suggested above, there are plenty in the documentation which illustrate what this function does.

              As to whether it solves your problem, I do not know. Your post is not at all clear.

                Write a Reply...