In the sever side i have an array with structure like that:
array ('_1489378560544_544' => array (
'customer_group_id' => '0',
'permission_id' => 'disable_products_price',),
'_1489740032764_764' => array (
'customer_group_id' => '',
'permission_id' => '',),)
So now in the client side i want to create an javascript array with the same structure to server side. Is there any possible way to do that?
So after i got all data separately how can i organize my array look like this
var arr = [{_1489378560544_544 : [customer_group_id : 0 , permission_id : 'permission_id'] }]
Here is my javascript get data function:
$('#category_permission > tbody > tr').each(function() {
var id = $(this).attr("id");
var customer_group_id = $(this).children('td:first').children('select:first').val();
var permission_id = $(this).children('td:nth-child(2)').children('select:first').val();
});
Thanks for your help.
-
For sure. Its quite simple. But we're not here to code for you. stackoverflow.com/help/how-to-ask .. show us what you've tried so far and we'll help you find your mistakes.Twinfriends– Twinfriends2017年03月22日 08:33:29 +00:00Commented Mar 22, 2017 at 8:33
3 Answers 3
You can use json_encode to convert your PHP array to a JSON string and use JSON.parse() to obtain the equivalent Javascript object. Take a look here: https://www.w3schools.com/js/js_json.asp
Comments
Something like this :
var arr = [{_1489378560544_544 : [customer_group_id : 0 , permission_id : 'permission_id'] }]
4 Comments
in your php view file
jsStr = '<?php echo json_encode($array)?>'
jsObj = JSON.parse(jsStr);
console.log(jsObj.keyname);
like that all keys can be accessed to get the value. javascript don't support alphanumerical keys for array. Answer from @Mistalis should help.