array sorting using PHP - 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.

Related

Array Intersect in the same order 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 );

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]

PHP Merge two indexed arrays but NOT normally [duplicate]

This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?
Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.

array_intersect_key with three array in php

I have three array from dynamic data in php for example :
array1 = array('10-11-2015'=>23.6,
'25-11-2015'=>48.6,
'12-11-2015'=>14.52);
array2 = array('10-11-2015'=>8.7,
'22-10-2015'=>86.6,
'12-11-2015'=>78.5);
array3 = array('10-11-2015'=>5.8,
'19-09-2015'=>3.6,
'12-11-2015'=>96.4);
I just need common keys from this three array
newarray = array('10-11-2015','12-11-2015');
I use array_intersect_key but it is for only two array.
How I can get it form three or more array ?
Something like this?
array_keys(array_intersect_key($array1, $array2, $array3));

Using php array_multisort twice to sort the same array

I have a large multidimensional array and I want to sort it twice by date using array_multisort and get the last 3 arrays from each sort
I could create a duplicate of the array but it seems a waste when all I want is 3 arrays from it
$rows = array(
array(...),
array(...),
...
);
I create the arrays to be sorted like this
foreach($rows as $key => $row) {
$submit_date[$key] = $row['Submit_Date'];
$view_date[$key] = $row['View_Date'];
}
On this iteration of the sort, everything works as I expect
array_multisort($view_date, SORT_DESC, $rows);
$viewed = array_slice(array_unique($rows, SORT_REGULAR), 0, 3, true);
but on this one which is run straight after, I get different results to what I expect
array_multisort($submit_date, SORT_DESC, $rows);
$unlisted = array_slice(array_unique($rows, SORT_REGULAR), 0, 3, true);
I can't sort on both sort arrays because there will be occasions where $view_date array will have null values.
Is there a way I can use the same array to sort by view date, get the last 3 rows then sort the array by submit date then get the last 3 rows?
It's because your first multisort messed up the order of $rows.
A dummy array should do the trick: $temp = $rows;

Categories