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?

3 Answers 3

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); 


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

3 Comments

The problem is $dataTree may have more then 2 dimansions (some parts can be nested very much)
Then we have to use recursive function to traverse in your $dataTree
I use such thing, but numeric indexes are still there after json_encode, such "0", "1", "2" ....
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"}]

1 Comment

I think it will address you requirement !
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.

Comments

Your Answer

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.