Vento
Vento

Reputation: 37

PHP float to string

I must convert float and int to string in array..

for($x = 0; $x < $ile; $x++)
        {
        $myArray['result']['items'][$x]['id'];
        echo '<pre>';
        $myArray['result']['items'][$x]['id'];  
        $tablica[$x] = $myArray['result']['items'][$x]['id'];
        echo '</pre>';
        var_dump ($tablica[$x]);
        echo '<br/>';
        }

Return:

int(81121789) 
int(207360665) 
int(683847370) 
int(1256003572) 
float(2535676003) 
float(5158703351) 
float(5266812473) 
float(5267345149) 
float(5267945040) 

How i can convert this numbers, to string? btw. $myArray is json.

Upvotes: 0

Views: 5929

Answers (2)

Iwnnay
Iwnnay

Reputation: 2008

You can simply use strval this will call the native __toString() method on the class/instance that is passed to it. You could also accomplish your desired layout like this.

echo '<pre>';
$items = $myArray['result']['items'];
$tablica = [];
foreach($items as $item)
    $tablica[] = strval($item['id']);
}
var_dump($tablica);
echo '</pre>';

Alternatively you could force the type to a string like this:

echo '<pre>';
$items = $myArray['result']['items'];
$tablica = [];
foreach($items as $item)
    $tablica[] = (string) $item['id'];
}
var_dump($tablica);
echo '</pre>';

Upvotes: 2

Vento
Vento

Reputation: 37

Ok i find a problem $tablica[$x] = (string) $myArray['result']['items'][$x]['id'];

Upvotes: 0

Related Questions