0

I am trying to select data from an array. I am using the following script.

I basically have a function that is supposed to scan the array and find results within the last hour but I think my function has some problems.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

    $result = 'array(2) {
      ["Status"]=>
      string(3) "001"
      ["CallData"]=>
      array(1) {
        ["item"]=>
        array(36) {
          ["CallRef"]=>
          string(10) "1234567890"
          ["CallStartDate"]=>
          string(10) "2017-01-22"
          ["CallStartTime"]=>
          string(8) "11:59:30"
        }
      }
    }';

    function getFilteredCallsByDate($calls, $since) {
        return new CallbackFilterIterator(new ArrayIterator($calls['CallData']['item']), function ($call) use ($since) {
            return strtotime(sprintf('%s %s', $call['CallStartDate'], $call['CallStartTime'])) >= strtotime($since);
        });
    }

    // limit to last 60 minutes
    foreach (getFilteredCallsByDate($result, '-60 minutes') as $call) {

        var_dump($call);

    }

?>

However, when I run this, I get the following errors:

Warning: Illegal string offset 'CallData' in test.php on line 25

Warning: Illegal string offset 'item' in test.php on line 25

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in test.php:25 

Stack trace: 
#0 test.php(25): ArrayIterator->__construct('a') 
#1 test.php(31): getFilteredCallsByDate('array(2) {\n\t [...', '-60 minutes') 
#2 {main} thrown in test.php on line 25

How can I debug these errors and resolve why I'm getting them?

1 Answer 1

1

$result isn't an array, but a string. You have encapsulated it in quotation marks.

Update: Apart from that: Your CallbackFilterIterator goes over each entry in result["CallData"]["item"]. So it will, in your example, return: "1234567890", "2017-01-22", "11:59:30".

What you probably want to do is iterate over result["CallData"] rather:

<?php
$result = ["Status"=>"001", "CallData" => ["item" => ["CallRef"=>"1234567890", "CallStartDate"=>"2017-01-22", "CallStartTime"=>"11:59:30"]]];
function getFilteredCallsByDate($calls, $since) {
    return new CallbackFilterIterator(new ArrayIterator($calls['CallData']), function ($call) use ($since) {
        return strtotime(sprintf('%s %s', $call['CallStartDate'], $call['CallStartTime'])) >= strtotime($since);
    });
}

foreach (getFilteredCallsByDate($result, '-60 minutes') as $call) {

    var_dump($call);

}

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

4 Comments

Thanks for this. I should probably explain this better. $result actually stores an array but in the example above I've (stupidly) made it into a string. So the original code is $result = $client->call("GetCalls"); which returns an array to $result. I'm still getting the same error when I do either.
I owe the world's biggest hug right now, fjc. It works, it amazingly works!!!! Thank you so so much. No idea how much head scratching I've been doing since 6am here :-/ :-( Thank You.
Quick tip how I found it easily: I just echoed the value of $call inside your closure.
I did that too, or at least tried, but just didn't have the knack to figure out the rest. Very much a big thank you from my end. Hope you have a great day :-)

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.