how to create array php into blank braces

Viewed 19

i'm trying to make a php array, i need to create array with blank braces variable like {}, but if i use array() the data shows [] square bracket, how to create variable into {}

php code:

<?php

    $newArray = array("name"=>"SMK Bina Rahayu", "grade"=>"below", "parent"=>array());

    $afterEncode = json_encode($newArray);

    print_r($afterEncode);

?>

the result :

{
  "name": "SMK Bina Rahayu",
  "grade": "below",
  "parent": [
    
  ]
}

the result that i need or i want:

{
  "name": "SMK Bina Rahayu",
  "grade": "below",
  "parent": {}
}

is it possible to make the blank array from square bracket into braces? i need to change it because i want to use cloudflare api that only accept braces in blank variable and not square brackets

1 Answers

A couple of ways I can think of ... The {} indicated an object, so

$newArray = array("name"=>"SMK Bina Rahayu", "grade"=>"below", "parent"=>(object)array());
$afterEncode = json_encode($newArray);
// or
$newArray = array("name"=>"SMK Bina Rahayu", "grade"=>"below", "parent"=> new stdClass);
$afterEncode = json_encode($newArray);
print_r($afterEncode);

RESULT

{"name":"SMK Bina Rahayu","grade":"below","parent":{}}
Related