【问题标题】:get value from array with std object (php) [duplicate]使用标准对象(php)从数​​组中获取值[重复]
【发布时间】:2023-03-15 11:28:02
【问题描述】:

我的 PHP 代码:

    $stuff = array($this->complaint_model->get_reply($id)->result());

    print_r($stuff);

结果:

Array (
    [0] => Array (
        [0] => stdClass Object (
            [id_customer] => 21 
            [id_complaint] => 2 
            [id] => 4 
            [nama_customer] => Muhammad Bima Zehansyah 
            [from] => Admin 
            [balasan] => coba update 
         ) 
     ) 
)

我的问题是,如何获得价值 [nama_customer] ?在伙计们之前谢谢

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:

    试试这个

    $stuff = array($this->complaint_model->get_reply($id)->result());
    echo $stuffVal = $stuff[0][0]->nama_customer;
    

    【讨论】:

    • $stuffVal 绝对没有必要
    • 打得好兄弟!
    【解决方案2】:

    获取这样的值

    $stuff[0][0]->nama_customer
    

    这里有多维数组对象,这就是为什么你需要先遍历两个数组,如$stuff[0][0],然后遍历对象,如$stuff[0][0]->nama_customer

    【讨论】:

    • 谢谢兄弟分享!!
    • 欢迎您并通过接受我的回答来结束您的问题 :)
    【解决方案3】:

    实际上你不需要将结果放入额外的array 并且检查结果是否为空是明智的。 因此,您只需从结果中获取第一项(这是一个对象)并调用参数nama_customer

    $stuff = $this->complaint_model->get_reply($id)->result();
    if (!empty($stuff[0]))
        echo $stuff[0]->nama_customer;
    

    【讨论】:

    • 不错的答案兄弟!干得好
    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多