-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

2番目の配列セットを「日」に追加するにはどうすればよいですか?だから私は

   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 に答える