2016-11-11 98 views
-1
$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); 

實際結果:如何刪除這個 「0」:{從陣列

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

annotated screenshot

我需要的是這樣的:

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

你可以使用'unset'。 –

+0

重複是錯誤的。這個問題是關於將[0 => $ v]更改爲$ v,這不是完全刪除一個元素。 – SOFe

+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);