Comparing two arrays with array_diff - php

I have the following code and am trying to compare two array's with array_diff however I keep getting no results. I not sure if it matters, but there are many fields in the array and I really only want to compare 1 field...is this possible? what am I missing?
<?php
$json = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-AZ&back=7&fmt=json");
$json2 = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-NV&back=7&fmt=json");
$array1 = json_decode($json, TRUE);
$array2 = json_decode($json2, TRUE);
if ( $array1 == $array2 ) {
echo 'There are no differences';
}else
var_dump(array_diff($array2, $array1));
echo 'they are different';
?>

You will need to check the arrays against each other:
$Array_1 = array (1,2,3,4,5);
$Array_2 = array(1,2,3,4,5,6);
print_r(array_diff($Array_1,$Array_2));
Will output:
Array
(
)
Whereas:
print_r(array_diff($Array_2,$Array_1));
will output:
Array
(
[5] => 6
)
So this might be a solution:
function ArrayDiff ($Array_1, $Array_2){
$Compare_1_To_2 = array_diff($Array_1,$Array_2);
$Compare_2_To_1 = array_diff($Array_2,$Array_1);
$Difference_Array = array_merge($Compare_1_To_2,$Compare_2_To_1);
return $Difference_Array;
}
print_r(ArrayDiff($Array_1,$Array_2));
Which will output:
Array
(
[0] => 6
)
Putting this into an if statement:
$Differences = ArrayDiff($Array_2,$Array_1);
if (count($Differences) > 0){
echo 'There Are Differences Between The Array:';
foreach ($Differences AS $Different){
echo "<br>".$Different;
}
All the examples and code is based off the arrays at the start ($Array_1 and $Array_2)

$po_line_array=array();
$po_line_clone_array=array();
foreach($cart->line_items as $line_no => $po_line)
$po_line_array[$line_no]=$po_line->labdip_details_id;
print_r($po_line_array,1);
foreach($cart->line_items_clone as $line_no_clone => $po_line_clone)
$po_line_clone_array[$line_no_clone]=$po_line_clone->labdip_details_id;
print_r($po_line_clone_array,1);
$result=array_diff($po_line_clone_array,$po_line_array);
print_r($result,1);
Output:
Array ( [0] => 101 )
Array ( [0] => 101 [1] => 103 )
Array ( [1] => 103 )

Related

Need to get unique values in array

Array
(
)
Array
(
)
Array
(
[0] => 14
)
Array
(
[0] => 14
)
Array
(
[0] => 14
)
Array
(
[0] => 14
)
Array
(
[0] => 14
)
Array
(
[0] => 14
[1] => 12
)
I want this array
Array
(
[0] => 14
[1] => 12
)
Here is my code:
$colorarray = array();
foreach($catIds as $catid){
$colorarray[] = $catid;
}
Need to get unique array values
Thanks
You can use call_user_func_array with array-merge for flatten your array and then use array-unique as:
$res = call_user_func_array('array_merge', $arr);
print_r(array_unique($res)); // will give 12 and 14
Live example 3v4l
Or as #Progrock suggested: $output = array_unique(array_merge(...$data)); (I like that syntax using the ...)
You can always do something like this:
$colorarray = array();
foreach($catIds as $catid){
if(!in_array($catid, $colorarray) {
$colorarray[] = $catid;
}
}
But also this has n*n complexity, So if your array is way too big, it might not be the most optimised solution for you.
You can do following to generate unique array.
array_unique($YOUR_ARRAY_VARIABLE, SORT_REGULAR);
this way only unique value is there in your array instead of duplication.
UPDATED
This is also one way to do same
<?php
// define array
$a = array(1, 5, 2, 5, 1, 3, 2, 4, 5);
// print original array
echo "Original Array : \n";
print_r($a);
// remove duplicate values by using
// flipping keys and values
$a = array_flip($a);
// restore the array elements by again
// flipping keys and values.
$a = array_flip($a);
// re-order the array keys
$a= array_values($a);
// print updated array
echo "\nUpdated Array : \n ";
print_r($a);
?>
Reference link
Hope this will helps you
I have updated your code please check
$colorarray = array();
foreach($catIds as $catid){
$colorarray[$catid] = $catid;
}
This will give you 100% unique values.
PHP: Removes duplicate values from an array
<?php
$fruits_list = array('Orange', 'Apple', ' Banana', 'Cherry', ' Banana');
$result = array_unique($fruits_list);
print_r($result);
?>
------your case--------
$result = array_unique($catIds);
print_r($result);
You can construct a new array of all values by looping through each sub-array of the original, and then filter the result with array_unique:
<?php
$data =
[
[
0=>13
],
[
0=>13
],
[
0=>17,
1=>19
]
];
foreach($data as $array)
foreach($array as $v)
$all_values[] = $v;
var_export($all_values);
$unique = array_unique($all_values);
var_export($unique);
Output:
array (
0 => 13,
1 => 13,
2 => 17,
3 => 19,
)array (
0 => 13,
2 => 17,
3 => 19,
)

Sum arrays in array variable

I have a little problem. Here is the code:
$arr = explode(',', $odluka);
$arr2 = array($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7], $arr[8], $arr[9]);
while ($arrk = current($arr2)) {
if ($arrk == '1') {
$ark = key($arr2);
//print_r($ark);
//echo $arr2[$ark];
$arop = explode(',', $utroseno);
$aropk = array($arop[0], $arop[1], $arop[2], $arop[3], $arop[4], $arop[5], $arop[6], $arop[7], $arop[8], $arop[9]);
$array = array($aropk[$ark]);
print_r($array);
}
next($arr2);
}
Output of $array is
Array ( [0] => 1 ) Array ( [0] => 5 ) Array ( [0] => 10 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 1 ) Array ( [0] => 1 )
How can I merge this values and sum them. I want sum of 1+5+10+4+4+1+1. Thanks!
declare a variable to store sum
iterate over array
-> add value to sum
Here is a simple example how to deal with your output array:
$data = [
[1], [5], [10], [4]
];
$sum = array_sum(array_map(function($elem) { return $elem[0]; }, $data));
var_dump($sum);
You don't need to assign them to another array and loop..you can just sum everything after explode. You just need one line of code for that:
array_sum(explode(',', $odluka));
Then you'll get the sum of all the numbers
Not need using any array and loop.You are using only "array_sum ()" php building function.Like
<?php
$foo[] = "12";
$foo[] = 10;
$foo[] = "bar";
$foo[] = "summer";
echo array_sum ($foo); //same as echo "22";
?>
For more information Read Php Manual link
Use this function
array_sum ($arr);

manipulating with multiple arrays

$newarry = array
(
[0] => 2
[1] => 4
)
$serial_model_itemid = array
(
[0] => 0
[1] => 2
[2] => 1
[3] => 2
)
need to merge both arrays and get like
array
(
[2] => 2 // in 2nd array [1]+[3] as single count
[4] => 1 // in 2nd array [0] + [2] as single count
)
I have tried:
$indent_det_id1 = array_unique($data['indent_detail_id1tabl']);
$serial_model_itemid = array_count_values($data['item_id']);
$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC);
$itmid_zero = array_splice($serial_model_itemid,0);
unset($itmid_zero[0]);
$newarry = array_filter($indent_det_id1);
$serial_model_itemid = array_count_values($data['item_id']);
print_r( $serial_model_itemid);
$a = array_unique( $serial_model_itemid);
print_r( $data['item_id']);
$serial_model_itemid = array_count_values($data['item_id']);
//$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC);
$pos = array_search (0,$serial_model_itemid);
echo "0 founded at ".$pos;
unset($serial_model_itemid[$pos]);
print_r($serial_model_itemid);
print_r( $serial_model_itemid);
$a = array_unique( $serial_model_itemid);
// count items in array
$serial_model_itemid = array_count_values($data['item_id']);
// search the array
$pos = array_search (0,$serial_model_itemid);
// echo where it was found
echo "0 found at ".$pos;
// unset it
unset($serial_model_itemid[$pos]);
// print out for debug
print_r($serial_model_itemid);
// Assign the unique key
$a = array_unique( $serial_model_itemid);
Use the function array_count_values
<?php
$n = array(0,2,1,2);
$newarray = array_count_values($n);
echo '<pre>'.print_r($newarray, true).'</pre>';
?>
Result
Array
(
[0] => 1
[2] => 2
[1] => 1
)
Here's what I found worked:
// count items in array
$serial_model_itemid = array_count_values($data['item_id']);
// search the array
$pos = array_search (0,$serial_model_itemid);
// echo where it was found
echo "0 found at ".$pos;
// unset it
unset($serial_model_itemid[$pos]);
// print out for debug
print_r($serial_model_itemid);
// Assign the unique key
$a = array_unique( $serial_model_itemid);

