Symfony2 プロジェクトで KnpPaginatorBundle を使用しています。結果の特定のページをリクエストするたびに、次のような結果が返されます。
{
"currentPageNumber": "2",
"numItemsPerPage": 5,
"items": [ ...
]
}
ご覧のとおり、currentPageNumber
ここに文字列があります。このプロパティのタイプを整数に変更するにはどうすればよいですか?
Symfony2 プロジェクトで KnpPaginatorBundle を使用しています。結果の特定のページをリクエストするたびに、次のような結果が返されます。
{
"currentPageNumber": "2",
"numItemsPerPage": 5,
"items": [ ...
]
}
ご覧のとおり、currentPageNumber
ここに文字列があります。このプロパティのタイプを整数に変更するにはどうすればよいですか?
php で standardtype キャストを使用できます (ドキュメント)
サンプルの場合、次のようにできます。
$intValue = (int) "123";
そして、このケースを見てください:
$stringValue = "123";
echo gettype($stringValue); // will be 'string' here
$intFromStringValue = (int) $stringValue;
echo gettype($intFromStringValue); // will be 'integer' here
// but be aware of this
echo (int) "Some words"; // will be integer but value will be 0, because type casting cannot get the number from string
echo (int) "200 and some words"; // integer 200
echo (int) "Some words and 300"; // integer 0