Friday, 17 November 2017

php - Array in foreachloop returns additional null value

I have a problem in php.

So, i have a
json data which i will decode and put in object
Then, i will use foreach
function for gettings the parameters.
I'll be putting this to a array so i can
call it outside of the loop.
However,when i'm displaying the results it
returns a additional null value like
this:



[{"mobile":"639179512744"},{"mobile":"639054971214"},
{"mobile":"639394297841"},{"mobile":"639296378818"},{"mobile":"639265161309"},{"mobile":null}]


Where
the last record should not be included. I only have 5 records of
mobile.




Here is my
code:



$data =
file_get_contents("php://input");
$data =
(object)json_decode($data);
$model = $data->data;
$model =
json_encode($model);
$model = json_decode($model);
$model =
(object)$model;
foreach($model AS $row){
$mobile =
$row->mobile_number;

$array[] = array(

'mobile'=>$mobile
);

}

echo
json_encode($array);


And
here is the
data:




{"action":"upload","data":[{"mobile_number":"639179512744","sentdt":"2017-
02-07 00:21:57","imsi":"","remarks":"ACTIVE"},
{"mobile_number":"639054971214","sentdt":"2017-02-07
00:21:57","imsi":"","remarks":"ACTIVE"},
{"mobile_number":"639394297841","sentdt":"2017-02-07
00:21:57","imsi":"","remarks":"Absent Subscriber - No International Mobile Subscriber
Identity"},{"mobile_number":"639296378818","sentdt":"2017-02-07
00:21:58","imsi":"","remarks":"Absent Subscriber - No International Mobile Subscriber
Identity"},{"mobile_number":"639265161309","sentdt":"2017-02-07
00:21:58","imsi":"","remarks":"ACTIVE"},{}]}


Someone
can help?I want to get rid of the additional null
value
Thanks

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...