3

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 Answers 3

4

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());
Sign up to request clarification or add additional context in comments.

Comments

1

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);

2 Comments

This solution doesn't work. The data of $this->user->options->cart is the same. Nothing is removed. The unset function doesn't do anything in this case.
@bmpf_pt oh that's too bad. I've edited my post to clarify my thinking. I'm not aware of how your underlying code is implemented. Weird that it doesn't work.
0

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;.

1 Comment

This its not changing anything :/ $user->options->cart = $data;

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.