【问题标题】:how to get single value from array in codeigniter如何从codeigniter中的数组中获取单个值
【发布时间】:2023-04-10 01:06:02
【问题描述】:

我使用此代码将一些数据存储到数组中

$this->data['result']    =   $this->mymodel->search($keyword);

当我使用print_r($this->data);打印我的结果时

它是这样显示的

Array ( [base_url] => http://localhost/pelikanartline.com/ [result] => Array ( [0] => stdClass Object ( [product_id] => 2 [product_code] => 167007 [product_name] => ARTLINE VIVIX HIGHLIGHTERS 10PK YELLOW [product_discription] => some description [xylene_free] => y [rohs_compliant] => y [product_features] => some text ) ) )

现在我想将 product_id 值存储到 pro_id 之类的变量中。请帮我这样做。

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    试试这个:

     $pro_id = [];
    
     foreach($this->data['result'] as $key => $val){
        $pro_id[] = $val->product_id;
     }
    
     print_r($pro_id);
    

    【讨论】:

    • 真棒人...它的工作。爱你兄弟...我也删除 $pro_id = [];这部分。当我使用它时,它会出错。否则它 100% 完美......
    • @Udara Suranga。如果这可行,请接受答案。
    • 是的,我在等 4 分钟。变成他们不允许我在 4 分钟内接受答案。
    【解决方案2】:

    如果你的模型只返回一条记录,你可以使用这个快捷方式:

    $pro_id = $this->data['result'][0]->product_id;

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多