2016-08-12 48 views
0

當前我正在使用深度優先搜索來恢復值的項目,但我只能回顯值,但我不知道如何將值存儲到變量中。存儲深度第一次將值存入數組php

這裏是我的代碼

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

我已經嘗試使用的回報,但只給第一電平值..

+0

創建一個數組並添加值到它。 – Alexanderp

回答

0

商店valuesarray由Alexanderp評論和

後使用
// inside while 

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

$arr['level'][] = $result['level']; 
$arr['id_val'][] = $result['ID']; 
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; 

}}

+0

謝謝你,我從你的評論中找出更好的方法! –