0

如何在“邻接”键值对中推送数组,该键值对应该有一个包含“数组”的封装数组(即。array((“nodeTo”=>“$to”),(“nodeTo”=>“$to "))) 而不覆盖它们并将它们附加到类似于“+=”。对关键“邻接”的推动似乎也没有增加价值。

    $node[] = array(
        "adjacencies" => array(), //inside this array should go all the arrays seprated by commas.
        "data" => array(
                "color" => $color1,
                "type" => $type1 
            );

 // this push doesnt seem to detect the adjacencies value and doesnt really push the array inside of the container array. I also tried $node["adjacencies"][]=array("nodeTo" => "$to"); but it didnt work
    $node["adjacencies"]=array("nodeTo" => "$to");



}
4

2 回答 2

0

顺便说一句,您$node在第二个语句中使用的方式,我认为您的意思是:

$node = array(

不是:

$node[] = array(
//   ^^

然后,您可以通过执行以下操作来推送数组:

$node['adjacencies'][] = array('nodeTo' => $to);
于 2013-03-31T06:11:05.597 回答
0

如果您想要多个数组'adjacencies',请将它们附加到数组的末尾:

$node[0]['adjacencies'][] = array("nodeTo" => "$to");

当然,您需要知道要$node使用哪个索引(如果有多个节点)。

编辑:

阅读评论后,看起来 OP 所需的数组结构是这样的:

$node = array(
  'adjacencies' => array(),
  'data' => array(
    'color' => $color1,
    'type' => $type1,
  );
);

因此,要将其他节点附加到adjacencies数组中,您可以这样做:

$node['adjacencies'][] = array('nodeTo' => "$to");
于 2013-03-31T06:12:09.483 回答