简体   繁体   中英

Define dynamic JSON-Array

i've got a new problem.

I know how i have to define a static JSON-Array, but now i must make this dynamic so i could add items with a loop.

this is the static version:

$json = array(array('field' => 'name', 
                    'value' => $name), 
          array('field' => 'nummer', 
                    'value' => $numbers));

echo json_encode($json );

and now i got this, but it doesn't work this way:

$element_array = array($element_array);
array_push($element_array, 'field' => 'name', 'value' => $name);
array_push($element_array, 'field' => 'nummer', 'value' => $numbers);

$json = $element_array;

any idea what's the problem?

You want to have not single array, but arrays in array, so:

    $element_array = array();
    $element_array[] = array( 'field' => 'name', 'value' => $name );
    $element_array[] = array( 'field' => 'nummer', 'value' => $numbers);

//and so on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM