存储深度第一次将值转换为php中的数组

时间:2016-08-12 10:08:16

标签: php depth-first-search

目前我正在开发一个项目,它使用深度优先搜索来重新计算值,但我只能回显该值,但我不知道如何将值存储到变量中。

这是我的代码

function calculate_ttl_member_agent ($conn, $id) {
$id_val = $level = "";
$search_dl_sql = "select * from table where foreign_ID = ".$id;
$search_dl_exe = mysqli_query($conn, $search_dl_sql);
while($result = mysqli_fetch_assoc($search_dl_exe)){
  $level = $result['level'];
  $id_val = $result['ID'];
  echo ",'".$level."'";
  calculate_ttl_member_agent ($conn, $id_val);
}}

我已尝试使用return,但它只提供第一级值..

2 个答案:

答案 0 :(得分:0)

values存储在array中,如Alexanderp所述,并在

之后使用
// inside while

 $level = $result['level'];
  $id_val = $result['ID'];
  $level[]=  $level;

$arr['level'][] = $result['level']; 
$arr['id_val'][] = $result['ID'];

答案 1 :(得分:0)

创建一个数组并为其添加值。

function calculate_ttl_member_agent ($conn, $id) {
  $id_val = $level = "";
  $search_dl_sql = "select * from table where foreign_ID = ".$id;
  $search_dl_exe = mysqli_query($conn, $search_dl_sql);
  // define an empty array
  $arr = [];
  while($result = mysqli_fetch_assoc($search_dl_exe)){
    $level = $result['level'];
    $id_val = $result['ID'];
    echo ",'".$level."'";
  calculate_ttl_member_agent ($conn, $id_val);
  array_push( $arr, [$level, $id_val, 'add anything else needed']);

  return $arr;

}}