Sorting an array of array in PHP - php

Request your help on sorting an array of array in PHP as below, tried all these function ksort, usort, arsort, krsort nothing seem to be working, any help would be much appreciated.
Original Array
Array
(
[serv1-DB] => Array
(
[2019-11-22] => 0
[2019-11-23] => 0
[2019-11-24] => 0
[2019-11-25] => 1
[2019-11-26] => 3
[2019-11-27] => 3
[2019-11-28] => 3
[2019-11-29] => 3
)
[ser2-DB] => Array
(
[2019-11-25] => 0
[2019-11-24] => 0
[2019-11-22] => 0
[2019-11-23] => 0
[2019-11-26] => 3
[2019-11-27] => 3
[2019-11-28] => 3
[2019-11-29] => 3
)
Output Required
Array
(
[serv1-DB] => Array
(
[2019-11-29] => 3
[2019-11-28] => 3
[2019-11-27] => 3
[2019-11-26] => 3
[2019-11-25] => 1
[2019-11-24] => 0
[2019-11-23] => 0
[2019-11-22] => 0
)
[ser2-DB] => Array
(
[2019-11-29] => 3
[2019-11-28] => 3
[2019-11-27] => 3
[2019-11-26] => 3
[2019-11-25] => 0
[2019-11-24] => 0
[2019-11-23] => 0
[2019-11-22] => 0
)

You want to sort the sub arrays, so you need to loop over each of them (in write mode) and sort the keys in reverse order using krsort:
foreach ($array as &$subArray) {
krsort($subArray);
}
Demo: https://3v4l.org/g8pBu

You have to sort reversely on the arrays like this:
// The arrays
$arrays = array(
"serv1-DB" => array(
"2019-11-22" => 0,
"2019-11-23" => 0,
"2019-11-24" => 0,
"2019-11-25" => 1,
"2019-11-26" => 3,
"2019-11-27" => 3,
"2019-11-28" => 3,
"2019-11-29" => 3
),
"ser2-DB" => array(
"2019-11-25" => 0,
"2019-11-24" => 0,
"2019-11-22" => 0,
"2019-11-23" => 0,
"2019-11-26" => 3,
"2019-11-27" => 3,
"2019-11-28" => 3,
"2019-11-29" => 3
)
);
// The sorting part
foreach($arrays AS $k => $array) {
krsort($array);
$arrays[$k] = $array;
}

Related

Find the difference from a multi-dimencional array to a normal array in php

I have 2 arrays one contains just a list of email address and the other contains emails and names.
The list of emails in array1 are stored in a database and the other array is a list of new and current users.
I have tried using array_diff however this only seems to work if I specify in my loop that I just want emails.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array();
foreach ($array2 as $item) {
$array2emails[] = $item[1];
}
$result = array_diff($array2emails, $array1);
$results gives me
Array
(
[1] => james#jameshouse.com
[2] => marvin#marvinshouse.com
[3] => jane#janeshouse.com
)
however I need to get this:
Array
(
[1] => array(
[0] => james
[1] => james#jameshouse.com
)
[2] => array(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => array(
[0] => jane
[1] => jane#janeshouse.com
)
)
Any help would be much appreciated. Thanks
Since the array returned by array_diff has the same keys as the original array, you can use that to get the elements of the original array back, with array_intersect_key.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array_column($array2, 1);
$keys = array_diff($array2emails, $array1);
$result = array_intersect_key($array2, $keys);
print_r($result);
Result:
Array
(
[1] => Array
(
[0] => james
[1] => james#jameshouse.com
)
[2] => Array
(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => Array
(
[0] => jane
[1] => jane#janeshouse.com
)
)
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$diffs = array();
foreach ($array2 as $item) {
if (!in_array($item[1], $array1)) {
$diffs[] = $item;
}
}
Since you are using a foreach loop why not create the array there?
As long as the arrays are not to long this will word nicely. I even took the original key into the new array. So you will be able to do other comparison methods to it.
$array1 = [
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
];
$array2 = [
0 => [
0 => 'tom',
1 => 'tom#tomshouse.com'
],
1 => [
0 => 'james',
1 => 'james#jameshouse.com'
],
2 => [
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
],
3 => [
0 => 'jane',
1 => 'jane#janeshouse.com'
],
];
$arrayExits = [];
foreach ($array2 as $key => $item) {
if(in_array($item[1], $array1)) {
$arrayExits[$key] = $item;
}
}
print_r($arrayExits);
Use array_filter function.
function filterEmail($value, $key){
global $array1;
return !in_array($value[1], $array1);
}
$emailDiff = array_filter($array2, 'filterEmail' , ARRAY_FILTER_USE_BOTH );
print_r($emailDiff );

Array combine with all as it is like same keys and values

I Have three arrays as array1 array2 array3 follows, I want this combine like as array4.
array1
(
[1869] => 1
[1871] => 1
[0] => 2
[1807] => 1
[1875] => 1
[1811] => 1
[1877] => 1
[1878] => 1
[1879] => 1
[1880] => 1
[1886] => 1
[1850] => 2
[1618] => 3
[1679] => 1
)
array2
(
[1619] => 1
[1625] => 1
)
array3
(
[1111] => 1
[2222] => 1
)
Need Output like:
array4
(
[1869] => 1
[1871] => 1
[0] => 2
[1807] => 1
[1875] => 1
[1811] => 1
[1877] => 1
[1878] => 1
[1879] => 1
[1880] => 1
[1886] => 1
[1850] => 2
[1618] => 3
[1679] => 1
[1619] => 1
[1625] => 1
[1111] => 1
[2222] => 1
)
I want this array1 and array2 combine like array3.Please some one help me.Thanks in advance.
If each array is on a variable you can do this,
$final=$arr1 + $arr2 + $arr3;
THat way you will not loose the keys as well. Check the manual here.
Or
$array1 = array('1869' => 1,'1871' => 1,'0' => 2,'1807' => 1,'1875' => 1,'1811' => 1,'1877' => 1,'1878' => 1,'1879' => 1,'1880' => 1,'1886' => 1,'1850' => 2,'1618' => 3,'1679' => 1);
$array2 = array('1619' => 1,'1625' => 1);
$array3 = array('1111' => 1,'2222' => 1);
$newArray = array_replace( $array1,$array2,$array3);
But I think just using the + is preferred.
print_r($newArray);

How to append this array as it is in php

I need to append this array in php script
.Kindly help to do it perfectly .I tried multiple solution but none resulted to correct solution.
Array(
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
)
to the following array
Array(
[Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
//add the given array here
))
So that the result is
Array([Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
//added here
))
Please use least loops as much as possible.So as to get time feasible solution.Use php array function if possible.
Use foreach to add into the oti index:
<?php
$firstArr = Array(
"Don" => Array
(
"Ab" => 1,
"Ang" => 1,
"Ant" => 2,
"Bo" => 1,
"Ch" => 1,
"Chri" => 2,
"Chri" => 4,
"Deau" => 1,
"Der" => 1,
"Sylveste" => 1,
),
"Lam" => Array
(
"Ab" => 2,
"Ch" => 22,
"Dona" => 1,
"Irw" => 1,
"Kou" => 1,
"Llo" => 1,
"Ro" => 1,
"Shumy" => 1,
),
"Oti" => Array
(
"Ab" => 1,
"Arla" => 1,
"Kour" => 1,
"Osh" => 1,
"Roy " => 1,
"Tim" => 1,
"War" => 1,
//add the given array here
));
$secondArr = Array(
"Donn" => 0,
"Lamo" => 0,
"Otis" => 0,
"Stev" => 0,
"Matt" => 0,
"Samm" => 0,
"Andr" => 0,
"Jerr" => 0,
"Simm" => 0,
"Steph" => 0,
"Fredd" => 0,
"Willi" => 0,
);
foreach ($secondArr as $key => $value) {
$firstArr["Oti"][$key] = $value;
}
print_r($firstArr);
Demo
You can also use array_merge as #Mark Baker said in comments:
$firstArr["Oti"] = array_merge($firstArr["Oti"], $secondArr);
This should solve it, you use the array_merge function to merge the 2 arrays :)
$secondArray['Oti'] = array_merge($secondArray['Oti'], $firstArray);

PHP Pushing array into a spessific key

I have these two 2Dimensional arrays,
$array1
Array(
'week1' => Array (
0 => '2015-06-29',
1 => '2015-06-30',
)
)
$array2
Array(
'week1' => Array (
0 => '2015-07-01',
1 => '2015-07-02',
2 => '2015-07-03',
3 => '2015-07-04',
4 => '2015-07-05',
),
'week2' => Array (
0 => '2015-07-06',
1 => '2015-07-07',
2 => '2015-07-08',
3 => '2015-07-09',
4 => '2015-07-10',
5 => '2015-07-11',
6 => '2015-07-12',
),
)
And this is my expected result,
Array(
'week1' => Array (
0 => '2015-06-29',
1 => '2015-06-30',
2 => '2015-07-01',
3 => '2015-07-02',
4 => '2015-07-03',
5 => '2015-07-04',
6 => '2015-07-05',
),
'week2' => Array (
0 => '2015-07-06',
1 => '2015-07-07',
2 => '2015-07-08',
3 => '2015-07-09',
4 => '2015-07-10',
5 => '2015-07-11',
6 => '2015-07-12',
),
)
i have been trying to use array_push but the array become 3 dimensional instead of joining the same key name. Could you guys halp me out?
Thanks in advance. :D
I think you can use array_merge_recursive (http://php.net/manual/en/function.array-merge-recursive.php)
Just:
array_merge_recursive($array1, $array2)
Just loop through your array, which you want to add and use the key to array_merge() the array, e.g.
foreach($array1 as $k => $v)
$array2[$k] = array_merge($array2[$k], $v);

Walkthrough Entire Multidimensional Array PHP

I have a dummy array, that I want to order.
How can I have following result
with foreach?
with while next()
with RecursiveIterator
with IteratorIterator
which one is the fastest?
Here is array
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
Expected Result
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive is what you're looking for.
Example (not tested):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
Working example

Categories