目前我正在开发一个项目,它使用深度优先搜索来重新计算值,但我只能回显该值,但我不知道如何将值存储到变量中。
这是我的代码
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,但它只提供第一级值..
答案 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;
}}