PHP Pushing array into a spessific key - php

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);

Related

Sorting an array of array in 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;
}

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 );

How to assign indexed array values to multidimensional array?

I'd like to replace the values of a multidimensional array with the values of a simple numerically indexed array. The number of values in each array is equal, but they do not have the same keys/indexes due to different array structure.
I had a working solution using array_combine, iteration, and a hashtable but cannot recreate my work. I've tried array_merge, array_combine, and looping through the arrays.
$venue_ids = array(
0 => array(
0 => 2476 ),
1 => array(
0 => 2482,
1 => 2480,
2 => 2463 ),
2 => array(
0 => 2484,
1 => 2476,
2 => 2486,
3 => 2463 ));
$names = array(
0 => 'clubhouse',
1 => 'campground',
2 => 'lake',
3 => 'range',
4 => 'trfields',
5 => 'clubhouse',
6 => 'trfields',
7 => 'range' );
I'd like the result to keep the structure of the first array, and simply substitute its values with those from the second. It should look like this:
$venues = array(
0 => array(
0 => 'clubhouse' ),
1 => array(
0 => 'campground',
1 => 'lake',
2 => 'range' ),
2 => array(
0 => 'trfields',
1 => 'clubhouse',
2 => 'trfields',
3 => 'range' ));
The configuration of $venue_ids will change, including the number of indexes and number of keys/values therein, but there will always be a matching 1-to-1 number of $names. I'd be happy just replacing values or constructing a whole new array matching the structure of $venues_id and working with that.
You should probably ask a new question about the queries to see if you can get the correct result to begin with, but in the meantime you can use array_walk_recursive to substitute the values:
array_walk_recursive($venue_ids, function (&$v) use (&$names) { $v = array_shift($names); });
print_r($venue_ids);
Output:
Array
(
[0] => Array
(
[0] => clubhouse
)
[1] => Array
(
[0] => campground
[1] => lake
[2] => range
)
[2] => Array
(
[0] => trfields
[1] => clubhouse
[2] => trfields
[3] => range
)
)
Demo on 3v4l.org

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);

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