Temuulen Optimistic Temuulen Optimistic - 5 months ago 9
PHP Question

PHP JSON display number if digits in foreach loop

i have a question and i need help with my json and php. I use foreach loop to get the object number from my json. And when i get data from mark of mastery it displays like 13423452623423 i want to display it as 14 not 13423452623423. I just want to display how many number of digits but how do i do that? Thank you. (Sorry for my bad english)



<?php
$player_vehicles = file_get_contents("https://api.worldoftanks." . $_GET["server"] . "/wot/tanks/stats/?application_id=demo&account_id=" . $account_id);

$vehicles_player = json_decode($player_vehicles, TRUE);
foreach ($vehicles_player['data'][$account_id] as $tank_id) {
echo $tank_id['mark_of_mastery'];
}
?>




Answer

You can output the length of a string using strlen. If your data is coming in as an int, you can cast that to a string before using strlen. So assuming that echo $tank_id['mark_of_mastery'] is displaying 13423452623423, then you would use:

<?php
$player_vehicles = file_get_contents("https://api.worldoftanks." . $_GET["server"] . "/wot/tanks/stats/?application_id=demo&account_id=" . $account_id);

$vehicles_player = json_decode($player_vehicles, TRUE);
foreach ($vehicles_player['data'][$account_id] as $tank_id) {
echo strlen($tank_id['mark_of_mastery']);
}
?>