0

hopefully a easy one for you,

my sql query is returning a mulit-dimensional array, I need to access only one key the is nested on the second level but cant figure out how.

here is my function.

public function get_visitor_id($id)
{
  $this->db->where('mobile',$id);
  $this->db->or_where('email',$id);
  $this->db->select('uid');
  $result = $this->db->get('visitors');
  if ($result)
  {
    foreach ($result->result() as $key=>$value){
      $array[$key] = $value;
    }
    var_dump($array);
    return $array;
  }
}

The array returned is

{ [0]=> object(stdClass)#20 (1) { ["uid"]=> string(2) "24" } }

I only need the value of ['uid'] so in essence if I was to echo get_visitor_id() it would evaluate to "24".

Thanks for you help.

Cheers

3
  • may i ask why you using $array? Commented Sep 28, 2016 at 21:57
  • What library is $this->db? Is this Laravel? Commented Sep 28, 2016 at 23:10
  • xYuri, Im not sure if I needed to, but im still quite new to programming so thought it would be easiest to loop through the results and add them to an array and then return only that key's value. I am open to a better way of doing it. Commented Sep 29, 2016 at 1:07

1 Answer 1

1

try changing foreach() func to:

foreach($result as $res){
$res = $res->fetch_assoc();
$array['uid'] = $res['uid'];}

EDIT: in case of this didn't work then try while loop:

while($res = $result->fetch_assoc()){
  $array['uid'] = $res['uid'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't look like he's using mysqli. I think it's Laravel.
if so, i wont be much useful in that case :(
Thanks, I will give that a try. I am using codeignitor and its built in library
well try it and tell me what you got ;) hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.