Currently, lets assume I have two arrays as follow:
$values1= array(1, 2, 3);
$values2= array(1, 3, 4);
If I compare both arrays in loop I would be able to differentiate which array elements of first array is not being compared. For example :
$data=array();
foreach($values1 as $value) {
if(! in_array($value, $values2 )) {
$data[]=$value;
}
}
In this way i would be figure out that 2 is the value which is not in the $value2 array but at the same time i also wanted to know what are the other extra elements which are not being compared in $valnues2 array such as 4 in this case.
Any help will be highly appreciated.
I hope you don't mind using a function, but you could merge them both and compare them to the ones with their similar values to get the differences. Like this:
$values1 = array(1, 2, 3);
$values2 = array(1, 3, 4);
$others = array_diff(array_merge($values1, $values2), array_intersect($values1, $values2));
echo '<pre>';
print_r($others);
Should output something like:
Array
(
[1] => 2
[5] => 4
)
array_diff() only checks for elements existing in arg 1 that don't exist in arg 2, one way to get around this is to check both ways and merge the results:
function array_diff_both($a, $b) {
$d1 = array_diff($a, $b);
$d2 = array_diff($b, $a);
return array_merge($d1, $d2);
}
print_r(array_diff_both($values1, $values2));
Example:
Array
(
[0] => 2
[1] => 4
)
Looking forward, it might be best to pass arrays in as an array so you could expand this function's use with the number of your arrays.
Related
I have this dynamic multiple arrays that I need to combine in one array and serialized them. The problem is I need to keep both key and value.
$arr = array($bet_option_id => $bet_option_name);
Here i need to keep both bet_option_id AND bet_option_name. Then this result output:
Array ( [997650802] => Over 2.5 )
Array ( [997650807] => Yes )
This need to be simply
Array
(
[997650802] => Over 2.5
[997650807] => Yes
)
As it's dynamic, sometimes not comes with just single array so apparently I couldn't get it working. I need to retrieve both bet_option_id & bet_option_name. Tried something like this:
$arr = array($bet_option_id => $bet_option_name); //This is where all array keys, values are stores
$result = array();
foreach ($arr as $array) {
$result = array_merge($result, $array);
}
Any inputs will be nice.
Rather than create individual arrays like...
$arr = array($bet_option_id => $bet_option_name);
If you first create an empty array ( like you do with $result)
$arr = array();
and then add each item in using
$arr[$bet_option_id] = $bet_option_name;
Then you don't need to manipulate the array after - just create it as you want it in the first place.
You could either do like Nigel Ren suggested which is the most elegant solution
In case that you do not have arrays that their keys are entirely numeric you may use array_merge. The quote following is from PHP array-merge
Example #2 Simple array_merge() example
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
Don't forget that numeric keys will be renumbered!
Array
(
[0] => data
)
Alternatively you can always join arrays together like this
$a1 = [ 997650802 => 'Over 2.5' ];
$a2 = [ 997650807 => 'Yes' ];
var_dump( $a1 + $a2 ); // result is [997650802 => 'Over 2.5',997650807 => 'Yes']
You can check more about Array Types and Array Operators
I am creating a faceted search, and I'm am trying to use array_intersect to compare the arrays and find the inputs that match.
The problem is that I will have a variable amount of arrays at anytime depending on what filters the user has selected:
$array_1, $array_2, $array_3 etc...
How do I create an array_intersect function that is dynamic in this sense?
This is what I've tried:
$next_array = 0;
for($i = 0; $i < $array_count; $i++) {
$next_array++;
if ($i == 0) {
$full_array = ${array_.$i};
} else {
if(!empty(${cvp_array.$next_array})) {
$full_array = array_intersect($full_array, ${cvp_array_.$next_array});
}
}
}
------------- EDIT -------------
I'll try to narrow down my goal a bit more:
If the user clicks three filters, this results in three arrays being created with each having individual results:
Array_1 ( [0] => 2, [1] => 4, [2] => 6 )
Array_2 ( [0] => 1, [1] => 4, [2] => 6 )
Array_3 ( [0] => 6, [1] => 7, [2] => 8 )
I need code that will find the number that is in ALL of the arrays. And if there is no common number then it would end as false or something. In the case above, I'd need it to retrieve 6. If it was only the first two arrays, it would return 4 and 6.
Try this:
$fullArray = array($array1, $array2, $array3...);
call_user_func_array('array_intersect', $fullArray);
One can use:
$intersect = array_intersect(...$fullArray);
First of all, turn those arrays into an array of arrays. Then you can use array_reduce combined with array_intersect to reduce a variable amount of arrays down to one.
You can turn those array to a single array named $total_array by using array_combine(),
then use array_intersect($full_array, $total_array). I hope this useful
I have 2 arrays:
$array1 = array(1,2,3,4,5);
$array2 = array(3,4,5,6,7);
Is there any PHP function that does this?
$finalArray = unknown_php_function($array1,$array2);
// result: $finalArray = array(3,4,5);
It merges both arrays and removes values which aren't present in both arrays. Do I have to build a foreach cycle or is there an easier way? Thanks
You want array_intersect for this, basically the intersection of two sets (arrays, in this case), just like in school. :-)
You're looking for array_intersect(). Here is a demo:
$array1 = array(1,2,3,4,5);
$array2 = array(3,4,5,6,7);
$finalArray = array_intersect($array1,$array2);
print_r($finalArray);
Outputs:
Array
(
[2] => 3
[3] => 4
[4] => 5
)
I have an array something like below.
Array
(
[0] => 100
[1] =>
[2] => 107
[3] => 109
)
I may get empty value more than one also. I just wanted to move the empty values to down.
Thanks for reading my question and waiting for your valuable answer.
I'd just sort it in reverse order
arsort($myarray, SORT_NUMERIC);
You'd end up with the values:
109, 107, 100, <blank>
Edit: to get the values in the order specified in the below comment:
// initial data
$a = (100, '', 107, 109);
// define functions to detect blank/notblank
function isBlank($var) {
return $var == '';
}
function isNotBlank($var) {
return !isBlank($var);
}
// get two arrays, one containing blanks, one containing numbers
$blanks = array_filter($a, 'isBlank');
$notBlanks = array_filter($a, 'isNotBlank');
// reverse sort the numbers
arsort($notBlanks, SORT_NUMERIC);
// merge the arrays
$output = array_merge($notBlanks, $blanks);
array_merge documentation
I have two arrays of values that I would like to combine - but the only methods that PHP provides seem to combine by key instead of value. Here is a hack that I was able to use to get this to work, but I am wondering if there is a better method or a native function that I have missed. It's been a while since I last used arrays and it seems like there is an easy answer to this.
//Input arrays that we want to combine into one array
$array = array(2, 3, 4, 5);
$array2 = array(5, 6, 1);
//Flip values and keys
$array = array_flip($array);
$array2 = array_flip($array2);
//Combine array
$array3 = $array2 + $array;
//flip array keys back to values
$array3 = array_keys($array3);
//Optional sort
sort($array3);
print_r($array3);
Which returns the combined values of the two arrays:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Not entirely sure what you are trying to accomplish. I am assuming you are trying to combine 2 arrays without having any duplicates. If this is the case then the following would work
$newarr = array_unique(array_merge($array, $array2));