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
Related
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;
}
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 );
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);
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);
I have this array (it's just a part of it). 6 = question ID, optionIDs = possible answers.
Array
(
[3] => Array
(
[0] => 6
[1] => Array
(
[0] => Array
(
[optionID] => 16
[isCorrect] => 0
)
[1] => Array
(
[optionID] => 14
[isCorrect] => 1
)
[2] => Array
(
[optionID] => 15
[isCorrect] => 0
)
[3] => Array
(
[optionID] => 17
[isCorrect] => 0
)
)
)
[7] => Array
(
[0] => 6
[1] => Array
(
[0] => Array
(
[optionID] => 16
[isCorrect] => 0
)
[1] => Array
(
[optionID] => 15
[isCorrect] => 0
)
[2] => Array
(
[optionID] => 17
[isCorrect] => 0
)
[3] => Array
(
[optionID] => 14
[isCorrect] => 1
)
)
)
)
I'm trying to merge redundant questions (6 and 6) with array_map:
$unique = array_map('unserialize', array_unique(array_map('serialize', $quizQuestionArray)));
And it works as long as optionIDs are in the same order. But in some cases (like here) they are shuffled (16,14,15,17) (16,15,17,14). Is there a way to keep them shuffled and remove duplicate questions?
array_map-serialize is a pretty crude way to deduplicate an array. You should be using something like this instead:
$dupeIds = [];
$array = array_filter($array, function ($item) use (&$dupeIds) {
$keep = !isset($dupeIds[$item[0]]);
$dupeIds[$item[0]] = true;
return $keep;
});
You will need to sort them to the same order before applying you array_map() function. You can use the uasort() function and supply your own comparison function like this:
// Example array
$array = array(
3 => array(
0 => 6,
1 => array(
0 => array(
'optionID' => 16,
'isCorrect' => 0
),
1 => array(
'optionID' => 14,
'isCorrect' => 1
),
2 => array(
'optionID' => 15,
'isCorrect' => 0
),
3 => array(
'optionID' => 17,
'isCorrect' => 0
),
)
),
7 => array(
0 => 6,
1 => array(
0 => array(
'optionID' => 16,
'isCorrect' => 0
),
1 => array(
'optionID' => 15,
'isCorrect' => 0
),
2 => array(
'optionID' => 17,
'isCorrect' => 0
),
3 => array(
'optionID' => 14,
'isCorrect' => 1
),
)
)
);
// You can supply parts of an array to uasort()
// uasort() will modify your array but keep your keys.
uasort($array[3][2], 'sort_by_optionid');
uasort($array[7][3], 'sort_by_optionid');
function sort_by_optionid($a, $b) {
if ($a['optionID'] === $b['optionID']) {
return 0;
} else if ($a['optionID'] > $b['optionID']) {
return 1;
} else {
return -1;
}
}
// Done.
Now the keys are preserved and you can easily array_map() to find the duplicates and then sort again back to the original state according to the keys. E.g. with uksort()