0

Here is my array:

print_r($mCOneC);

=> Array ( [0] => start [1] => Trial [2] => Refusal [3] => Expel [4] => Mouth Clean )

If I do:

$length = count( array_keys( $mCOneC, 'start' ));

echo $length;

I get: 1

If I do:

$length = count( array_keys( $mCOneC, 'Trial' ));

echo $length;

I get: 0

Any reason why this is not working?

6
  • 1
    Please help us by providing how you defined (built) your array. Commented Oct 24, 2012 at 14:46
  • I cannot reproduce this problem. How do you create the array? Commented Oct 24, 2012 at 14:48
  • At my localhost both Lenghts have result = 1 ... Commented Oct 24, 2012 at 14:49
  • 1
    Are you sure the Trial value in the array isn't Trial[space] or otherwise has an invisible/unprintable character in it, making it something OTHER than Trial? a var_dump of the array will show its string length, and it SHOULD be 5. if it's not, then it's not just 'Trial'. Commented Oct 24, 2012 at 14:49
  • Array was built from a db result: $mCOneC = $row['mCOneC']; $mCOneC = explode(",",$mCOneC); Commented Oct 24, 2012 at 14:51

2 Answers 2

1

The array_keys does not lie to you. For your second example you're having an issue with returning zero:

count(array_keys($mCOneC, 'Trial'));

This just means that the array $mCOneC does not contain any string that is exactly 'Trial'. You need to more thoroughly inspect the original data why it does not match, for example with the var_dump function on the concrete value:

var_dump($mCOneC[1]);

This should shed more light into your issue. The print_r function you use is not that specific as var_dump. Also take care when viewing things inside the browser window, take a look into the source-view of your browser, too. It's displaying things better than the browser window when you need to debug.

Sign up to request clarification or add additional context in comments.

Comments

0

Simple you are dealing with space

$mCOneC = Array(
        0 => "start",
        1 => "Trial ",
        2 => "Refusal",
        3 => "Expel",
        4 => "Mouth Clean");


$mCOneC = array_map("trim", $mCOneC); <------------- Fix Spaces
$length = count( array_keys( $mCOneC, 'start' ));
var_dump($length);


$length = count( array_keys( $mCOneC, 'Trial' ));
var_dump($length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.