0

Let's say I need to update one of the [status] array values in the returned Array below.

In PHP I will get the array returned below into this variable $taskArray

The only thing I will have is the [taskid] of the Array item I need to modify and the new value for [status] that I would like to change it to.

I am looking for the most efficient way that I can take the array below, find the array that matches my [taskid] and change the [status] to my new status value and then return the complete updated array to a variable so I can pass it back to my database.

I'm really not sure how I can do that based on how the array is setup, I would appreciate any help in doing this please?

Based on this array below, I would like to basically pass in these 2 variable into a function and have the function make the updates mentioned above and return the whole updated array...

function updateTaskStatus($taskId, $newStatus){

    // Contains the Array that is shown below this function
    $tasksArray;

    // Update $tasksArray [status] with the value of $newStatus
    // WHERE $taskId is in the SAME array

    // RETURN UPDATED $tasksArray
    return $tasksArray;

}


// Calling function above would update the [status] to 'completed 
// WHERE [taskid] = 4
updateTaskStatus(4, 'Completed');



Array
(
    [0] => Array
        (
            [taskid] => 3
            [name] => sdgsdfgdfg
            [description] => dfgsdfgsdfg
            [status] => In Progress
            [priority] => Low
            [type] => Magento
        )

    [1] => Array
        (
            [taskid] => 4
            [name] => Dfgyrty
            [description] => rtyrty
            [status] => Open
            [priority] => Urgent
            [type] => Design
        )

    [2] => Array
        (
            [taskid] => 9
            [name] => yrgrtyerty
            [description] => rtyrt6yerty
            [status] => Cancelled
            [priority] => Urgent
            [type] => Magento
        )

    [3] => Array
        (
            [taskid] => 9
            [name] => ertgsdftg
            [description] => dfgsdfg
            [status] => Open
            [priority] => Medium
            [type] => SEO
        )

    [4] => Array
        (
            [taskid] => 30
            [name] => fghdfgh
            [description] => fghdfgh
            [status] => In Progress
            [priority] => Low
            [type] => SEO
        )

    [5] => Array
        (
            [taskid] => 1410858495187
            [name] => tyrty
            [description] => tyrty
            [status] => Open
            [priority] => Low
            [type] => Other
        )

)
0

4 Answers 4

2

If I understood your question, the simple answer is to do a loop like this:

function updateTaskStatus($taskId, $newStatus){
    global $tasksArray;
    foreach($tasksArray as $k => $task)
    {
      if ($task['taskid'] == $taskId)
      {
        $tasksArray[$k]['status'] = $newStatus;
        break;
      }
    }
    return $tasksArray;
  }

Note there is other solution (see php documentation with all array_* functions).

I added global to your code because otherwise this would not work, but using global is something to avoid everytime you can.

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

2 Comments

Good point on the global however i'll be either passing the array into my function or setting it on a class variable, I can't stamd using globals, thanks for the answer
If you define how the $tasksArray is made, you can also take the answer of @danielg44k and set the taskId as key of your array.
1

The easiest way to do this sort of thing is using a loop.

<?php

function updateTaskStatus($taskId, $newStatus){

    // Loop through all the tasks to see if there's a match
    foreach ($tasksArray as $id => $task) {
        if ($task['taskid'] != $taskId) {
            // Mismatch
            continue;
        }

        $tasksArray[$id]['status'] = $newStatus;
    }

    // RETURN UPDATED $tasksArray
    return $tasksArray;

}

1 Comment

Good example, I thought it was something simple like this but I couldn't remember how to do this for the life of me, time for some sleep thanks for the answer!
1

You can do it like this:

foreach ($tasksArray as $task)
    if ($task['taskid'] == $taskId)
        $task['status'] = $newStatus;

Comments

0

Change key to task id and then use something like this

function updateTaskStatus($taskId, $newStatus){
  $tasksArray[$taskId]['status'] = $newStatus;
}

updateTaskStatus(4, 'Completed');

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.