invalid json resulted by foreach loop

前端 未结 1 395
时光说笑
时光说笑 2021-01-27 12:46

says I want to get a value of something and store in into an arr to produce json :

foreach($item as $items){
   $items = $someting->name;

$arr = array(
   \'         


        
相关标签:
1条回答
  • 2021-01-27 13:20

    In most cases json_encode should be called once only. Keep adding all you want to convert to JSON in a php array, then call json_encode

    $json = array('items' => array());
    foreach ($items as $item) {
        $json['items'][] = array(
           'itemName' => $item->name
        );
    };
    
    echo json_encode($json);
    

    You could also call json_encode for all your $items array without doing a loop

    json_encode($items)

    0 讨论(0)
提交回复
热议问题