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]);
}
}
}
Related
I need some help on below array formation. I want to create a custom array using 2 arrays.
This is my first array :-
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[1] => Array
(
[0] => 25
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
This is my second array :-
Array
(
[0] => Array
(
[0] => 24
)
[1] => Array
(
[0] => 26
)
)
I want final array as below. Can someone please suggest how to form this array.
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
I have used below but I want it without foreach.
$finalArray = array();
foreach ($secondArray as $key => $value) {
$key = array_search($value[0], array_column($firstArray, 0));
$finalArray[] = $firstArray[$key];
}
You can use array_filter():
$finalArray = array_filter($firstArray,
fn($item) => in_array($item[0], array_column($secondArray, 0))
);
It could be more efficiant to compute allowed values first:
$allowedValues = array_column($secondArray, 0);
$finalArray = array_filter($firstArray, fn($item) => in_array($item[0], $allowedValues));
Before PHP 7.4, you have to use use() to pass variable to the anonymous function:
$finalArray = array_filter($firstArray,
function($item) use($secondArray) {
return in_array($item[0], array_column($secondArray, 0));
}
);
I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
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
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');
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);