0

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.

9
  • 3
    MAYBE Because of the multiple syntax errors in that code Commented May 14, 2019 at 11:09
  • @jeroen ok let me try thank you Commented May 14, 2019 at 11:11
  • 1
    @jeroen , working perfect Thank you Commented May 14, 2019 at 11:13
  • 4
    Or just $result = ['status' => 1, 'content' => $data ]; Commented May 14, 2019 at 11:14
  • 4
    @NigelRen And wrapp a json_encode() round that and Bob's yer Uncle Commented May 14, 2019 at 11:15

3 Answers 3

5

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"}]}
Sign up to request clarification or add additional context in comments.

2 Comments

Working perfectly for me
I think as the original answer did not have any explanation with it was (I think) the problem.
3

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

4 Comments

What is the need of loop in your code, can you explain?
@quickSwap There is no need. But I suspect this is not exactly what the OP is trying to do in their original code. It's probably a simplified version.
Yes but the actual code probably is not like that. I suspect the $data is just for our benifit. It's probably coming from a database or some other source. Either way, if the OP is actually just looping over the same data pointlessly then the other answers are showing he doesn't need to do that. I'm just showing another point of view.
Yes, It is coming from database
0

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

      ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.