views:

29

answers:

1

Hey,

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?

+1  A: 

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