Access array - stdClass Object Problem
Moderator: General Moderators
Access array - stdClass Object Problem
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 ) )
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 ) )
Re: Access array - stdClass Object Problem
Just type cast the object to (array).

Re: Access array - stdClass Object Problem
Thanks for the response. I tried that with no success. Am I doing something wrong?
To give the rest of the picture, here's what leads up to this (using codeIgniter)
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 ) )
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();- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Access array - stdClass Object Problem
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).
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).
Then you can check for the existence of the key to check for a specific d_namepsurrena 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; }
Re: Access array - stdClass Object Problem
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.
Yeah, there's no real way to convert that into an array except by looping through it.
Re: Access array - stdClass Object Problem
can't avoid a loop? that stinks!
Thanks guys!
Thanks guys!
Re: Access array - stdClass Object Problem
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.
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;
}
}Re: Access array - stdClass Object Problem
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
}Re: Access array - stdClass Object Problem
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.
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.
Re: Access array - stdClass Object Problem
Why not loop through, and append the title to an array if it's not already in the array?
Re: Access array - stdClass Object Problem
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.
http://tinyurl.com/qe6xf6
When I am in this category, I want the disciplines that do not have any projects associated to disappear.
Re: Access array - stdClass Object Problem
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??
You're checking if the parent "discipline" has an child "disciplines"...by checking if the parent is one of the children??
Re: Access array - stdClass Object Problem
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
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();
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Access array - stdClass Object Problem
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'];
}
Re: Access array - stdClass Object Problem
Or you could just select DISTINCT d_name. Should work.