dongtuojuan8998
2018-03-02 09:23
浏览 91

如何在json中转换for循环结果

I Want to convert this php results in to JSON.

for ($i = 0; $i < (count($getnodays)); $i++)
{
    $category = $getctgry[$i];
    $Priority = $getpriotity[$i];
    $NoOfDays = $getnodays[$i];
    $StDate = $date1[$i];
    $EdDate = $date2[$i];
}

Results Required to be in :

{ 
    "Category": "Category", 
    "Priority": "Priority1", 
    "StDate": "1/1/2016", 
    "EdDate": "5/1/2018" 
},{ 
    "Category": "Category", 
    "Priority": "Priority1", 
    "StDate": "1/1/2016", 
    "EdDate": "5/1/2018" 
},{ 
    "Category": "Category", 
    "Priority": "Priority1", 
    "StDate": "1/1/2016", 
    "EdDate": "5/1/2018" 
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhuxun5136 2018-03-02 09:33
    已采纳

    In your loop, create a new array occurance each iteration.

    Then use json_encode() to turn that array into a valid JSON string.

    $json = [];
    for ($i = 0; $i < (count($getnodays)); $i++) {
        $json[] = ['Category'   => $getctgry[$i],
                    'Priority'  => $getpriotity[$i],
                    'NoOfDays'  => $getnodays[$i],
                    'StDate'    => $date1[$i],
                    'EdDate'    => $date2[$i],
                ];
    }
    echo json_encode($json);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题