【问题标题】:KnpPaginatorBundle return currentPageNumber as integerKnpPaginatorBundle 以整数形式返回 currentPageNumber
【发布时间】:2016-11-16 09:04:01
【问题描述】:

我在我的 Symfony2 项目中使用 KnpPaginatorBundle。每次我请求特定页面的结果时,结果返回给我看起来像:

{
  "currentPageNumber": "2",
  "numItemsPerPage": 5,
  "items": [ ...
   ]
}

如您所见,currentPageNumber 在这里是字符串。如何将此属性的类型更改为整数?

【问题讨论】:

    标签: symfony knppaginatorbundle


    【解决方案1】:

    您可以在 php 中使用标准类型转换 (documentation)

    对于示例,您可以这样做:

    $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
    

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2023-04-11
      • 2014-04-18
      • 1970-01-01
      • 2011-01-11
      相关资源
      最近更新 更多