mysql - php array matching between two arrays -


$a = array('one'=> val, 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');  $b = array('four'=> val, 'one'=> val, 'eight'=> val, 'five'=> val)  

i have 2 arrays shown above.

need result this:

will output:

'one' => val 'two' => 0 'three' => 0 'four' => val 'five' => val 'six' => 0 'seven' => 0 'eight' => val 'nine' => 0 'ten'  => 0 

is there easy way this?

any

actually array_intersect this. creates new array out of these 2 arrays, holding values available in both arrays.

http://php.net/manual/en/function.array-intersect.php

array_intersect() returns array containing values of array1 present in arguments. note keys preserved.

edit: should rather use array-diff,

compares array1 against 1 or more other arrays , returns values in array1 not present in of other arrays.

http://php.net/manual/en/function.array-diff.php

the code output described above like:

<?php $a = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten');  $b = array('four', 'one', 'eight', 'five');  //find matches , not matches $matches = array_intersect($a, $b); $dontmatches = array_unique(array_merge($a, $b));  //set dont matches 0 for($i = 0; $i<count($dontmatches);$i++)     $dontmatches[$i] = 0;  $final = array_merge($matches, $dontmatches); print_r($final); ?> 

i used both described functions above plus following:

http://php.net/manual/en/function.array-unique.php

http://php.net/manual/en/function.array-merge.php


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -