Itereating - Looping through stdClass

Discussion in 'PHP' started by m0rt1l, Jun 28, 2012.

  1. #1
    Hi im wondering if anyone could shed light on how to do this. I have an array below:

    Array ( [0] => stdClass Object ( [id] => 1 [cat_name] => Doo Range ) [1] => stdClass Object ( [id] => 2 [cat_name] => Com Range ) [2] => stdClass Object ( [id] => 3 [cat_name] => Master Range ) )

    I need to loop through and pick out all cat names, at the moment using the code I have only returns one cat name.

    $query = $this->db->query("SELECT * FROM fuel_product_cats");

    foreach($query->result() as $rows)
    {
    $options = array($rows->id => $rows->cat_name);
    }

    // Check the array
    print_r($options); ### ONLY ONE CAT NAME APPEARS IN THE ARRAY, NOT ALL 3 LIKE I WANT

    Thanks in advance.
     
    m0rt1l, Jun 28, 2012 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    
    foreach($query as $rows)
    {
    $options = array($rows->id => $rows->cat_name);
    }
    
    PHP:
    This should work, if not, print_r your query before the array
     
    ssmm987, Jun 28, 2012 IP