How To Subtract Values From An Array In PHP?

I am trying to "subtract" the values of an array in php. I used array_diff but it doesn't seem to work for more than one value.
<?php
$array1 = array(1,3,7,10,7);
$array2 = array(1,7);
$result=array_diff($array1,$array2);
print_r($result);
?>
//Output//
Array ( [1] => 3 [3] => 10 )
What I would like to do is return 3,7,10 instead of excluding all 7's. Thanks in advance!
Try:
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
foreach( $array1 as $key => $value ) {
if ($value === $remove ) {
unset($array1[ $key ]);
break;
}
}
}
print_r($array1); // Array ( [1] => 3 [3] => 10 [4] => 7 )
sort($array1)
print_r($array1); // Array ( [0] => 3 [1] => 7 [2] => 10 )
based on thelastshadows post but shorter and may faster because only one foreach
$array1 = array(1,3,7,10,7);
$removals = Array(1,7);
foreach( $removals as $remove ) {
unset($array1[array_search($remove,$array1)]);
}
sort($array1);
print_r($array1);

Merge a string in an array

I have this array:
$fields = $_GET['r'];
Which has some ids, for example:
Array (
[0] => 3134
[1] => 3135 )
and then I have this string in $tematiche:
3113,3120
How can I marge this string in the first array? Also, how can I remove the equals id (if any)?
Try,
$fields = array_unique(array_merge($fields,explode(",",$tematiche)));
echo "<pre>";
print_r($fields);
Try this :
$fields = $_GET['r'];
$string = '3113,3120';
$array = explode(",",$string);
$res_array = array_unique(array_merge($fields,$array));
echo "<pre>";
print_r($res_array);
Output :
Array (
[0] => 3134
[1] => 3135
[2] => 3113
[3] => 3120
)
A combination of explode(), array_merge() and array_unique() would be suitable.
// Make $tematiche into an array
$tematiche_array = explode(',', $tematiche);
// Merge the two arrays
$merged_array = array_merge($fields, $tematiche_array);
// Remove all duplicate values in the array
$unique_array = array_unique($merged_array);
Please try like this. This should work for you.
$a = explode(',', '3113,3120');
print_r(array_merge($fields,$a));

Categories