$arr1 = array('a' => '1', 'b' => 'blah', 'c' => 'whatever...',
'aa' => '2', 'bb' => 'lbha', 'cc' => 'everwhat...', 'dd' => 'bingo',
'aaa' => '3', 'bbb' => 'halb', 'ccc' => 'revetahw...');
In the array I have three different index lengths a,b and c are all 1 in length. aa,bb,cc and dd are all 2 in length. And aaa,bbb and ccc are all 3 in length.
What I'm trying to do is find the index (group by length) with the most elements and the greatest length.
so I would use aa,bb,cc,dd as they have 4 elements, this would return the index length of 2.
I want to know how I can get the 2?
Here is what I'm trying but it's not working
foreach($arr1 as $key => $data) {
$index_length_arr[strlen($key)] = $index_length_arr[$key] + 1;
}
Results:
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Expected Output:
Array
(
[1] => 3
[2] => 4
[3] => 3
)
Then I could see that index (with the length of 2) had the most elements:
'aa' => '2', 'bb' => 'lbha', 'cc' => 'everwhat...', 'dd' => 'bingo',
$array = array_count_values(array_map('strlen',array_keys($arr1)));
Should give ya what ya want.
Edit:
Your original code was fine except you have to change the = .... to $index_length_arr[strlen($key)]
You were using two different keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With