0

I would like to create a JSON object dynamically. The JSON object will have to be as the follows:

{ 
                $capa:[$fila['test_name'],...etc],
                .
                .
                .etc    

};

The key and value will be retrieved through a MySQL query.
This is what i'm doing:

$array_container= array();

while($fila=mysqli_fetch_assoc($sql)){


                    $format_org=str_replace(" ","_",$fila["organization"]);
                    $format_eval=str_replace(" ","_",$fila["EvaluationType"]);
                    $format_test=str_replace(" ","_",$fila["test_name"]);
                                        $CapaEnviar=$format_org.$format_eval;

                              $array_container[] = array($CapaEnviar => $fila['test_name']);

}

echo json_encode($array_container,true);

Using the previous code, I can retrieve a JSON object with duplicate keys.

This code is an answer for an AJAX request, so once the JSON object has been created correctly, I will send back this JSON object in order to retrieve the key and value, so I will have to retrieve separately the key and value.

6
  • You already are creating a JSON object dynamically when issuing json_encode($array_container,true), so I'm not sure what you're asking for. Please clarify, and show what you've tried so far. Commented May 6, 2016 at 17:03
  • 1
    what i'm trying is create a an dynamic array and then convert it to json object. the results of this array is as the follows: ` [{"TEST1":"valueT1"},{"TEST1":"otherValue"}] ` and what i'm looking for is to have a json like this: ` [{"TEST1":['valueT1','otherValue']}] ` as you can see, i want to avoid duplicates keys. if is possible to check if there are an existing key . Commented May 6, 2016 at 17:19
  • Ha, I see, thanks for the clarification. Commented May 6, 2016 at 17:27
  • Note: This line $array_container[] = array($CapaEnviar => $fila['test_name']); should be $array_container[] = array($CapaEnviar => $format_test);, don't you think? Commented May 6, 2016 at 17:31
  • no, fila['test_name'] is correct, because i want that user show text whitout "_" Commented May 6, 2016 at 17:52

1 Answer 1

2

From your comment,

...the results of this array is as the follows: [{"TEST1":"valueT1"},{"TEST1":"otherValue"}] and what i'm looking for is to have a json like this: [{"TEST1":['valueT1','otherValue']}] as you can see, i want to avoid duplicates keys.

Solution:

In your while loop, change this line

$array_container[] = array($CapaEnviar => $fila['test_name']);

to

$array_container[$CapaEnviar][] = $fila['test_name'];

Update:

how i can retrieve this key and their values through ajax?

Since you're expecting a json object from server, add this setting dataType:'json' to your AJAX request. dataType is the type of data you're expecting back from the server. And in the success() callback function, loop through the json result to get (key, value) pairs

Here's the reference:

So your AJAX skeleton code should be like this:

$.ajax({
    type: 'POST',
    url: 'yourpage.php',
    dataType: 'json',
    cache: 'false',

    beforeSend: function(){

    },

    success: function(data){
        $.each(data, function(key, value) {
            alert("Key:" + key + ", value: " + value);
        });
    },

    error: function(){
        // error
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, is working good!. By the way , how i can retrieve this key and their values through ajax?
i want to retrieve the name of key and their values, somebody has an idea?
Are you using an ajax library, something like jQuery?
@riztak I've updated my answer. Please see the Update section of my answer.

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.