I have this array:
$number= array(
array([0]=>2,[1]=>2)
array([0]=>2,[1]=>2,[2]=>2,[3]=>2)
array([0]=>2,[1]=>2,[2]=>2)
array([0]=>2,[1]=>2)
)
I want maximum index in this array. for this array is 4.
This code tested and works for your array. Of course array_map with count func is best solution for dimention 1 max count
<?php
$number= array(
array(0=>2,1=>2),
array(0=>2,1=>2,2=>2,3=>2),
array(0=>2,1=>2,2=>2),
array(0=>2,1=>2),
);
$ak = array_values($number);
for($i;$i<count($number);$i++){
$c[] = count($ak[$i]);
}
echo max($c);
?>
Use count() function:
$max = count($number); // returns 4
Update
Your question is not clear enough. Provided comments and answers indicate that you want to get number of elements that exists in the array that have elements more than the other arrays within the $number
array. And in this case @xdazz
answer is the best one for you. And it's better and more correct to write your $number
array like this:
$number = array(
array(1, 2),
array(1, 2, 3, 4, 5),
array(1, 2, 3),
array(1, 2)
);
Write a loop that iterates over the array. For each array in the array, get the number of elements in that array, and if it's greater than the greatest so far (say, $max
), set $max
to the number of elements. Then, when you're done with the loop over the array, $max
has your answers.
You may mean this:
echo max(array_map('count', $number));
You can try the following code:
$data = array(
"apples" =>
array("red", "yellow", "pineapples","enam"),
"bananas" =>
array("small", "medium", "big","enam","enam"),
"vegs" =>
array("potatoes", "carrots", "onions")
);
foreach($data as $in_arr){
$count = count($in_arr);
if($count>$max)$max = $count;
}
echo "The max:".$max;