-1

我以以下 php 脚本为例

    $stockline = array("details" => "This will hole all data for entire page" ) ;


    $date = "2013-20-20" ;  
    $named_array = array( "date" => $date  ) ;
        $named_array["vehicles"][] = array("vehicle" => "i100-1" , "driver" => "jack" , "events" => "boat race"  ) ;
        $named_array["vehicles"][] = array("vehicle" => "i100-2" , "driver" => "paul" , "events" => "boat race"  );
        $named_array["vehicles"][] = array("vehicle" => "i60-2" , "driver" => "andrew" , "events" => "popping"  );  

    $stockline['day'] = array( $named_array ) ;


    // I cant add this next data as an array to 'day' 
    $date = "2013-20-21" ;  
    $named_array = array( "date" => $date  ) ;
        $named_array["vehicles"][] = array("vehicle" => "i100-1" , "driver" => "jack" , "events" => "Tuesday"  ) ;
        $named_array["vehicles"][] = array("vehicle" => "i100-2" , "driver" => "paul" , "events" => "Tuesday"  ) ;
        $named_array["vehicles"][] = array("vehicle" => "i60-2" , "driver" => "andrew" , "events" => "Tuesday"  ) ;



    $rs =  json_encode($stockline);

    echo $rs ;

当我使用控制台日志时,我得到了这个,这正是我想要的

    Object {details: "This will hole all data for entire page", day: Array[1]}
    day: Array[1]
    0: Object
    length: 1

如何将我的第二组数组添加到 'day' ?所以我最终得到

   Object {details: "This will hole all data for entire page", day: Array[2]}
   day: Array[2]
   0: Object
   1: Object
   length: 2

我在这里有点超出我的深度,所以任何帮助将不胜感激,谢谢

4

1 回答 1

1
<?php
$stockline = array("details" => "This will hole all data for entire page" ) ;
$stockline['day'] = array();

$date = "2013-20-20" ;  
$named_array = array( "date" => $date  ) ;
$named_array["vehicles"][] = array("vehicle" => "i100-1" , "driver" => "jack" , "events" => "boat race"  ) ;
$named_array["vehicles"][] = array("vehicle" => "i100-2" , "driver" => "paul" , "events" => "boat race"  );
$named_array["vehicles"][] = array("vehicle" => "i60-2" , "driver" => "andrew" , "events" => "popping"  );  

$stockline['day'][] = $named_array;

//second array 
$date = "2013-20-21" ;  
$named_array = array( "date" => $date  ) ;
$named_array["vehicles"][] = array("vehicle" => "i100-1" , "driver" => "jack" , "events" => "Tuesday"  ) ;
$named_array["vehicles"][] = array("vehicle" => "i100-2" , "driver" => "paul" , "events" => "Tuesday"  ) ;
$named_array["vehicles"][] = array("vehicle" => "i60-2" , "driver" => "andrew" , "events" => "Tuesday"  ) ;

$stockline['day'][] = $named_array;


$rs =  json_encode($stockline);

echo $rs;
?>
于 2013-03-20T21:05:54.883 回答