-2

I need a JS array of PHP arrays, specifically formatted, like this:

products = [{}, {}, {}]

I have been google for 2 days and trying to use php function json_encode, but it doesn't give me format I'm looking for. I'm getting this:

Array
(
    [0] => Array
        (
            [id] => 3971
            [name] => Test
            [price] => 1107.9000
            [brand] => KM
            [variant] => 
            [quantity] => 3.27
        )

    [1] => Array
        (
            [id] => 3957
            [name] => Test
            [price] => 87.3000
            [brand] => KM
            [variant] => 
            [quantity] => 4
        )    
)

Sorry for dumb duplicated question.

alexxero
  • 93
  • 10

1 Answers1

2

You can build your javascript via php:

<script>
var foo = <?php echo json_encode(["foo"=>"bar"]); ?>;
</script>

obtaining

<script>
var foo = {"foo":"bar"};
</script>
sensorario
  • 20,262
  • 30
  • 97
  • 159