我使用 for循环
创建了一个无数天的数组"use strict";
var app = angular.module( "app",[],function( $routeProvider ) {
$routeProvider.when( "/visitplace", {
templateUrl: "placetovisit.html",
controller: "TourCoordinatorCtrl",
resolve: {
"accommodation": function( $q, $timeout ) {
var myFriend = $q.defer();
$timeout(function(){
myFriend.resolve({
hotelName: function( ) {
return "My Friend's friend's hotel";
},
roomNo: function( ) {
return "404";
}
});
},5000);
return myFriend.promise;
}
}
});
} );
app.controller( "TourCoordinatorCtrl", function( $scope, accommodation ) {
$scope.name = "Shidhin";
$scope.place = "Switzerland";
$scope.hotel = accommodation.hotelName( );
$scope.roomno = accommodation.roomNo( );
} );
并将此数组转换为json
$noOfDays=array();
for($i=1;$i<=31;$i++){
if ($i == '1') {
$noOfDays[$i]['Number']=$i;
$noOfDays[$i]['Days']=$i.' Day';
}else{
$noOfDays[$i]['Number']=$i;
$noOfDays[$i]['Days']=$i.' Days';
}
}
$ noOfDaysjson返回json如下: -
echo $noOfDaysjson= json_encode($noOfDays);
但我不想要这个。显示为1,2,以json格式粗体显示
我怎样才能得到json: -
{**"1"**:{"Number":1,"Days":"1 Day"},**"2"**:{"Number":2,"Days":"2 Days"},**"3"**:{"Number":3,"Days":"3 Days"},**"4"**:{"Number":4,"Days":"4 Days"},"5":{"Number":5,"Days":"5 Days"},"6":{"Number":6,"Days":"6 Days"},"7":{"Number":7,"Days":"7 Days"},"8":{"Number":8,"Days":"8 Days"},"9":{"Number":9,"Days":"9 Days"},"10":{"Number":10,"Days":"10 Days"},"11":{"Number":11,"Days":"11 Days"},"12":{"Number":12,"Days":"12 Days"},"13":{"Number":13,"Days":"13 Days"},"14":{"Number":14,"Days":"14 Days"},"15":{"Number":15,"Days":"15 Days"},"16":{"Number":16,"Days":"16 Days"},"17":{"Number":17,"Days":"17 Days"},"18":{"Number":18,"Days":"18 Days"},"19":{"Number":19,"Days":"19 Days"},"20":{"Number":20,"Days":"20 Days"},"21":{"Number":21,"Days":"21 Days"},"22":{"Number":22,"Days":"22 Days"},"23":{"Number":23,"Days":"23 Days"},"24":{"Number":24,"Days":"24 Days"},"25":{"Number":25,"Days":"25 Days"},"26":{"Number":26,"Days":"26 Days"},"27":{"Number":27,"Days":"27 Days"},"28":{"Number":28,"Days":"28 Days"},"29":{"Number":29,"Days":"29 Days"},"30":{"Number":30,"Days":"30 Days"},"31":{"Number":31,"Days":"31 Days"}}
答案 0 :(得分:4)
你需要的json表示你想要一个对象数组,如果那是你真正想要的,那么这将获得所需的结果。
<?php
$noOfDays=array();
for($i=1;$i<=31;$i++){
$obj = new stdClass();
$obj->Number = $i;
if ($i == '1') {
$obj->Days = $i. ' Day';
}else{
$obj->Days = $i. ' Days';
}
$noOfDays[] = $obj;
}
$noOfDaysjson = json_encode($noOfDays);
echo $noOfDaysjson;
答案 1 :(得分:2)
这应该适合你:
<?php
$noOfDays=array();
for($i=1;$i<=31;$i++){
if ($i == '1') {
$noOfDays[] = [
'Number' => $i,
'Days' => $i. ' Days'
];
}else{
$noOfDays[] = [
'Number' => $i,
'Days' => $i. ' Day'
];
}
}
echo $noOfDaysjson= json_encode($noOfDays);
答案 2 :(得分:1)
最短且优化的方式:
$noOfDays = [];
for($i = 1; $i <= 31; $i++){
$noOfDays[] = [
'Number'=> $i,
'Days' => $i . (($i == 1)? ' Day': ' Days' )
];
}
答案 3 :(得分:0)
让你的数组多维,试试这个:
<?php
$noOfDays=array();
for($i=1;$i<=31;$i++){
$noOfDays[] = [
'Number'=>$i,
'Days'=>$i. ($i == '1') ? ' Day' : ' Days'
];
}
echo $noOfDaysjson= json_encode($noOfDays);
答案 4 :(得分:0)
这里的所有答案都是正确的......
// in case you would like to change your array keys
define ('ARRAY_NUMBER', 'Number');
define ('ARRAY_DAYS', 'Days' );
// i think it's more clean to avoid if else 31 times
$noOfDays = array();
$noOfDays[] = array(ARRAY_NUMBER => 1,
ARRAY_DAYS => '1 Day');
for ($i=2 ; $i<=31 ; $i++) {
$noOfDays[] = array(ARRAY_NUMBER => $i,
ARRAY_DAYS => $i . ' Days');
}
var_dump(json_encode($noOfDays));