-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!

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Arbflow
  • 1
  • 3

1 Answers1

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.

A.B
  • 20,110
  • 3
  • 37
  • 71
Ranjeet Singh
  • 924
  • 5
  • 11