Jump to content

PHP to use the array with highest number of keys


irkevin

Recommended Posts

Hi,

 

I'm lost, I don't know if this is possible using PHP, guess I will have to ask. Here it goes. Below are some array:

 

Array
(
    [0] => 1
    [1] => ps2
    [2] => game
)

Array
(
    [0] => 2
)

Array
(
    [0] => 3
    [1] => game
)

 

As you can see, the first array has more data in it, how do I tell PHP to use only the array with more data?

 

I hope this is possible. Please help guys.

Just a quick example I can think of now:

 

$array1 = array(1,2,3);
$array2 = array(1,2);
$array3 = array(1,2,3,4);

$allArrays = array($array1 , $array2, $array3);
$count = 0;

for ($i=0; $i<sizeof($allArrays); $i++)
{
  $result = count($allArrays[$i]);
  if ($result>$count)
  {
    $count = $result;
    $largerArray = $allArrays[$i];
  }
}

print_r($largerArray);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.