简体   繁体   English

定义动态JSON数组

[英]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. 我知道我必须定义一个静态JSON数组,但是现在我必须使它动态化,这样我才能添加带有循环的项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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