dongqiao6445 2014-08-07 13:24
浏览 33

更新php多维数组并重新保存到db

I have the following structure for my array which is dynamically generated:

Array
(
    [0] => Array
        (
            [friend_request_sender_id] => 1
            [friend_request_date] => 07-08-2014
            [friend_request_time] => 11:12:19
            [friend_request_recipient_id] => 5
            [friend_request_sent] => 1
            [friend_request_accepted] => 0
        )

    [1] => Array
        (
            [friend_request_sender_id] => 1
            [friend_request_date] => 07-08-2014
            [friend_request_time] => 11:12:47
            [friend_request_recipient_id] => 2
            [friend_request_sent] => 1
            [friend_request_accepted] => 0
        )

)

I would like to update the first array value [friend_request_accepted] => 0 to this: [friend_request_accepted] => 1 which i have achieved by doing the following:

$test = get_user_meta($current_user->ID,'get_user_friends', true );

    foreach($test as &$values){
    if($values['friend_request_recipient_id'] === '5'){
        $values['friend_request_accepted'] = '1';
        break; // Stop the loop after we've found the item
    }
}

However, i would like to actually save this new value into the database, overwriting the existing value. The array should then look like this:

Array
    (
        [0] => Array
            (
                [friend_request_sender_id] => 1
                [friend_request_date] => 07-08-2014
                [friend_request_time] => 11:12:19
                [friend_request_recipient_id] => 5
                [friend_request_sent] => 1
                [friend_request_accepted] => 1
            )

        [1] => Array
            (
                [friend_request_sender_id] => 1
                [friend_request_date] => 07-08-2014
                [friend_request_time] => 11:12:47
                [friend_request_recipient_id] => 2
                [friend_request_sent] => 1
                [friend_request_accepted] => 0
            )

    )

How should I do this?

  • 写回答

1条回答 默认 最新

  • dongyiluan1718 2014-08-08 14:07
    关注

    this line doesn't work:

    if($values['friend_request_recipient_id'] === '5'){
            $values['friend_request_accepted'] = '1';
            break; // Stop the loop after we've found the item
    }
    

    because

    [friend_request_recipient_id] => 5
    

    is an integer I think you just need to sure in condition because '===' is also checked the types of variable.

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