dse3168
2019-07-09 13:35
浏览 541
已采纳

Laravel - 根据键值删除数组中的对象 - array_filter

I'm having trouble removing an object inside of this array.

First I get the $id that is to be removed from the array. But when I'm filtering throw the array its appending keys to it.

So the logic ll no longer work on the rest of the application.

How can I maintain the same syntax on the options object after removing the object inside of the cart array ?

enter image description here

    public function destroy( $id,  Request $request )
    {
        $user = $this->user ;

        $data = array_filter( $user->options->cart , function ( $option ) use ( $id ) {
            if ( $option->product_id ==  $id ) {
                return false;
            }
            return json_encode($option);
        });

        //dd($user->options->cart);
        //dd($data);

        $user->options = (object)['cart' => $data ];
        $user->save() ;
        return response()->json( $user , 200 ) ;

    }

Solved :

    public function destroy( $id,  Request $request )
    {
        $user = $this->user ;

        $data = array_filter( $user->options->cart , function ( $option ) use ( $id ) {
            if ( $option->product_id ==  $id ) {
                return false;
            }
            return true;
        });


        $user->options = ['cart' => array_values( $data ) ];
        $user->save() ;
        return response()->json( $user , 200 ) ;

    }
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dosro793520628 2019-07-09 13:43
    已采纳

    if i understood u right , u want to rearrange the array after u do your logic plus keeping the structure , i would suggest you to use array_values

    $new_data= array_values($data);
    

    and if u got an error that its not an array although i doubt that just use the toArray() method

    $new_data= array_values($data->toArray());
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtv995719 2019-07-09 13:43

    It seems like (object)['cart' => $data ] is somehow changing your array.

    Setting the property directly should work:

    $user->options->cart = $data;
    

    Also, return json_encode($option); doesn't have any real effect except making the execution slower. You can just return true;.

    评论
    解决 无用
    打赏 举报
  • duandang9434 2019-07-09 14:01

    Looking at your JSON encoding, I see that your options object on the bottom left is an object that contains a property, cart, which is an array. Your options object on the bottom right is an object that contains a property, cart, which is an object that contains a property for each numeric index.

    I'm not at all certain, but I think the problem might be that the array_filter function preserves the array keys:

    If the callback function returns TRUE, the current value from array is returned into the result array. Array keys are preserved.

    I suggest you try using some approach that does not try to preserve array keys so that your filtered array has contiguous, numeric values.

    public function destroy( $id,  Request $request )
    {
        foreach($this->user->options->cart as $key => $cart_item) {
            if ($cart_item->product_id == $id) {
                unset($this->user->options->cart[$key]);
            }
        }
    
        $user->save() ;
        return response()->json( $user , 200 ) ;
    
    }
    

    EDIT: I am not privy to the details of your implementation (I don't know what type of object $user is or what $user->save or $response->json() might do) but this code will remove an array element by product_id:

    $arr = array(
            (object)["product_id" => 819, "name" => "I am 819"],
            (object)["product_id" => 820, "name" => "I am 820"],
            (object)["product_id" => 821, "name" => "I am 821"],
            (object)["product_id" => 822, "name" => "I am 822"]
    );
    
    
    foreach($arr as $key => $v) {
            if ($v->product_id == 820) {
                    unset($arr[$key]);
            }
    }
    
    var_dump($arr);
    
    
    评论
    解决 无用
    打赏 举报