Access array - stdClass Object Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Access array - stdClass Object Problem

Post by psurrena »

I'm am trying to use in_array() with db results but there is a problem since all the results I need are in the stdClass Object...

How do I either convert the object or access the elements within? Thanks!

Array ( [0] => stdClass Object ( [d_id] => 3 [d_name] => Architecture ) [1] => stdClass Object ( [d_id] => 4 [d_name] => Landscape Architecture ) [2] => stdClass Object ( [d_id] => 1 [d_name] => Planning ) [3] => stdClass Object ( [d_id] => 2 [d_name] => Urban Design ) )
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

Just type cast the object to (array).

;)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

Thanks for the response. I tried that with no success. Am I doing something wrong?

Code: Select all

$disciplines=(array)$disciplines;
 
if(in_array("Architecture", $disciplines)) {
    echo "it's in!";
} else {
    echo "no good!";
}
 
Array ( [0] => stdClass Object ( [d_id] => 3 [d_name] => Architecture ) [1] => stdClass Object ( [d_id] => 4 [d_name] => Landscape Architecture ) [2] => stdClass Object ( [d_id] => 1 [d_name] => Planning ) [3] => stdClass Object ( [d_id] => 2 [d_name] => Urban Design ) )
 
To give the rest of the picture, here's what leads up to this (using codeIgniter)

Code: Select all

//located in the model
function discipline()
{
    $query=$this->db->query("
        SELECT 
            d_id, d_name 
        FROM discipline
        ORDER BY d_name ASC");
    
    return $query->result();
}
 
// located in the controller
$disciplines = $this->Project_model->discipline();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Access array - stdClass Object Problem

Post by John Cartwright »

Simply casting your result set to an array will not make a difference because it is already an array (containing stdClass objects elements). You need to iterate your result set using a loop and compare the values directly.

I prefer to have my models return the result set grouped by the row's primary key, or in this case d_name (to group them accordingly).
psurrena wrote:

Code: Select all

//located in the model
function discipline()
{
    $query=$this->db->query("
        SELECT 
            d_id, d_name 
        FROM discipline
        ORDER BY d_name ASC");
    
    $result = $query->result();
    
    $return = array();
    foreach ($result as $row) {
       if (!array_key_exists($row['d_name'], $return)) {
          $return[$row['d_name']] = array();
       }    
       $return[$row['d_name']] = $row;
    }    
 
    return $return;
}
 
Then you can check for the existence of the key to check for a specific d_name
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

Oh yeah, my bad, I thought the whole thing was an object...

Yeah, there's no real way to convert that into an array except by looping through it.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

can't avoid a loop? that stinks!

Thanks guys!
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

One last thing...I got everything to work, just in a very obtuse way but, it illustrates what I'm going for. What we are doing is stopping the loop when the category id is true, since a category only needs to be listed once. What I am filtering out is: if there are no projects in a category, then the category will not appear.

Any help in cleaning it up / re-thinking would be appreciated.

Code: Select all

foreach($active as $item) {
    if($item->d_id == 1) {
        echo "Number ". $item->d_id;
        break;
    }
}
 
foreach($active as $item) {
    if($item->d_id == 2) {
        echo "Number ". $item->d_id;
        break;
    }
}
 
foreach($active as $item) {
    if($item->d_id == 3) {
        echo "Number ". $item->d_id;
        break;
    }
}
 
foreach($active as $item) {
    if($item->d_id == 4) {
        echo "Number ". $item->d_id;
        break;
    }
}
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

So you only want four to be displayed, and avoid empty values?

Code: Select all

$i = 0;
 
foreach($whatever as $something_else)
{
if($i == 3 || empty($some_else))
break;
else
$i++;
 
//do processing
}
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

No, I want each value, if it exists, to be displayed once.

10 projects might be architecture projects but I only need to have the title appear once.

My original idea was just to use in_array but since I found out I have to loop, it's making it a bit more confusing.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

Why not loop through, and append the title to an array if it's not already in the array?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

Here's the project in-progress:
http://tinyurl.com/qe6xf6

When I am in this category, I want the disciplines that do not have any projects associated to disappear.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

Sorry for being stupid...but I don't understand.

You're checking if the parent "discipline" has an child "disciplines"...by checking if the parent is one of the children??
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Access array - stdClass Object Problem

Post by psurrena »

Parent: Project Type
Child: Discipline

Not every Project Type contains all four Disciplines. I want only the Disciplines that contain a project for that Project Type to appear. It works just fine except, using the query below, it will list a discipline for each project so the list may look like:

Architecture
Architecture
Architecture
Architecture
Architecture
Architecture
Planning
Urban Design
Urban Design

I want:
Architecture
Planning
Urban Design

Code: Select all

    function categoryDiscipline($id)
    {
        $query = $this->db->query("
            SELECT 
                project.id, 
                discipline.d_name,
                discipline.d_id
            FROM project
            
            INNER JOIN project_category ON project_category.project_id = project.id
            INNER JOIN project_discipline ON project_discipline.project_id = project.id
            INNER JOIN discipline ON discipline.d_id = project_discipline.discipline_id
            
            WHERE project_category.category_id=$id
        
            ORDER BY project.name ASC
        ");
    
        return $query->result();
    }
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Access array - stdClass Object Problem

Post by John Cartwright »

If it's simply a matter for formatting the output, as suggested, just output the title everytime the category changes from the previous row (since your ordering by the category already).

Code: Select all

$lastcategory= '';
foreach ($disciplines as $row) {
   if ($lastcategory!= $row['d_name']) {
      echo '<h3>'. $row['d_name'] .'</h3>';
   }
   $lastcategory = $row['d_name'];
}
 
:?:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Access array - stdClass Object Problem

Post by jackpf »

Or you could just select DISTINCT d_name. Should work.
Post Reply