1

I need to encode a multidimensional array with e.g. CJavaScript or CJSON, but I need to avoid PHP array keys.

Assuming the following data structure

$dataTree = array(
  '39'=>array(
      'label' => 'node1',
      'children' => array(
          '42'=>array('label' => 'child1'),
          '44'=>array('label' => 'child2'),
      ),
  ),
  '40'=>array(
      'label' => 'node2',
  )
);

I would need to get the following output (in Javascript):

var data = [
    {
        label: 'node1',
        children: [
            { label: 'child1' },
            { label: 'child2' }
        ]
    },
    {
        label: 'node2',
        children: [
            { label: 'child3' }
        ]
    }
];

Is there any way to do this?

nietonfir
  • 4,797
  • 6
  • 31
  • 43
Anton Abramov
  • 2,251
  • 6
  • 24
  • 27

3 Answers3

1

Use this code.

<?php
$dataTree = array(
  '39'=>array(
      'label' => 'node1',
      'children' => array(
          '42'=>array('label' => 'child1'),
          '44'=>array('label' => 'child2'),
      ),
  ),
  '40'=>array(
      'label' => 'node2',
     'children' => array(
          '42'=>array('label' => 'child3'),         
      ),
  )
);
$res = array();
foreach( $dataTree as $val) 
{
   $temp_ch = array();
  foreach($val["children"] as $ch)
    $temp_ch[]=$ch; 
  $val["children"] = $temp_ch;
$res[] = $val;
}
echo json_encode($res); 


?>
Kumar V
  • 8,810
  • 9
  • 39
  • 58
1

For your example :

$dataTree = array(
  '39'=>array(
      'label' => 'node1',
      'children' => array(
          '42'=>array('label' => 'child1'),
          '44'=>array('label' => 'child2'),
      ),
  ),
  '40'=>array(
      'label' => 'node2',
  )
);

Try this :

function correctToJsonArray($array){
    foreach ($array as $key => $value) {
        if(isset($value['children'])){
            $value['children'] =  correctToJsonArray($value['children']);
        }
       if(isset($value['label'])){
            $temp[]=$value;
        }
    }
    return $temp;
}

Which outputs if you output like this :

echo json_encode(correctToJsonArray($dataTree));

to,

[{"label":"node1","children":[{"label":"child1"},{"label":"child2"}]},{"label":"node2"}]
Chaulagai
  • 752
  • 8
  • 12
0

If removing numeric indexes is all what you want, this should do the trick for you:

$dataTree = array(
    '39'=>array(
        'label' => 'node1',
        'children' => array(
            '42'=>array('label' => 'child1'),
            '44'=>array('label' => 'child2'),
        ),
    ),
    '40'=>array(
        'label' => 'node2',
        'children' => array(
            '42'=>array('label' => 'child3'),
        ),
    )
);

function removeNumericKeys($arr) {
    $return = array();
    foreach ($arr as $k => $v) {
        $data = (is_array($v)) ? removeNumericKeys($v) : $v;
        if (is_numeric($k)) {
            $return[] = $data;
        } else {
            $return[$k] = $data;
        }
    }

    return $return;
}


echo json_encode(removeNumericKeys($dataTree));
/* returns
[
    {
        "label":"node1",
        "children":[
            {"label":"child1"},
            {"label":"child2"}
        ]
    },
    {
        "label":"node2",
        "children":[
            {"label":"child3"}
        ]
    }
]
*/

It's basically a recursive function that removes any numeric indexes from passed arrays.

nietonfir
  • 4,797
  • 6
  • 31
  • 43