0

I will be honest, I have just started learning objects and I am stuck.

I want to loop through an array of objects and display the name and description for each. But either nothing is displayed or it displays all the names first and then all the descriptions.

I am pulling the information from an API into the object:

// get tasks
foreach($tasksList->items  as $task_details) {
    $tasks_name[$task_details->id]=$task_details->name;
    $tasks_desc[$task_details->id]=$task_details->description;
    $tasks_details[$task_details->id]=$task_details->id;
    $tasks_progress[$task_details->id]=$task_details->progress;
}

foreach($tasks_name as $taskid=>$my_task_name) {
    echo "Task_Name: " . $my_task_name . "</br>";
    $task_id = $task_details->id;

    foreach($tasks_desc as $taskid1=>$my_task_desc) {
        if($taskid==$task_details->id) {
            echo "Task_Desc: " . $my_task_desc . "</br>";   
        }
    }   
}

Now what I don't understand is: inside the first foreach loop it is like a while loop while i=0, it checks $tasks_name[0], then $tasks_name[1] and so on.

However, I am not sure how to figure out what the id is in the current loop it is on, so I can tell it to only print the description of the current loop and not display all of them.

To be honest I am copying this from another example, but I don't quite understand it. I plan to study objects more, but this is holding me up on my current code:

foreach($tasks_desc as $taskid1=>$my_task_desc)

I understand it is looping through all the $tasks_desc and assigning the value to $my_task_desc but what is the signifigance of $taskid?

Sorry for the newbie questions. :)

2
  • Can we see a snippet of your object print_r($yourObject) Commented Sep 1, 2012 at 14:39
  • you dont need the second loop. $tasks_desc[$task_details->id] will do it for you. Commented Sep 1, 2012 at 14:41

2 Answers 2

1

This is poorly written. I would move on to another tutorial if it's a tutorial. Either that or the line that task_id is used was left out.

Anyway, tasks_name is an array of tasks indexed by the ID. So the outer loop in the second block loops over the keys/values of that array (the ID is the key, taskid. It was assigned by $task_details->id in the first loop above).

The second loop goes over all of the tasks again, but this time by description task_desc instead of by name. It's trying to find the task_desc with an ID that matches the ID of the task_name before (which would make it the same task).

That's unnecessary, though, because you could just store all of the entries (name, desc, etc.) in one array indexed by the ID instead of storing each in their own array:

(this is the first loop):

foreach($tasksList->items  as $task_details) {
   $all_tasks[$task_details->id]['name'] = $task_details->name;
   $all_tasks[$task_details->id]['desc'] =$task_details->description;
   // Don't need the ID again; it's the key
   $all_tasks[$task_details->id]['progress'] = $task_details->progress;
}

However, you don't even need to do that, because you can just iterate over tasksList->items when you need to.

Sign up to request clarification or add additional context in comments.

Comments

1

There's no need for two loops. To display name and description for each object, one loop is enough:

foreach($tasksList->items  as $task_details) {
    echo 'name: ', htmlspecialchars($task_details->name);
    echo ', description: ', htmlspeciachars($task_details->description);
}

(I also don't understand why you want to store every object field in its own array first?)

Comments

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.