I've got an array of input data and I need to find the max and min values. The POST array could look like any of the following, depending on the options the user selected:
[a] => Array
(
[0] => 2
)
[a] => Array
(
[0] => 2
[1] => 4
[2] => 7
)
[a] => Array
(
[0] => 2
[1] => 4
[2] => Array
(
[0] => 7
)
)
I had it working by sorting the array and grab the min and max values when 'a' was always a single-dimensional array, but since we've added the option for it to be multidimensional I'm stuck.
I would use an Iterator: http://php.net/spl.iterators.php
function array_max($arr) {
$max = null;
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)) as $value) {
if ($max === null || $value > $max) {
$max = $value;
}
}
return $max;
}
I think you can figure out how to do array_min() on your own.
Related
I need to merge 2 arrays and i have found lot of examples here on stackoverflow, but nothing has worked for me, in my case, so i explain my case:
Arrays (can be one, two, or three, or more...):
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png-
[2] => name-file_icon_003_00.png- )
Array ( [0] => rel
[1] => rel
[2] => rel )
Or can be two:
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png- )
Array ( [0] => rel
[1] => rel )
Or one:
Array ( [0] => name-file_icon_001_00.png- )
Array ( [0] => rel )
Need to insert the relative value "[0] => rel" with "[0] => name-file_icon_001_00.png-"
Expected result (merged):
Array ( [0] => name-file_icon_001_00.png-rel
[1] => name-file_icon_002_00.png-rel
[2] => name-file_icon_003_00.png-rel )
Reading around the web, seems that not exist a native function for make this.
Please, hope in your help :)
A simple foreach loop using the index and value parameters will do it in no time
Example
$a1 = ['name-file_icon_001_00.png-',
'name-file_icon_002_00.png-',
'name-file_icon_003_00.png-'
];
$a2 = ['rel1','rel2','rel3'];
foreach ($a1 as $i => $v){
$new[] = $v . $a2[$i];
}
print_r($new);
RESULT
Array
(
[0] => name-file_icon_001_00.png-rel1
[1] => name-file_icon_002_00.png-rel2
[2] => name-file_icon_003_00.png-rel3
)
You can map each array to a function that concatenates them:
$result = array_map(function($a, $b) { return $a.$b; }, $one, $two);
If you define one array with subarrays then you can unpack that array ...:
$array = [$one, $two];
$result = array_map(function($a, $b) { return $a.$b; }, ...$array);
Or for fun, you can extract each column from the subarrays and implode them:
$array = [$one, $two];
for($i=0; $a=array_column($array, $i); $i++) {
$result[] = implode($a);
}
I have a multidimensional array. I want to get array element which value is greater than 2 and less than 17. My Array is given below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
[1] => Array
(
[0] => 2
)
)
I want output like below:
Array
(
[4] => Array
(
[0] => 17
[1] => 15
[2] => 12
)
[5] => Array
(
[0] => 16
)
[2] => Array
(
[0] => 3
[1] => 1
)
)
Please help me how can I do it easy & fast method.
You can use a nested array filter.
$result = array_filter($outer_array, function($inner_array) {
return array_filter($inner_array, function($number) {
return $number > 2 && $number < 17;
});
});
Then inner array filter will result in an empty array being passed to the outer array filter if no values are found in the specified range. The empty array will evaluate to false in the outer filter callback, eliminating that array from the result.
Demo at 3v4l.org.
Generally, we want you to show what you have tried before we'll write code for you, but this one's on the house. For future reference, include some code snippets along with your question to avoid getting downvoted.
$output = array();
for($i = 0; $i < count($arr); $i++)
{
$use = false;
for($j = 0; $j < count($arr[$i]); $j++)
{
if($arr[$i][$j] > 2 and $arr[$i][$j] < 17)
{
$use = true;
break;
}
}
if($use)
$output[] = $arr[$i];
}
return $output;
i have a array placed in the variable $class that is constituted of sub arrays containing 2 student id's each,
i am trying to find all the sub arrays that contain a certain id for example 11
i want to keep all arrays containing this id in a variable.
array example
Array
(
[0] => Array
(
[s1] => 6
[s2] => 37
)
[1] => Array
(
[s1] => 8
[s2] => 11
)
[2] => Array
(
[s1] => 11
[s2] => 48
)
)
code
foreach ($class as $key => $value) {
if(!in_array($id, $class)){
unset($class[$key]);
}
}
You are close. If you loop and reference the correct variable with the in_array(), it would work as you hopefully need. Then assign the matching array into a new array var to use later (so you are not altering your original array!):
$id = 11;
$matched = array();
foreach ($class as $i => $students) {
if ( in_array($id, $students) ) {
$matched[] = $class[$i];
}
}
print_r($matched);
Would result in:
Array (
[0] => Array
(
[s1] => 8
[s2] => 11
)
[1] => Array
(
[s1] => 11
[s2] => 48
)
)
Associative array
http://php.net/manual/es/function.array-search.php
$class = array(array("s1"=>6, "s2"=>37),array("s1"=>8,"s2"=>11),array("s1"=>11, "s2"=>48));
$id = 11;
foreach ($class as $key => $value) {
if(!array_search($id, $value)){
unset($class[$key]);
}
}
Sorry, you can follow this answers
using array_search for multi dimensional array
I've decoded a JSON result from a server request and now need to sort based on the [name] field from the array. The deserialized code looks like this (snippet)
Array
(
[items] => Array
(
[0] => Array
(
[houseTypes] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
[id] => 1
[name] => Aberdeen
[isLive] =>
)
[1] => Array
(
[houseTypes] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
[id] => 2
[name] => Aberystwyth
[isLive] =>
There is no guarantee that the data coming down from the server will by alphabetical, so I need to sort based on the name.
I've tried using sort, assort and ksort, but none are showing correctly.
Is there a simple way to do this?
do it simple,
try this:
function cmp($a,$b)
{
if($a['name'] == $b['name'])
return 0;
return ($a['name'] < $b['name']) ? -1 : 1;
}
uasort($yourarray['items'],'cmp');
print_r($yourarray);
you can try with usort.See this http://php.net/manual/en/function.usort.php
I use this:
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$users = subval_sort($users,'name');
I have an array data that look like this :
Array (
[0] => Array (
[0] => Name:
[1] => John W.
[2] => Registration ID:
[3] => 36
)
[1] => Array (
[0] =>Age:
[1] => 35
[2] => Height:
[3] => 5'11"
)
[3] => Array (
[0] => Sex:
[1] => M
[2] => Weight:
[3] => 200lbs
)
[4] => Array (
[0] => Address
)
[5] => Array (
[0] => 6824 crestwood dr delphi, IN 46923
))
And I want to convert it to associative array like this :
Array(
['Name']=> John W.
['Registration ID']=> 36
['Age']=> 35
['Height'] => 5'11''
['Sex']=>M
['Weight']=>200lbs
['Address']=>6824 crestwood dr delphi, IN 46923
)
I have no idea at all how to do this, since the supposed to be array column header were also in sequence, so it makes difficult to convert this array.
Any help I appreciate, thx.
Given your origin array is called $origin , you can do it like this:
$merged = array();
foreach($origin as $val) {
$merged = array_merge($merged, $val);
}
$tot = count($merged) - 1;
for ($i=0;$i<$tot;$i+=2) {
$result[$merged[$i]] = $merged[$i+1];
}
var_dump($result); // To test the resulting array
Firstly, I use array_merge() to flatten the $origin array to only one dimension/depth, so we later iterate it (stepping each 2 items per iteration) and assigning each pair of items ($i and $i+1) to the resulting array.
Looks like, for the first 3 children, you can just assign the even value to the previous element as key. Then, assign the fourth one as key for fifth element.
$result = array();
foreach ($array as $key => $value)
{
if ($key < 4) {
$elements = array_values($value);
$result[$elements[0]] = $elements[1];
$result[$elements[2]] = $elements[3];
}
if ($key == 4)
$fifthkey = $value;
if ($key == 5)
$result[$fifthkey] = $value;
}
Also, note that you have to escape your height string quotes.