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?