In my following code I can explode and get my 3 elements imei,lat,lon as the response was returning only the 3 data separated by ' '
.
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
echo "$remote_ip : $remote_port -- " . $buf;
list($imei,$lat,$lon) = explode(' ', $buf, 3);
But what if the response is sending me a lot of data separated by ' '
but still i need only the 3 elements. I don't need the rest data. How can i explode like that?
The data will not be the first three items returned by explode()
.
Just add one more comma to list()
to ignore the remaining values:
list($imei,$lat,$lon,) = explode(' ', $buf);
Demo
Or, if the values are in specific places in your array:
list($imei, , $lat, , $lon,) = explode(' ', $buf);
Demo
The empty placeholders "skip" those values and they are never assigned to a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With