0

I want to get this as a single object because I work with a JObject on the frontend.

I got an array at the moment but I am unsure how i should modify it so it returns a single object instead.

This is the code:

$contacts = array(); 

        while ($row = mysqli_fetch_array($stmt))
        {

            $contact = array("ID" => $row['ProduktID'],
                             "Name" => $row['ProduktNamn'],
                             "Number" => $row['ProduktPris']);

            array_push($contacts, $contact);
        }

        echo json_encode($contacts, JSON_PRETTY_PRINT);

And the goal is for it to look something like this with "results" as well so I can reach the whole thing:

enter image description here

3
  • What do you mean a single object? Don't loop and don't push onto the array if you don't want multiple elements in the array. Commented Feb 29, 2016 at 20:34
  • I want to create the json as the picture I mentioned above Commented Feb 29, 2016 at 20:35
  • What are you getting now? Commented Feb 29, 2016 at 20:35

3 Answers 3

2

To wrap your array of contacts in an object with a single results property:

echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is what he actually wants, regardless of the confusing wording of his question. I was about to post it with a link to sandbox.onlinephpfunctions.com/code/…
I am sorry for being confusing! This did it though. Thanks alot guys. Amazing feedback and support. I am fairly new so this is amazing. Thx again.
Onto the next issue :) The life of a beginner programmer ;)
2

You can use typecasting to convert an array to an object:

$object = (object) $array_name;

or manually convert it into an object

$object = object;
foreach($arr as $key => $value)
{
    $object->$key = $value;
}

2 Comments

Now I dont get 1 object I think. When I do it Each row gets "0" and the next one "1" etc.
Like this: { "0": { "ID": "43", "Name": "pro2", "Number": "pris2" }, "1": { "ID": "42", "Name": "Produkt1", "Number": "Pris1" } }
0

Like this? Keep in mind an object is different then an array and your question is rather confusing.

    while ($row = mysqli_fetch_array($stmt)){
        $contact[] = [
          "ID" => $row['ProduktID'],
          "Name" => $row['ProduktNamn'],
          "Number" => $row['ProduktPris']
        ];
    }

    json_encode(['result' => $contact]); // Forget the JSON_PRETTY_PRINT. 

Using this method [], it will use the first available numeric index starting with 0. This way you don't have to push an array.

5 Comments

I do not want it as an array. I want to send the info as a object. Just like the image I showed. Because I want to work with it as a JObject in my frontend
@William.John, you're sending a JSON string, not an array. json_encode will encode it whether it's a PHP array or a PHP object. It makes no difference.
Hm okay. I was told otherwise in my previous thread (where I show my front end as well) You can take a look here if u want to see if there is something wrong in my code there: stackoverflow.com/questions/35705061/…
@William.John As Devon said, your output is JSON its the array structure you need to worry about. And seeing the comment in Sudoscience answer this code would suffice.
I think Viktor just nailed it. I will try it and see

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.