0

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.

1
  • 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. Commented Mar 22, 2017 at 8:33

3 Answers 3

1

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

Mistalis
18.3k14 gold badges78 silver badges97 bronze badges
answered Mar 22, 2017 at 8:35
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this :
var arr = [{_1489378560544_544 : [customer_group_id : 0 , permission_id : 'permission_id'] }]

answered Mar 22, 2017 at 8:34

4 Comments

Hi Avihay. Your answer is exactly what i'm looking for. So after i got all data separately how can i organize my array like yours. Here is my 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(); });
I cant understand what you are trying to do ?
My purpose is getting data from a table and then organize it like your answer. Obove is my javascript get data function form a table.
So define a variable like my example and build him with your variables.
0

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.

answered Mar 22, 2017 at 11:00

2 Comments

Hi Ervishalporwal. Mistalis's answer is in reverse order to what I want to do. My purpose is getting data from a table and then organize it in the form like @Avihay's answer
Do you want to print it like @Aviha's answer?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.