简体   繁体   English

使用foreach循环将数组转换为json

[英]Convert Array to json using foreach loop

I am Trying to convert array to json but not getting exact result I am looking for. 我正在尝试将数组转换为json,但没有得到我想要的确切结果。

Here, 这里,

<?php
      $result=array();
      $result[status]=1;
      $data=array(
                array("ucode" => "123","name" => "abc","lname" => "xyz"),
                array("ucode" => "431","name" => "cdb","lname" => "zsa")
              );
      foreach($data as $res){ 
          $data=array();
           $data[ucode]=$res['ucode'];
           $data[name]= $res['name'];
           $data[lname]= $res['lname'];
           $result[content]=$data;
        }

echo $res=json_encode($result);

?>

Actul Result: 实际结果:

{"status":1,"content":{"ucode":"431","name":"cdb","lname":"zsa"}}

My expected Result: 我的预期结果:

{"status":1,"content":[{"ucode":"123","name":"abc","lname":"xyz"},{"ucode":"431","name":"cdb","lname":"zsa"}]}

please, Guide me where is mistake, not getting the expected result. 请,指导我哪里出错了,没有得到预期的结果。

Why need loop, if you can directly push data into content index of result. 如果您可以直接将数据推入结果的内容索引,那么为什么需要循环。

$result         = [];
$result["status"] = 1;
$data           = [
    ["ucode" => "123", "name" => "abc", "lname" => "xyz"],
    ["ucode" => "431", "name" => "cdb", "lname" => "zsa"],
];
$result['content'] = $data;
echo $res = json_encode($result);

Short form of it, 简而言之,

$result = ['status' => 1, 'content' => $data];
echo json_encode($result);

Working demo . 工作演示

Output 输出量

{"status":1,"content":[{"ucode":"123","name":"abc","lname":"xyz"}, 
 {"ucode":"431","name":"cdb","lname":"zsa"}]}

Your reusing the variable $data which is causing your problem. 您重用变量$data会导致您的问题。 Also when you append to the $result['content'] array, you need to use [] . 同样,当您追加到$result['content']数组时,也需要使用[]

<?php
    $result = array(
        'content' => array(),
        'status' => 1
    );
    $data= array(
        array("ucode" => "123","name" => "abc","lname" => "xyz"),
        array("ucode" => "431","name" => "cdb","lname" => "zsa")
    );
    foreach($data as $res){ 
        $tmp = array(
            'ucode' => $res['ucode'],
            'name' => $res['name'],
            'lname' => $res['lname']
        );
        $result['content'][] = $tmp;
    }
    echo $res = json_encode($result);
?>

I got another way solution, just with you guys, 我得到了另一种解决方案,就像你们一样

because I want rename my variables names when pass in api with json_encode(). 因为我想在使用json_encode()传递api时重命名变量名称。

  <?php
            $result=array();
            $result['status']=1;
            $data=array(
                      array("ucode" => "123","name" => "abc","lname" => "xyz"),
                      array("ucode" => "431","name" => "cdb","lname" => "zsa"),
                    );
            $ar=array();
            foreach($data as $res){
                $data=array();
                 $data['u_code']=$res['ucode'];
                 $data['u_name']= $res['name'];
                 $data['u_lname']= $res['lname'];
                 $ar[]=$data;
              }
            $result['content']=$ar;
            echo $res=json_encode($result);

      ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM