0

So currently, I have a single array ($array1) that I want to compare with an array ($array2) that I created by retrieving data from the database. Technically speaking, $array2 is multidimensional since I used a while loop with mysqli_fetch_assoc. Is there any way to compare these two arrays with each other? My end goal is to compare these two arrays, and to only remove the data from the single array ($array1) when there is a mismatch. By that, I mean only to remove the data that doesn't match with $array2.

For example: $array1 = Array ( [0] => cookies [1] => chicken [2] => tesla ) $array2 = Array ( [name] => tesla ) Array ( [name] => bmw ) Array ( [name] => opel )

So in this case, $array2 came from the database, and $array1 is given. So how can I compare these two arrays in order to get this array back: $arraynew = Array ( [0] => tesla )?

Note: So far I have tried this:

            $query = "SELECT name FROM tagsbreedables WHERE idTagCategory = 6";
            $result10 = mysqli_query($conn, $query);

            if (mysqli_num_rows($result10) > 0) {
                while ($row = mysqli_fetch_assoc($result10)) {
                    $bloem = $datas4['name'] = $row;

                    print_r($row);

                    $subarray = array_column($bloem,'name');

                    print_r($array3);

                }
            }


        $aMust = explode(" ", $_GET['q']);
        $searchTermBits = array();
        foreach ($aMust as $term) {
            $term = trim($term);
            if (!empty($term)) {
                $searchTermBits[] = "$term";
            }
        }
        $matches = $searchTermBits;
        $test = array($subarray, $matches);



        foreach ($test as $key => $subarray) {
            foreach ($subarray as $subsubarray) {
                foreach ($matches as $match) {
                    if ($subsubarray == $match) {
                        $finalarr[$key][] = $subsubarray;
                    }
                }
            }
        }
        print_r($finalarr[0]);

        $pindakaas = implode('","',$finalarr[0]);
        echo $pindakaas;

The code works great, it's just that I don't know how to get data from the database into $subarray... I just get Array ( ) Array ( ) ...for $array3

2
  • You can flatten the database array to a one dimensional array and then use array_intersect() on them. php.net/manual/en/function.array-intersect.php Commented Jun 6, 2019 at 23:33
  • But how do you flatten it? I tried it before, but I failed... Commented Jun 6, 2019 at 23:38

1 Answer 1

1

if $array2 is an array of arrays like below

array(Array ( [name] => tesla ) Array ( [name] => bmw ) Array ( [name] => opel ))

you can use the function array_column to get the values from a single column, in this case name.

$array3 = array_column($array2,'name')

the above should give you

array(0=>tesla,1=>bmw,2=>opel)

you can then use array_intersect to compare them

$arraynew = array_intersect($array1,$array3);
Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't work. I think the problem is because I use a while loop to get the data out of the database to put in $array2. So if I follow your code, I get Array ( [name] => tesla) Array ( ) Array ( [name] => bmw) Array ( )

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.