Array Intersect in the same order PHP - php

I have 2 arrays:
$array1 = array("1","2","3","4","5");
$array2 = array("7","2","3","1","5");
What I want to do is match the values in $array1 and $array2 strictly in order.
The desired output should be 2, 3 and 5 since 1 is not in the same order. I've tried array_intersect but it only matches array values and not taking account their order.
Thanks in advance.

You can use array_intersect_assoc as below:
$result = array_intersect_assoc($array1 , $array2 );

Related

How to merge two array and take only value in first array if there is no matching value in second array

I have two arrays :
$array1 = [460,471];
$array2 = [193,42,471];
I want to take the value only in $array1 if there is no same value in $array2, if there is a same value in $array2 filter it out.
Expected output if no same value available in $array2:
$output = [460, 471]
Expected output is there is same value in $array2:
$output = [460]
Try array_diff() function
$array1 = [460,471];
$array2 = [193,42,471];
$filtered = array_diff($array1,$array2);
print_r($filtered);
You can achieve the same with the help of Hashing pretty easily.
First, try to map the values of array2 into a Hash.
For example, each element of $array2 = [193,42,471]; will have a value marking their presence as true.
Now, for each element is $array1, check if it has a value in the Hash. If it has one, then skip it.
So after the check, you will get all the values that are not present in the second array.

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]

How to get unique value from array in PHP?

I have two array inputs like this :
$array1 = [1,2,3,4,6];
$array2 = [1,3];
$output = array_merge(array_diff($array1,$array2),array_diff($array2,$array1));
Now I want to check array1 with array 2 and eliminate 1 and 3 in $array1
and the output I am expecting is
$output = [2,4,6];
but in this method I get some bugs, when array2 have single value e.g.: $array2 = [1]; , $array1 = [1,2,3,4,6]; the output should be $output = [2,3,4,6];. But I am getting $array1 all values [1,2,3,4,6];
Simple :)
(Just un-complicate your code and you don't need anything new for that)
<?php
$array1 = array(1,2,3,4,6);
$array2 = array(1,3);
$result = array_diff($array1, $array2);
print_r($result);
?>
Demo
In Your style it can even be a one liner :P
<?php print_r(array_diff(array(1,2,3,4,6), array(1,3))); ?>

How do you get the top 10 items in an array (PHP)?

What is the best way to get the top 10 items in an array, I have an array that has several hundred items and I would like to get the top 10 items (most duplicated items) from the array using PHP, any suggestions?
This should do the trick:
$inputArray = array('orange','banana', 'banana', 'banana', 'pear', 'orange', 'apples','orange', 'grape', 'apple');
$countedArray = array_count_values($inputArray);
arsort($countedArray);
$topTen = array_slice($countedArray, 0, 10);
The above will return the array in order of the items with the most occurrences.
Try using php's array_count_values() to get the number of occurrences of each value in the array, in conjunction with arsort() to sort the array by the highest frequency values. Then you can take the top 10 most frequent values of the array with array_slice().
$dataArr = array('test', 4, 15.2, ...); // Input array with all data
$frequencies = array_count_values($dataArr);
arsort($frequencies); // Sort by the most frequent matches first.
$tenFrequencies = array_slice($frequencies, 0, 10, TRUE); // Only get the top 10 most frequent
$topTenValues = array_keys($tenFrequencies);
Note: We need to use array_keys() to get the final values because array_count_values() "returns an array using the values of the input array as keys and their frequency in input as values."

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.

Categories