2

So I have the following:

    echo array_search('Resolved at Tier 1', array_column($getHighLevelOverviewPeriodsArray, 'status'));
    print_r($getHighLevelOverviewPeriodsArray);

    if (!array_search('Resolved at Tier 1', array_column($getHighLevelOverviewPeriodsArray, 'status'))) {
        $resolved = array('status' => 'Resolved at Tier 1', 'amount' => 0);
        array_splice($getHighLevelOverviewPeriodsArray, 0, 0, array($resolved));
    }
    print_r($getHighLevelOverviewPeriodsArray);

The echo spits out a zero which is right. It does exist in the first place. However the second part runs (if statement) and the array_splice gets executed. The output of print_r is below.

What is it being executed even though it's there?

I have the exact same code for Tier 2, character for character (expect for the 2) and that works as expected.

Array
(
    [0] => Array
        (
            [status] => Resolved at Tier 1
            [amount] => 10
        )

    [1] => Array
        (
            [status] => Resolved at Tier 2
            [amount] => 7
        )

    [2] => Array
        (
            [status] => Resolved Total
            [amount] => 17
        )

    [3] => Array
        (
            [status] => Phone Calls
            [amount] => 0
        )

)
Array
(
    [0] => Array
        (
            [status] => Resolved at Tier 1
            [amount] => 0
        )

    [1] => Array
        (
            [status] => Resolved at Tier 1
            [amount] => 10
        )

    [2] => Array
        (
            [status] => Resolved at Tier 2
            [amount] => 7
        )

    [3] => Array
        (
            [status] => Resolved Total
            [amount] => 17
        )

    [4] => Array
        (
            [status] => Phone Calls
            [amount] => 0
        )

)
5
  • Why do you have array($resolved) at the end of your spliced statement instead of just $resolved since $resolved is already an array? Commented Jul 9, 2017 at 0:41
  • That's what I saw had to be done but regardless of that - the search still isn't working Commented Jul 9, 2017 at 0:52
  • Are you getting any errors? Have you checked the logs? Commented Jul 9, 2017 at 0:54
  • No errors. Nothing in logs. Commented Jul 9, 2017 at 0:58
  • Possible duplicate of How does true/false work in PHP? Commented Jul 9, 2017 at 4:05

1 Answer 1

1

Read the warning in the manual http://php.net/manual/en/function.array-search.php. 0 == false after type juggling. You need:

 if (false !== array_search ...

instead of:

 if (!array_search...

edit to add: Tier 2 works as expected, because indexes greater than zero are 'truthy'.

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

1 Comment

if (false === array_search seems to have sorted it

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.