【问题标题】:Get first element in PHP stdObject获取 PHP stdObject 中的第一个元素
【发布时间】:2012-03-17 11:34:42
【问题描述】:

我有一个像这样的对象(存储为 $videos)

object(stdClass)#19 (3) {
  [0]=>
  object(stdClass)#20 (22) {
    ["id"]=>
    string(1) "123"

  etc...

我只想获取第一个元素的 ID,而不必遍历它。

如果它是一个数组,我会这样做:

$videos[0]['id']

以前是这样工作的:

$videos[0]->id

但现在我在上面显示的行中收到错误“不能使用 stdClass 类型的对象作为数组...”。可能是由于 PHP 升级。

那么如何在不循环的情况下获得第一个 ID?有可能吗?

谢谢!

【问题讨论】:

    标签: php object multidimensional-array


    【解决方案1】:

    array() 和 stdClass 对象都可以使用 current()key()next()prev()reset()end() 功能。

    所以,如果你的对象看起来像

    object(stdClass)#19 (3) {
      [0]=>
      object(stdClass)#20 (22) {
        ["id"]=>
        string(1) "123"
      etc...
    

    那你就可以了;

    $id = reset($obj)->id; //Gets the 'id' attr of the first entry in the object
    

    如果您出于某种原因需要密钥,您可以这样做;

    reset($obj); //Ensure that we're at the first element
    $key = key($obj);
    

    希望对你有用。 :-) 在 PHP 5.4 上,即使在超严格模式下也没有错误

    【讨论】:

    • reset() 和 end() 很好,因为您不需要知道密钥,只需要知道位置(第一个或最后一个)。
    • 他们应该将 reset() 命名为 first() 或类似的名称,这样更有意义。这个答案是正确的。正确答案无效。
    • 继续讨论最佳实践和 OOPS,此答案必须标记为已更正,尽管我的答案提供的解决方案不是最佳方法。
    【解决方案2】:

    更新 PHP 7.4

    花括号访问语法自 PHP 7.4 起已弃用

    2019 年更新

    继续讨论 OOPS 的最佳实践,@MrTrick 的答案必须是 标记为正确,尽管我的回答提供了一个被黑的解决方案,但它不是 最好的方法。

    使用 {}

    简单地对其进行迭代

    例子:

    $videos{0}->id
    

    这样您的对象不会被破坏,您可以轻松地遍历对象。

    对于 PHP 5.6 及以下版本使用此

    $videos{0}['id']
    

    【讨论】:

    • 不太确定此 {} 运算符是否明确存在文档。但根据经验,我知道这是可能的。您会更感兴趣地知道 {} 运算符也可用于遍历字符串,例如: $str = "cool";如果你 echo $str{0} 它会输出第一个字母“C”,echo $str{1} 会输出“o”等等。试一试..
    • 使用 PHP 5.6,这是不可能的。您收到以下消息:不能使用 stdClass 类型的对象作为数组
    • 不,错误正是Cannot use object of type stdClass as array,当您尝试将 stdClass 对象用作数组时,这是错误。 stdClass 不从基对象类继承,因此不提供此功能。`
    • 只是想注意,当一个对象已经定义/字符串键时,这不起作用。
    • @Clain Dsilva:处理字符串“Grzegrzółka”
    【解决方案3】:

    容易多了:

    $firstProp = current( (Array)$object );
    

    【讨论】:

    • 如果您不知道节点名称,那就太好了。
    【解决方案4】:

    正确:

    $videos= (Array)$videos;
    $video = $videos[0];
    

    【讨论】:

    • 很好的答案,但它并不能真正帮助我浏览一个对象,它只是将它转换为一个数组,然后将它作为一个数组进行导航。
    【解决方案5】:

    $videos->{0}->id 为我工作。

    由于 $videos 和 {0} 都是对象,因此我们必须使用 $videos->{0}->id 访问 id。花括号需要在 0 左右,因为省略大括号会产生语法错误:意外的“0”,需要标识符或变量或“{”或“$”。

    我正在使用 PHP 5.4.3

    在我的情况下,$videos{0}->id$videos{0}['id'] 都没有工作并显示错误:

    不能使用 stdClass 类型的对象作为数组。

    【讨论】:

      【解决方案6】:

      您可能会在对象上循环并在第一个循环中中断... 类似的东西

      foreach($obj as $prop) {
         $first_prop = $prop;
         break; // exits the foreach loop
      } 
      

      【讨论】:

        【解决方案7】:

        使用 Php 交互式 shell,Php 7:

        ➜  ~ php -a
        Interactive shell
        
        php > $v = (object) ["toto" => "hello"];
        php > var_dump($v);
        object(stdClass)#1 (1) {
          ["toto"]=>
          string(5) "hello"
        }
        php > echo $v{0};
        PHP Warning:  Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
        Stack trace:
        #0 {main}
          thrown in php shell code on line 1
        
        Warning: Uncaught Error: Cannot use object of type stdClass as array in php shell code:1
        Stack trace:
        #0 {main}
          thrown in php shell code on line 1
        php > echo $v->{0};
        PHP Notice:  Undefined property: stdClass::$0 in php shell code on line 1
        
        Notice: Undefined property: stdClass::$0 in php shell code on line 1
        php > echo current($v);
        hello
        

        只有current 正在处理对象。

        【讨论】:

          【解决方案8】:

          可以做到以下几点

          $rows=null; 
          foreach ($result as $rows) break;
          

          【讨论】:

            猜你喜欢
            • 2014-07-31
            • 1970-01-01
            • 2011-04-06
            • 1970-01-01
            • 1970-01-01
            • 2022-08-17
            • 1970-01-01
            • 2019-06-20
            • 1970-01-01
            相关资源
            最近更新 更多