du0173
2011-10-20 01:56
浏览 51
已采纳

使用php中的md数组创建graphviz图

Hy, I'm looking for a way to create a GraphViz graph from php arrays. I have an associative multidimensional array from which I need to extract the keys and their corresponding values, and use them to create a graph. Example by 1D array:

$arr = ("dogs" => "4", "cats" => "3"); 

I need to extract the key "dogs" and use it as the label of a nod, and extract the corresponding value "4" and somehow use that to define the size of that nod. I need to do the same thing with multidimensional arrays. Just a silly example:

$md_arr = ("dogs" => array("rot" => "7", "blood_hound" => "4"), 
           "cats" => array("long_hair" => "12", "some_other_kind" => "1")
          );

I need the output to be defined something like this:

name/label of the first node: first key from the first array => "dogs" name/label of the second node: first key from the second array => "rot" , size of that node is a value of the corresponding key - in this case "7".

I also need to know how to pass the variable from my array_script.php (script that creates my MD array) to a new script that will create the graph. I tried putting the example code for creating graph into my php script, and the output was a warning message stating:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\hare.php:1) in C:\xampp\php\PEAR\Image\GraphViz.php on line 174

Can anyone please help me with this?

图片转代码服务由CSDN问答提供 功能建议

Hy,我正在寻找一种从php数组创建GraphViz图的方法。 我有一个关联的多维数组,我需要从中提取键及其对应的值,并使用它们来创建图形。 以1D数组为例:

  $ arr =(“dogs”=>“4”,“cats”=>“3”);  
   
 
 

我需要提取关键字“狗”并将其用作点头标签,并提取相应的值“4”并以某种方式使用它来定义 那个点头的大小。 我需要用多维数组做同样的事情。 只是一个愚蠢的例子:

  $ md_arr =(“dogs”=> array(“rot”=>“7”,“blood_hound”=>“4”  ),
“cats”=>数组(“long_hair”=>“12”,“some_other_kind”=>“1”)
); 
   
 
  

我需要输出定义如下:

第一个节点的名称/标签:来自第一个数组的第一个键=> “dogs” name /第二个节点的标签:第二个数组中的第一个键=> “rot”,该节点的大小是相应键的值 - 在本例中为“7”。

我还需要知道如何从我的array_script.php传递变量( 创建我的MD数组的脚本)到一个将创建图形的新脚本。 我尝试将用于创建图形的示例代码放入我的php脚本中,输出是一条警告消息,说明:

 警告:无法修改标头信息 - 已经发送的标头(输出) 开始于C:\ xampp \ htdocs \ test \ hare.php:1)在第174行的C:\ xampp \ php \ PEAR \ Image \ GraphViz.php 
   
 
 < 请问有谁能帮帮我吗? 
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanan1228 2018-02-01 08:27
    已采纳

    I figured it out on my own :) link the trick is to use addNode function. In my case, I needed to create dynamic graph using data from dynamicaly populated MD array. So you just need to name the node with a variable containing data you need. I needed the label to be set by the value from a variable as well, so here's the code: $graph->addNode($var1, array('label' => $var1)); $graph->addNode($var2, array('label' => $var2)); $graph->addEdge(array($var1 => $var2)) works if var names are same, but values are differnet

    6 years late to the party, but hey! I do apologize to the community. I have no idea what was happening 6 years ago.

    点赞 评论

相关推荐 更多相似问题