Search code examples
phparrayslistarray-map

PHP: Apply a function to multiple variables without using an array


I have a function (for ease, I'll just use count()) that I want to apply to maybe 4-5 different variables. Right now, I am doing this:

$a = count($a);
$b = count($b);
$c = count($c);
$d = count($d);

Is there a better way? I know arrays can use the array_map function, but I want the values to remain as separate values, instead of values inside of an array.

Thanks.


Solution

  • I know you said you don't want the values to be in an array, but how about just creating an array specifically for looping through the values? i.e.:

    $arr = Array($a, $b, $c, $d);
    
    foreach ($arr as &$var)
    {
       $var = count($var);
    }
    

    I'm not sure if that really is much tidier than the original way, though.