将数组推入JSON但得到了不需要的结果

I suck at formatting, maybe I had a bad foundation. I have a json like this

'first_name'=>'steve', 'msg'=>'something here','profile_id'=>1

and I want to push a new item into it, I wrote

$i = array('first_name'=>'steve', 'msg'=>'something here','profile_id'=>1);
$loginId = array($_GET['login_id']);
array_push($i,$loginId);

echo json_encode($i);

The result I got is strange:

$i = array('first_name'=>'steve', 'msg'=>'something here','profile_id'=>1);
$loginId = $_GET['login_id'];

$i['login_id']=$loginId;    
echo json_encode($i);

The reason array_push didn't work is because you are treating $i as an array (collection) of objects (arrays), while it is just a key-value list (map).

If the array is like K1=>V1, K2=>V2, use $arr[K3]=V3 to add another pair.

If the array is like [(k1,v1), (k2,v2)], then array_push($arr,(k3,v3));

You are basically taking a value $_GET['login_id'], placing it in an array and trying to push it into an associate array, so you you get a new numeric index 0 holding a nested array, which in turn holds your value.

If you want the entire thing to be treated uniformly as an associative array (or an object once converted to JSON) then you should do something like:

$i = array('first_name'=>'steve', 'msg'=>'something here','profile_id'=>1);
$i['login_id'] = $_GET['login_id'];
echo json_encode($i);