Compare the difference of two array index by index - php

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]

Related

Returning differences in array using count

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

array sorting using PHP

Is there a way to use descriptors in PHP to do the following sort:
Array 1:
$array1 = ["blah", "quack", "yot"];
Array 2
$array2 = [1,9,6]; Essentially, I'm trying to describe blah with 1, quack with 9, and yot with 6. Basically, there are a total of 1 blahs, 9 quacks, and 6 yots. When I sort the arrays, I want to produce the following arrays:
Array 1 Sorted:
$array1 = ["quack","yot","blah"];
Array 2 Sorted:
$array2 = [9,6,1];
array_multisort($array2, SORT_DESC, $array1);
http://ideone.com/4Afyu
http://php.net/array_multisort
You can sort arrays with usort.
Well, you can restructure your data into an associative array.
$var = array ('blah'=>1, 'quack'=>9, 'yot'=>6);
So that your data is in one array. Then you can use asort or any php sorting function to manipulate the array.

Looking up keys for a multi-dimensional array

I have an array with a key and 3 values (day, start_time, end_time). I want to keep adding certain entries into this array while making sure each entry is unique. That means that every time I try to add an item into the array, I want to make sure it does not already exist in it. If it does exist, I want to be able to find the key that indicates that entry.
For example, this is the pre-existing array:
$array [0][0] = Monday
$array [0][1] = 2
$array [0][2] = 4
$array [1][0] = Tuesday
$array [1][1] = 3
$array [1][2] = 5
If I try to insert (Wednesday, 3, 5), then it should make the entry in the index 2.
If I try to insert (Monday, 2, 4), I need to be able to know that it is already in there and is indexed by 0.
How do I go about doing this?
I agree with the other answers here — it might be better to restructure your array so that there is no need to worry about duplication at all.
If you want to keep your current structure, however: use array_search.
$array = ...
$unique_check = array_search(array('Monday', 2, 4), $array);
if ( $unique_check === false )
// add to array
else
// $unique_check = the array key at which the existing matching element is located
Why not organize the array this way?
$array [Monday][0] = 2
$array [Monday][1] = 4
$array [Tuesday][0] = 3
$array [Tuesday][1] = 5

Splitting an array equally:

I have the following array:
$array = array(1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,1,0,1,0,0,1,0,1);
I want split it up into individual arrays so that each array contains seven or less values.
So for example the first array will become:
$one = array(1,0,0,0,1,1,1)
$two = array(1,0,1,1,1,1,0)
$three = array(1,1,0,1,0,0,1);
$four = array(0,1);
Also how would you count the number of times 1 occurs in array one?
array_chunk() is what you are looking for.
$splitted = array_chunk($array, 7);
For counting the occurences I would be lazy. If your arrays only contain 1s or 0s, then a simple array_sum() would do:
print array_sum($splitted[0]); // for the first chunk
I want split it up into individual arrays so that each array contains seven or less values.
Use array_chunk(), which is made expressly for this purpose.
Also how would you count the number of times 1 occurs in array one?
Use array_count_values().
$one = array(1,0,0,0,1,1,1);
$one_counts = array_count_values($one);
print_r($one_counts);
// prints
Array
(
[0] => 3
[1] => 4
)
Assuming you want to preserve the contents of the array, I'd use array_slice() to extract the needed number of elements from the array, incrementing the '$offset' by the required count each time until the array was exhausted.
And as to your second question, try:
$num_ones=count(preg_grep(/^1$/,$array));

Checking the element whether it is in array and getting remaining values of array

I have an array(1,3,5,7,10) and an variable containing value say a=1 Now i want to get the result as array(3,5,7,10) ie. getting the values other than that variable value
array_diff($original_array, array($a));
use array_diff: http://php.net/manual/en/function.array-diff.php
$arr1 = array(1,3,5,7,10)
$a = 1
$arr2 = array_diff($arr1, array($a))

Categories