如何从php中的数组中获取数据

时间:2015-12-26 13:23:59

标签: codeigniter

我有这样的查询:

    $this->db->select('shop_users.user_id');
    $this->db->select('users.*');
    $this->db->from('shop_users');
    $this->db->join('users' , 'users.id = shop_users.user_id' );
    $this->db->where('shop_users.shop_id',$gets['shop_id']);
    $user = $this->db->get()->result();

    $data['user'] = $user;

    var_dump($user);

    return;

var_dump()的内容是:

> array(1) { [0]=> object(stdClass)#76 (29) { ["user_id"]=> string(5)
> "33691" ["id"]=> string(5) "33691" ["username"]=> string(16)
> "" ["password"]=> string(32)
> "" ["name"]=> string(8) "سوده"
> ["family"]=> string(17) "محمد کاشی" ["email"]=> string(21)
> "" ["avatar"]=> string(11) "default.gif"
> ["status"]=> string(1) "1" ["submit_date"]=> string(19) "2015-12-26
> 13:17:20" ["last_activity"]=> string(1) "0" ["activecode"]=> string(1)
> "0" ["submit_ip"]=> string(0) "" ["city"]=> string(1) "1" ["zone"]=>
> string(1) "0" ["city_name"]=> string(1) " " ["phone"]=> string(20)
> "" ["bankAcount"]=> string(13) ""
> ["bankCart"]=> string(1) " " ["bank"]=> string(1) " " ["admin_seen"]=>
> string(1) "1" ["gender"]=> string(1) " " ["finance"]=> string(1) "0"
> ["coupons"]=> string(1) "0" ["shaba"]=> string(1) " " ["ref"]=>
> string(1) "0" ["done"]=> string(1) "0" ["hasmobile"]=> string(1) "0" }
> }

现在,如何获取user_id

我测试下面的代码,但它们返回null:

$user->user_id;
$user['user_id'];

2 个答案:

答案 0 :(得分:1)

$user[0]['user_id'];

$user[0]->user_id;

您必须访问数组的第一个元素,因为它是您返回值的数组。 (数组中的数组)

答案 1 :(得分:1)

您从此查询中获取数组。你可以在frist获得第一件商品,而不是获得商品的颜色。

if($user && count($user)>0){
  echo $user[0]->user_id;
}