i have an array like this which i got from parsing the html source:
Array
(
[0] => 12345
[1] => 54321
[2] => 32546
[3] => 98754
[4] => 15867
[5] => 75612
)
if I put it in a variable and run a foreach loop prefixing a url like so:
$x = Array;
foreach ($x as $y){
$r = 'http://example.com/' . $y;
echo $r . '<br>';
}
It will output like this:
http://example.com/12345
http://example.com/54321
http://example.com/32546
http://example.com/98754
http://example.com/15867
http://example.com/75612
and each of those output url if run in the browser will output an object EITHER like this:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
OR like this:
{
"error": {
"errorMessage": "errorMessageValue",
"errorType": "errorTypeValue"
}
}
so my question is... how do I filter the Array in php so that it will only give me an Array that has a valid key/value pair and not the error object.
as suggested, I tried the following:
$x = Array;
foreach ($x as $y){
$link = 'http://example.com' . $y;
$json = file_get_contents($link);
$array = array_filter((array)json_decode($json), "is_scalar");
echo '<pre>';
echo json_encode($array) . '<br>';
echo '</pre>';
}
but it still output the same array and does not exclude the error object.