JasonDavis
JasonDavis

Reputation: 48983

PHP function to update array value where part of array matches other part of array?

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
        )

)

Upvotes: 0

Views: 309

Answers (4)

iSWORD
iSWORD

Reputation: 808

You can do it like this:

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

Upvotes: 1

Asenar
Asenar

Reputation: 7030

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.

Upvotes: 2

Coded Monkey
Coded Monkey

Reputation: 605

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;

}

Upvotes: 1

danielg44k
danielg44k

Reputation: 55

Change key to task id and then use something like this

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

updateTaskStatus(4, 'Completed');

Upvotes: 0

Related Questions