如何从数组中删除此" 0":{

时间:2016-11-11 15:18:49

标签: php arrays json multidimensional-array

$test = array('hola'=>'mundo','salami'=>'frito');
$data = array(
    'gift_promoted' => '',
    'questions' => array(
        0 => array(
            'extra' => array()
        )
    ),
    'quiz' => array(
        'extra' => array(),
        $test
    )
);

print json_encode($data);

实际结果:

{"gift_promoted":"","questions":[{"extra":[]}],"quiz":{"extra":[],"0":{"hola":"mundo","salami":"frito"}}}

annotated screenshot

我需要这个:

{"gift_promoted":"","questions":[{"extra":[]}],"quiz":{"extra":[],"hola":"mundo","salami":"frito"}}

1 个答案:

答案 0 :(得分:0)

有点脏:

$test = array('hola'=>'mundo','salami'=>'frito'); $data = array(
     'gift_promoted' => '',
     'questions' => array(
         0 => array(
             'extra' => array()
         )
     ),
     'quiz' => array(
         'extra' => array(),
         //$test --> remove this.
     ) );

 foreach($test as $key => $value) {     $data['quiz'][$key]=$value; }

 print json_encode($data);