8

How to create an array in PHP that with json_encode() becomes a thing with following structure:

Array(
[1] => Array(
    [id] => 1
    [data] => 45
)
[2] => Array(
    [id] => 3
    [data] => 54
)
);
2
  • json_encode(what you have above)? json_encode is just a translator. You build an array, json_encode will convert it to text for you. there's nothing magical about it. Commented Oct 18, 2013 at 16:14
  • I know, you make an example of array in php that with json_encode($your_array_example); generates a result similar like above? Commented Oct 18, 2013 at 16:19

3 Answers 3

16

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Sign up to request clarification or add additional context in comments.

1 Comment

i have multiply array elements getting from database then how it implements? means above example is hardcoded i need this line $firstArray = array("id" => 1, "data" => 45); to be more generic
7

Here is a shorter way:

$myArray = array();

$myArray[] = array("id" => 1, "data" => 45);
$myArray[] = array("id" => 3, "data" => 54);

//convert to json
$json = json_encode($myArray);

Comments

3

This example PHP array is mixed, with the outer level numerically indexed and the second level associative:

<?php
// PHP array
$books = array(
    array(
        "title" => "Professional JavaScript",
        "author" => "Nicholas C. Zakas"
    ),
    array(
        "title" => "JavaScript: The Definitive Guide",
        "author" => "David Flanagan"
    ),
    array(
        "title" => "High Performance JavaScript",
        "author" => "Nicholas C. Zakas"
    )
);
?>

In the json_encode output, the outer level is an array literal while the second level forms object literals. This example demonstrates using the JSON_PRETTY_PRINT option with json_encode for more readable output as shown in code comments below:

<script type="text/javascript">
// pass PHP array to JavaScript 
var books = <?php echo json_encode($books, JSON_PRETTY_PRINT) ?>;

// output using JSON_PRETTY_PRINT
/* var books = [ // outer level array literal
    { // second level object literals
        "title": "Professional JavaScript",
        "author": "Nicholas C. Zakas"
    },
    {
        "title": "JavaScript: The Definitive Guide",
        "author": "David Flanagan"
    },
    {
        "title": "High Performance JavaScript",
        "author": "Nicholas C. Zakas"
    }
]; */

// how to access 
console.log( books[1].author ); // David Flanagan
</script>

Comments

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.