0

I need help to solve the question that was on the exam at my college. It goes something like this: ‘’Function in PHP which checks how many integers are in associative array, data is not transmitted over the arguments, but as a global variable’’. Thanks!

4
  • 4
    I guess they must've told you to make 1 as there's nothing ready made like this
    – Mr. Alien
    Oct 28, 2012 at 8:15
  • Please rephrase the question - as it stands it does not make sense.
    – Ed Heal
    Oct 28, 2012 at 8:17
  • Please give an example input and output. Where are you checking? Keys? Values? One dimensional? Multi dimensional? The question lacks details. Oct 28, 2012 at 9:02
  • This should be done recursive, so over arguments is how should be transmitted Oct 28, 2012 at 9:04

2 Answers 2

1

This is a really dodgy question. First, PHP isn't strongly typed, so the notion of "this is an integer" is not obvious. For instance, how many integers are in this following array?

$array = array(5, "6", "7abc", 8.0, "9.0xyz", false);

If we look at the types strictly, there is one integer. If we compare loosely (that is, allow implicit type coercion), there are six. You could use any arbitrary function to determine if something was integer-like for your given situation. I don't feel there is enough information in the question alone to give one answer, but I imagine there was more context given in your lectures.

Also there is another problem to the question. "how many integers are in associative array" has no clear definition of 'in', or rather, there is no definition of the true structure of the array. Consider the following.

class Foo {
    public $x;
    public $y;
    private $xy;
    public __construct($x, $y) { $this->x = $x; $this->y = $y; $this->xy = $x * $y; }
}

$array = array(
    10,
    new Foo(3, 4), 
    array(
        array(
            12,
        ),
        17,
        3
    )
);

Do you just consider the top-level or do you descend recursively? If you descend recursively, must you also scan inside objects? Are private integers in those objects also 'in' the array? Also, will the algorithm that descends recursively have to detect and avoid cycles? There is quite an amount of uncertainty as to what is really demanded.

So, to answer the question, you'll have to make a couple assumptions. I'm presuming they intended for you to check for integers with the 'is_int' function, or equivalent, to determine the variable's type. Secondly, I'm assuming they intended for you to only concern the top-level of the associative array. I imagine the exercise was to prove your familiarity with 'foreach', how to access global variables, and the reality that variables still have types, even if PHP allows you to ignore them.

"data is not transmitted over the arguments, but as a global variable" might sound a bit confusing, but it just means that your function will take no arguments. Instead, it counts integers in a global array. In this example, I'll call the global array '$globalArray'.

$globalArray = array("k1" => "foo", "k2" => 5, "k3" => "bar", "k4" => 10);

function num_ints_in_array() {
    global $globalArray;
    $num_ints = 0;
    foreach ($globalArray as $v) {
        if (is_int($v)) ++$num_ints;
    }
    return $num_ints;
}

Just an extra edit for fun: there are of course many approaches. This particular functional solution tickles my fancy.

function num_ints_in_array() {
    global $globalArray;
    return count(array_filter($globalArray, 'is_int'));
}
1
  • Well, that explaination is pretty clear! There are indeed many assumptions to make.
    – MC Emperor
    Oct 28, 2012 at 8:55
0

Try the following:

<?php

$a[0] = 1;
$a[1] = 'aam';
$a[2] = 5;
$a[3] = 'c';
$result = count($a);
$totalints = 0;

for ($i = 0; $i < $result; $i++)
{
    if (is_int($a[$i]))
    {
        $totalints++;
    } 
}


echo "Total number of integers: " . $totalints;

?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.