6

I want to achieve below array format:

{
    "success": true,
    "results": [
      {
        "name"  : "Choice 1",
        "value" : "value1",
        "text"  : "Choice 1"
      },
      {
        "name"  : "Choice 2",
        "value" : "value2",
        "text"  : "Choice 2"
      }
    ]
}

However, I am using PHP and a foreach loop, to return some values from my database:

//Search for clients in our database.
$stmt = $dbh->prepare("SELECT * FROM customers");
$stmt->execute();
$showAll = $stmt->fetchAll();

I then have my first part of the array, and my foreach loop:

$data = array(
    "success" => false,
    "results" => array()
);

foreach ($showAll as $client) {  
               $data_array[] = 
                     array(
                     'name' => $client['name'],
                     'value' => $client['name'],
                     'text' => $client['name']
                );

}

Above only outputs:

[
 {
   "name":"Choice 1",
   "value":"value 1",
   "text":"Choice 1"
 },
 {
   "name":"Choice 2",
   "value":"value2",
   "text":"Choice 2"
 }
]

So it is missing the top part of my original array - but I want to loop through each database results in "results": [ ... ]

3
  • 1
    "Above only outputs:" please show your output code Commented Apr 20, 2017 at 8:54
  • @Steve it is showed just below the text "Above only outputs:" Commented Apr 20, 2017 at 8:55
  • 1
    There is no code in this question that outputs anything. i'm guessing you call json_encode somewhere, but your question does not show that Commented Apr 20, 2017 at 9:10

4 Answers 4

12

Try this

$data = array(
  "success" => false,
  "results" => array()
);

foreach ($showAll as $client) {  
  $data['results'][] = array(
    'name' => $client['name'],
    'value' => $client['name'],
    'text' => $client['name']
  );
}

$data['success'] = true; // if you want to update `status` as well
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

2 Comments

This adds the "success" value to the end, and doesn't output the values as mentioned in my OP..
'success' is already in $data array, the order should not change.
5

After creating $data_array array just add few lines which i have in my post.

Try this code snippet here(with sample input)

ini_set('display_errors', 1);
foreach ($showAll as $client)
{
    $data_array[] = array(
                'name' => $client['name'],
                'value' => $client['name'],
                'text' => $client['name']
    );
}
// add these lines to your code.
$result=array();
$result["success"]=true;
$result["results"]=$data_array;
echo json_encode($result);

2 Comments

@arkascha Sir I have tested it, it is giving the expected output the way user want
I have to apologize, you are indeed right! I was fooled by the fact that json encloses immediate values by a single curly brace. Sorry!
1

try this as you have an array inside the $data_array on Key "Results" so you should use "results" as a key also and then try to push data in that array

foreach ($showAll as $client) {  
               $data_array["results"][] = 
                     array(
                     'name' => $client['name'],
                     'value' => $client['name'],
                     'text' => $client['name']
                );

}

1 Comment

so he can use json_encode(). this is not an issue. :)
0

You can simply use json_encode and push it to your result array

$data = array(
    "success" => false,
    "results" => array()
);

$result = [
 [
   "name" => "Choice 1",
   "value" => "value 1",
   "text" => "Choice 1"
 ],
 [
   "name" => "Choice 2",
   "value" => "value2",
   "text" => "Choice 2"
 ]
];

$data['results'] = json_encode($result);

2 Comments

it will make the $result as json but "success" will remain the same as it is. So, He want to make whole array as json. i think he should assign the $result array to $data["results"] first then json_encode($data); this might be helpful for him to solve his problem.
Ya OP can do that I missed that out I guess :p

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.