-2

How to create multi-dimensional javascript object in php?

I want to create javascript object from below php array,

$arrCat = array();
$arrCat['vehicles']['id'][0] = 2;
$arrCat['vehicles']['name'][0] = 'cars';
$arrCat['vehicles']['id'][1] = 3;
$arrCat['vehicles']['name'][1] = 'bikes';
$arrCat['property']['id'][0] = 5;
$arrCat['property']['name'][0] = 'house';
$arrCat['property']['id'][1] = 6;
$arrCat['property']['name'][1] = 'apartments';

Please help!

4
  • 6
    does json_encode($arrCat) not work? Commented Dec 9, 2014 at 6:07
  • stackoverflow.com/questions/7545641/… Commented Dec 9, 2014 at 6:08
  • yes that did work, but how can i loop through that object in javascript, var obj = {"vehicles":{"id":[2,3],"name":["cars","bikes"]},"property":{"id":[5,6],"name":["house","apartments"]}} Commented Dec 9, 2014 at 6:23
  • You'll loop through it using jQuery.each(). Commented Dec 9, 2014 at 6:30

1 Answer 1

0

for single dimension use it.

 looping through as :-   
     $arrCat = array(); 
        $dat = array();//a dummy array
        foreach($your_array as $val)
                            {
                                $dat['id'] = $val['value'];
                                $dat['name'] = $val['value'];
                                $arrCat [] = $dat;
                            }
        echo json_encode($arrCat );
        die;

for multidimensional change $arrCat['your_parent_array_name']['index'][] = 'your_value' ;
i hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.