How to convert an object from PHP to an Array in Javascript?

Professor Zoom

I'm trying to convert an object that I get from PHP into an array in Javascript but I don't know how to solve this, I try some solutions I read before do this question but it doesn't work, I got something like this in PHP

$data = explode(',', $articles);

$length = count($data);

for ($i = 0; $i < $length; $i++) {

    $products = explode('-', $data[$i]);

    $product_key = $products[0];
    $quantity    = $products[1];

   $get_data_product = mysqli_query($connection, "SELECT * FROM articles WHERE id_article = '".$product_key."' AND id_user = '".$id_user."'") or die (mysqli_error($connection));
   $get_data_product = mysqli_fetch_assoc($get_data_product);
   $article_name     = $get_data_product['name'];
   $article_price    = $get_data_product['price'];


   $info[$i] = array('name' => $article_name, 'quantity' => $quantity, 'price' => $article_price);
}

echo json_encode($info);

And in Javascript

success: function(info) {

   var data = JSON.stringify(info);

   console.log(data);
}

Console log show this

[{"name":"prueba 2","quantity":"4","price":"100"}]

I tried to read them all with ths code but it is showing data, only when I use console log or alert

 $.each(data, function(i) {

   $('#content').append('<tr class="text-center">' +
                            '<td>' + data[i].quantity + '</td>' +
                            '<td>' + data[i].name + '</td>' +
                          '</tr>');

});
Jonathan

As prodigitalson notes in the comments - and is effectively the answer - you converted your data into a JSON object in PHP, but then convert it needlessly into a string, so you then can't manipulate it at all.

E.g: JSON.stringify([{test: 1}]) becomes "[{test: 1}]".

Removing that, you will then be able to loop on it in your original way, or you can loop on it using a standard javascript foreach where data is your response in your callback containing [{"name":"prueba 2","quantity":"4","price":"100"}]:

data.forEach(function (e) {
    $('#content').append(
        '<tr class="text-center">\n' +
            '<td>' + e.quantity + '</td>\n' +
            '<td>' + e.name + '</td>\n' +
        '</tr>\n'
    );
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I receive a JSON object from PHP backend and convert to JavaScript array for frontend

How to convert an object to an array using either php or javascript?

How to convert a JavaScript object to a PHP array and get the raw string?

How to convert get perticular field from PHP array to an array in Javascript?

How to convert an array to object in PHP?

How to convert this PHP Object to an array?

How to convert this object to array in php

Convert array from php to javascript

How to convert array to object in Javascript?

How to Convert Object to Array in Javascript

How to convert javascript array to object

javascript how to convert array into object

How to convert a JavaScript array to an object?

How to convert array in Object in JavaScript?

How to convert php object array from one form to another

How to convert mongodb response array from javascript object to JSON string

PHP - How to Convert Javascript Array to PHP array

How to convert nested object to array of object in javascript?

How can I convert this JSON from PHP to a Javascript array

convert php associative array into javascript object

How to convert array of objects to object of array in javascript?

How to convert an array of JSON into an array of javascript object?

How to convert JSON array to OBJECT array javascript?

Convert JavaScript array element from string to object

Convert from Formdata object to javascript array

how to convert an object into an array in a specific format in PHP

How to convert an Object to array in php with framework Codeigniter

How to convert array of arrays into object in PHP?

how can i convert object into an array in javascript?