简体   繁体   中英

Convert Array to json using foreach loop

I am Trying to convert array to json but not getting exact result I am looking for.

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. Also when you append to the $result['content'] array, you need to use [] .

<?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().

  <?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);

      ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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