PHP push new key and value in existing object array

前端 未结 2 556
难免孤独
难免孤独 2020-12-15 07:17

In my study how objects and arrays work with PHP I have a new problem. Searching in existing questions didn\'t give myself the right \"push\".

I have this for exampl

相关标签:
2条回答
  • 2020-12-15 07:47

    array_push() doesn't allow you to specify keys, only values: use

    $html_doc["title"] = "testtitle";
    

    .... except you're not working with an array anyway, because you're casting that array to an object, so use

    $html_doc->title = "testtitle";
    
    0 讨论(0)
  • 2020-12-15 08:04

    You can simply use $html_doc["title"] = "testtitle";

    Check this comment on the array_push manual page.

    0 讨论(0)
提交回复
热议问题