Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joshblevins's avatar

Add values to php array.

I am trying to add values to php array on a change of radio buttons. This is what I have in my controller which works to create the array when it the db cell is null. However anytime I try to add to that array it will not add the next set of values.

public function selfEvalResponseUpdate(Request $request, $id)
    {
        //dd($request->questionId);
        $eval = EmployeeeSelfEvaluation::find($id);

        if(!$eval->responses){

            $array = array(
                'questionId' => $request->questionId ,
                'responseId' => $request->responseId
            );

            $eval->responses = serialize($array);
            $eval->update();

        }else{
            $array = unserialize($eval->responses);

            $response = array(
                'questionId' => $request->questionId ,
                'responseId' => $request->responseId
            );

            $array = array_push($array, $response);

            $eval->responses = serialize($array);
            $eval->update();
            
        }
    }
0 likes
2 replies
bobbybouwmann's avatar
Level 88

Well, first of all when you create the item it will store one array with the values. In the update you push a new array to it. However that will result in the following structure

[
	"test" => "test",
	"random" => "random",
	0 => [
		"test" => "test",
		"random" => "random",
	],
]

If you only want to store arrays you need to do something like this

if (! $eval->responses) {
    $array = array(
		array(
			'questionId' => $request->questionId ,
			'responseId' => $request->responseId
		)
	);
} else {
	$response = array(
		'questionId' => $request->questionId ,
		'responseId' => $request->responseId
	);

	$array = array_push($array, $response);
}

The second problem with your code is the return value of array_push. It returns the number of items in the array instead of the array. So you're not serializing the array anymore but you're serializing an integer.

You can simply fix it by just calling array_push and not assigning it to a value

array_push($array, $response);

$eval->responses = serialize($array);

Let me know if this make sense ;)

1 like
Ap3twe's avatar

Try this

$eval = EmployeeeSelfEvaluation::find($id);

    if(!$eval->responses){

        $array = array(
            'questionId' => $request->questionId ,
            'responseId' => $request->responseId
        );

        $eval->responses = serialize($array);
        $eval->update();

    }else{
        $array = unserialize($eval->responses);

        $response = array(
            'questionId' => $request->questionId ,
            'responseId' => $request->responseId
        );

        $arrayDb = array_push($array, $response);

        $eval->responses = serialize($arrayDb);
        $eval->update();
        
    }
}

Please or to participate in this conversation.