I'm trying to use array_diff like so. Here are my two array outputs:
List 1 Output
Array ([0] => 0022806 )
List 2 Output
Array ([0] => 0022806 [1] => 0023199 )
PHP
$diff = array_diff($list_1, $list_2);
print "DIFF: " . count($diff) ."<br>";
print_r($diff);
The Output is:
DIFF: 0
Array ( )
Any idea what I'm doing wrong? Why is 0023199 not returned?
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays
try;
$diff = array_merge(array_diff($list_1, $list_2), array_diff($list_2, $list_1));
print "DIFF: " . count($diff) ."<br>";
print_r($diff);
From the docs:
Returns an array containing all the entries from array1 that are not
present in any of the other arrays.
If you only want to check whether they are the same, you can use $list1 == $list_2
Per the documentation, the values of the second array are subtracted from the first one. Or, to put it another way, you start with the first array, and then remove all the values that appear in the second array. That would correctly yield an empty array that you see above
You might want to play around with intersection, that might help you get what you want.
Related
I am getting an array in through form .I am using explode method to separate my values
$getData =$_POST['jasonHandle'];
print_r($getData); // output incoetp,11,12,13,#,101,11,12,#
here # is used for separation
$arr=explode(",#,",$getData);
print_r($arr); // Array ( [0] => incoetp,11,12,13 [1] => 101,11,12,# )
The above array may contain any number of elements(in this case there are tow arrays) .
I need to delete this # element which is appearing at last of last array
I tried unset() and array_pop () ,but its deleting entire last array .
Also COUNT() function is not working for inner array
echo count($arr[1]); // Parameter must be an array or an object that implements Countable
I have been using count function for 2 d array and never had any issue .why is this happening?
you can use chop() function
chop()--> The chop() function removes whitespaces or other predefined characters from the right end of a string
$arr[1]=chop($arr[1],",#");
Delete it by using rtrim
$arr[1] = rtrim($arr[1], '#');
I have two arrays which look like this :
$has_end_date = ['value123' , 'value132'];
$has_no_end_date = ['value123' , 'value256'];
Now I wish to return the unique values from $has_no_end_date that doesn't appear in $has_end_date.
This is what I tried :
I tried to merge the two arrays together and then find the unique values. But that did not work, i will get the unique of both of them.
I am only looking for the unique different values from $has_no_end_date array.
You need to use array_diff() to get the difference and array_unique() to return unique values.
$difference = array_unique(array_diff($has_no_end_date, $has_end_date));
print_r($difference);
Output:-https://3v4l.org/3oLHA AND https://3v4l.org/7aEFW
You can use array_diff
$a1 = ['value123' , 'value132'];
$a2 = ['value123' , 'value256'];
$diff = array_diff($a2, $a1);
echo '<pre>';
print_r($diff);
Output :-
Array
(
[1] => value256
)
You can use array_diff and array_unique if you want to fetch that data,
print_r(array_unique(array_diff($has_no_end_date, $has_end_date)));
Demo.
I want to know how can I compare two arrays if there's a differences between their values in each indexes. I have this two arrays for example.
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"]..
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"]..
The result that I want to get would be 1 because only index 1 is not equal with the index 1 of the second array.
I tried using array_diff but the result is always 0. I want to compare each array by indexes and values and return the number of differences on each.
Thank you
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"];
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"];
print_r(array_diff_assoc($arr1, $arr2)); // output: [1 => "B"]
Is this what you want? If you only need an index, you can do this
print_r(array_keys(array_diff_assoc($arr1, $arr2))); // output: [1]
Im currently having issue's with an algorithm im creating in php using 2 separate 2 dimensional arrays.
I want to Find how different 1 array is from the other and return the value.
For now my issue is that with the algorithm im using 'should' return the same value for the 2 arrays im comparing...but currently it does not.
The values in the arrays are as follows:
$array1 contains ['index','index','index','index','index']
$array2 contains ['index','java','index','none']
When i run my algorithm :
function arrayDifference($array1,$array2)
{
if (is_array($array1)&&is_array($array2)){
$result = array_diff($array1, $array2);
$value=max(count($array1),count($array2));
$result=$value-count($result);
return $result;
}
}
I get these results:
When $array1 is passed in first the result is : 2.
When $array2 is passed in first the result is : 4.
The issue is that since im using the same arrays, should the resulting difference not be the same when both are passed in irrelevant of the parameter order?
Any help would be appreciated, thanks.
Update/Note ---------------------------------------
After printing out array_diff the first values returned from $array1 being passed in first is :
'( [1] => java [3] => none )'
and for array2 passed in first:
'Array ( ) 1'
See how array_diff() works.
In 1st case of $array1(first array in array_diff), there is only "index" value which is present in $array2 and therefore count(array_diff) is 0.
In 2nd case of $array2(first array in array_diff), there are 2 values (index,java) which are not present in $array1 therefore count(array_diff) is 2.
Thats why in first case, you get 5-0 = 5
Thats why in second case, you get 5-2 = 3
All I need to two is remove the final two elements from an array, which only contains 3 elements, output those two removed elements and then output the non-removed element. I'm having trouble removing the two elements, as well as keeping their keys.
My code is here http://pastebin.com/baV4fMxs
It is currently outputting : Java
Perl
Array
I want it to output:
[One] => Perl and [Two] => Java
[Zero] => PHP
$last=array_splice($inputarray,-1);
//$last has now key=>value of last element
$middle=array_splice($inputarray,-1);
//$middle has now key=>value of middle element
//$inputarray has now only key=>value of first element
It seems to me that you want to retrieve those two values being removed before getting rid of them. With this in mind, I suggest the use of array_pop() which simultaneously returns the last item in the array and removes it from the array.
$val = array_pop($my_array);
echo $val; // Last value
$val = array_pop($my_array);
echo $val; // Middle value
// Now, $my_array has only the first value
You mentioned keys, so I assume it's an associative array. In order to remove an element, you can call the unset function, like this:
unset ($my_array['my_key'])
Do this for both elements that you want to remove.
array_pop will do it for you quite easily:
$array = array( 'one', 'two', 'three');
$last = array_pop( $array); echo $last . "\n";
$second = array_pop( $array); echo $second . "\n";
echo $array[0];
Output:
three
two
one
Demo