Hope that title made sense but my issue is i have an object with an array like this, this is just 1 from the array as an example
object(stdClass)#6 (3) {
["items"]=>
array(40) {
[0]=>
object(stdClass)#7 (22) {
["id"]=>
int(46)
["parentId"]=>
int(0)
["name"]=>
string(22) "Complete monthly wages"
["description"]=>
string(294) "Complete monthly wages<span style=""></span><br /><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div><div>Complete monthly wages<span style=""></span><br /></div>"
["tags"]=>
string(0) ""
["projectId"]=>
int(12)
["ownerId"]=>
int(1)
["groupId"]=>
int(0)
["startDate"]=>
string(19) "2012-09-03T00:00:00"
["priority"]=>
int(2)
["progress"]=>
float(0)
["status"]=>
int(10)
["createdAt"]=>
string(19) "2012-09-03T07:35:21"
["updatedAt"]=>
string(19) "2012-09-03T07:35:21"
["notifyProjectTeam"]=>
bool(false)
["notifyTaskTeam"]=>
bool(false)
["notifyClient"]=>
bool(false)
["hidden"]=>
bool(false)
["flag"]=>
int(0)
["hoursDone"]=>
float(0)
["estimatedTime"]=>
float(0)
["team"]=>
object(stdClass)#8 (3) {
["items"]=>
array(2) {
[0]=>
object(stdClass)#9 (1) {
["id"]=>
int(2)
}
[1]=>
object(stdClass)#10 (1) {
["id"]=>
int(1)
}
}
["count"]=>
int(2)
["total"]=>
int(2)
}
}
As we can see it has a team section and this is my focus
["team"]=>
object(stdClass)#8 (3) {
["items"]=>
array(2) {
[0]=>
object(stdClass)#9 (1) {
["id"]=>
int(2)
}
[1]=>
object(stdClass)#10 (1) {
["id"]=>
int(1)
}
}
["count"]=>
int(2)
["total"]=>
int(2)
}
}
As you can see there is 2 id's in there, 1 and 2, there could be anything up to 30 or so but i cant figure out how to efficently tell it to search the whole array.
If i use this it works as long as the Id 1 happens to be the first item in id but thats obviously not always the case. My aim is to search through the object and just run code IF the users ID is in the team array, im new to php and objects in particular so hope someone can point me in the right direction
foreach($tasksList->items as $task_details)
{
if($task_details->team->items->id === 1)
{
echo "My Code";
}
}
Your code (and the [team]
section you pasted above) seems to miss the fact that team->items
is an array. In the top sample, it looks like:
["team"]=>
object(stdClass)#8 (3) {
["items"]=>
/* It's an array! */
array(2) {
...
}
}
It won't have an id
property directly. Instead, you need to iterate over team->items
:
foreach ($taskList->items as $task_details) {
foreach ($task_details->team->items as $key => $value) {
echo "Team item #: $key ... Team id: $value->id
";
if ($value->id === 1) {
echo "Matched Team ID 1
";
}
}
}