Returning differences in array using count - php

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

Related

Compare the difference of two array index by index

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]

Zero in increment array started at 1

This is my code, it's for have an array :
$zi = '1';
for ($zi = 1; $zi <= $v['Store']['stock']; $zi++) {
$options_array[$zi]= $zi;
}
var_dump($options_array);
Array
(
[0] => 0
[1] => 1
[2] => 2
)
why i have the zero in my result ?
i put $zi at 1, so why ?
As my comment turned out to be correct: The issue is that you already have a property 0 in the $options_array before you even get to the presented code block. You can start with a fresh array using $options_array = [] or $options_array = array() (for older php versions). You can also just remove property 0 with unset($options_array[0]).
In php there are two kinds of arrays, numerically indexed arrays, and associative arrays. Your output seems a little suspect but I can tell you that if all the keys of a PHP array are numeric then the array will be zero based.
I see how you have $zi = '1' above, which is along the lines of what you would have to do to create a one based array, but it would be associative, and you won't be able to simply use the ++ operator. I believe even if you use a string that is a number PHP will still convert to a numerically indexed array. I recommend against trying to implement a one based array, it's insanity.
Hope this page helps http://us2.php.net/manual/en/language.types.array.php
You start setting $options_array at index 1, so index 0 is whatever the default value of the underlying type is. In this case, it's an integer, so the default is 0.

Using array_diff(), how to get difference from array2 instead of array1?

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.

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.

How to sort an array in php

i want the same value has the same index
for example
1 2 2 3 5
after sort:
array(
0=>1
1=>2
1=>2
3=>3
4=>5);
but we can not set duplicate index in the array of php.
There's a sort function in php! ( I answer the topic and not the body, didn't quite follow you there, but here's how you sort in php )
Example
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
Duplicates
In the above example duplicates will just have their own indexes so the array:
5 4 5 1 3 1 2
Will look like this
1 1 2 3 4 5 5
This might not be what you are looking for, what you want is another type of dataset than just a simple array, maybe you want a hashtable or just a linked list on each row.
If you are okay with it, you can remove the duplicates by using array_unique
$newArray=array_unique($arr);
Which would lead to having an array looking like this
1 2 3 4 5
You're right, you can't have duplicate values at the same index in an array - each index in an array has exactly one value.
As to the title of the question, to sort an array in PHP, use sort.
If this doesn't answer what you're trying to ask, you might want to edit your question to make it a bit clearer (the body of the question doesn't seem particularly related to the question title).
Post OP's edit:
You cannot store multiple values at the same key, your output array (array(0=>1, 1=>2, 1=>2, 3=>3, 4=>5);) doesn't really make sense (the key 1 does map to the value 2) in the sorted array. Are you trying to store counts of occurrences of numbers?
e.g. given the input:
1, 2, 2, 3, 5
get the output:
array(1=>1, 2=>2, 3=>1, 5=>1); // there is 1 "1", there are 2 "2"s etc.
Try to use this.
sort($array);
You are looking for array_unique:
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Takes an input array and returns a new array without duplicate values.

Categories