What is the most efficient way in initializing large amount of multidimensional array in PHP?
example: I'm going to create 100 Multidimensional array that look like this:
Array
(
[1] => Array
(
[multi] => 1
)
[2] => Array
(
[multi] => 2
)
[3] => Array
(
[multi] => 3
)
[4] => Array
(
[multi] => 4
)
[5] => Array
(
[multi] => 5
)
.......
)
Currently, I'm using this code to create the array shown above:
// 100 arrays
for($i=1; $i<=100; $i++){
$array[$i]['multi']=$i;
}
I also found an alternative way by using array_fill() and array_fill_keys(), but It only allows the same value to be initialized in an array:
$array = array_fill(1, 100, array_fill_keys(array("multi"), "value_here"));
QUESTION: Is there a more efficient way in initializing this kind of array in terms of speed?
You could use array_map over the range of values you want:
$array = array_map(function ($v) { return array('multi' => $v); }, range(0, 5));
print_r($array);
Output:
Array
(
[0] => Array
(
[multi] => 0
)
[1] => Array
(
[multi] => 1
)
[2] => Array
(
[multi] => 2
)
[3] => Array
(
[multi] => 3
)
[4] => Array
(
[multi] => 4
)
[5] => Array
(
[multi] => 5
)
)
If you don't want the 0 element, just unset($array[0]);
Demo on 3v4l.org
Related
I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));
I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');
This is an issue I haven't come across before, and there doesn't appear to be a function for this.
I'm trying to sort the following multi dimensional array by its keys
Array (
[hiphop] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[rnb] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array ( )
[reggae] => Array ( )
[trap] => Array ( )
)
From this arrays values
Array (
[0] => hiphop
[1] => dubstep
[2] => reggae
[3] => trap
[4] => rnb
[5] => rnb
)
Has anyone attempted this before or know a workaround?
An explanation would be great as I'm new to multi dimensional arrays
Many thanks in advance if you can help!
The final output would match the value organsiation from the non multi dimensional array like so
Array (
[hiphop] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array ( )
[reggae] => Array ( )
[trap] => Array ( )
[rnb] => Array (
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
)
The easiest Way would be to "flip" the array definining the sorting - then grab the values matching the new keys in the other array:
$s = array ("hiphop","dubstep","reggae","trap","rnb");
$target = array_flip($s);
foreach($target AS $key => &$value){
$value = $array_containing_unsorted_values[$key];
}
Note: does not work if the same value appears twice in the sorting array - but that never should happen, cause does not make sence for a sorting-definition.
Should be way faster than using array_search 2 times within each sorting comparission.
result:
Array
(
[hiphop] => Array
(
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
[dubstep] => Array
(
)
[reggae] => Array
(
)
[trap] => Array
(
)
[rnb] => Array
(
[0] => 2123
[1] => 5683
[2] => 2345
[3] => 4567
)
)
You can use uksort for that, which lets you sort by key, based on a user supplied comparison function. Using that function, you can make a function yourself, so you can hide the complex functionality in an easy one.
For instance, you want a function like ksort, only you want to specify an external array of keys, so such a function could look like this:
bool ksort_ext(&$array, $keys)
For the implementation, you can wrap uksort, like so:
function ksort_ext(&$array, $keys)
{
return uksort($array, function($a, $b) use ($keys) {
// Result of callback is based on the position of $a and $b in $keys
return array_search($a, $keys) - array_search($b, $keys);
});
}
Then, you can call it like you would call other sort functions, only with the external array of keys as a second parameter.
$array1 = array(
'hiphop' => array(1,2,3),
'rnb' => array(1,2,3),
'dubstep' => array(1,2,3),
'reggae' => array(1,2,3),
'trap' => array(1,2,3));
$array2 = array('hiphop', 'dubstep', 'reggae', 'trap', 'rnb');
var_dump(ksort_ext($array1, $array2)); // Should return true
var_dump($array1); // Should display the ordered array,
Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);
I have a multi-dimensional array and what i want is in each second degree of array values limit should be set to 4 only. Like i have a array of type:
Array
(
[test2] => Array
(
[0] => Array
(
[application_id] => 405275016
)
[1] => Array
(
[application_id] => 405275016
)
[2] => Array
(
[application_id] => 303198288
)
[3] => Array
(
[application_id] => 303841592
)
)
[test3] => Array
(
[0] => Array
(
[application_id] => 289267216
)
[1] => Array
(
[application_id] => 303198216
)
[2] => Array
(
[application_id] => 405275016
)
[3] => Array
(
[application_id] => 303198288
)
[4] => Array
(
[application_id] => 303841592
)
[5] => Array
(
[application_id] => 311430400
)
[6] => Array
(
[application_id] => 318096216
)
[7] => Array
(
[application_id] => 320256352
)
)
)
and what i want that if the inner arrays value exceed 5 count it should not add any further values to it. i have to format this above array to something like:
Array
(
[test2] => Array
(
[0] => Array
(
[application_id] => 405275016
)
[1] => Array
(
[application_id] => 405275016
)
[2] => Array
(
[application_id] => 303198288
)
[3] => Array
(
[application_id] => 303841592
)
)
[test3] => Array
(
[0] => Array
(
[application_id] => 289267216
)
[1] => Array
(
[application_id] => 303198216
)
[2] => Array
(
[application_id] => 405275016
)
[3] => Array
(
[application_id] => 303198288
)
[4] => Array
(
[application_id] => 303841592
)
)
)
here in second array array last 3 arrays were truncated in order to take only 5 count as a value. I have tried many a methods but none achieved this.
Any idea on how can i achieve this will be highly appreciated ??
or you can use the array_slice function like this:
foreach ($array as $key=>$value)
{
$array[$key] = array_slice($value, 0, 5);
}
$array = array_map(function ($arr) { return array_slice($arr, 0, 5); }, $array);
Note that this uses PHP 5.3+ syntax.
If you actually want an array (or something like it) that cannot hold more than 5 elements, you'll need to implement a custom class (possibly by extending ArrayObject) and/or play around with SplFixedArray.
foreach ($array as $k => $v) { // Loop outer array
for ($i = 5; isset($array[$k][$i]); $i++) { // Loop inner array, starting at element #5
unset($array[$k][$i]); // unset all elements >=5
}
}
PHP has no ready functionality for this behavior, you'll have to make your own function for adding values to the array to achieve this.
Ex:
function array_add_value($array, $value) {
if(count($array) < 5) {
$array[] = $value;
}
}
If you wish to manipulate an existing array that already has too many elements, you will have to make a similar function for that
Ex:
function fix_array($array) {
if(count($array) > 5) {
for($i=4;$i<count($array);$i++) {
unset($array[$i]);
}
}
}