PHP Print keys from an object?

前端 未结 5 944
眼角桃花
眼角桃花 2021-01-07 18:07

I have an object BIRD and then there is [0] through [10] and each number has a subheading like \"bug\" or \"beetle\" or \"gnat\" and a value for each of those.

I wan

5条回答
  •  梦谈多话
    2021-01-07 18:39

    Looks like array_keys might have stopped working on objects but, amazingly, the foreach construct works, at least on a php stdClass object.

    $object = new stdClass();
    $object->a = 20;
    $object->b = "hello";
    $keys = array_keys($object);
    // array_keys returns null. PHP Version 7.3.3 windows
    foreach($object as $key=>$value)
    {
         // but this works
         echo("key:" . $key . " value:" . $value . "\n");
    }
    

提交回复
热议问题