我有这样的代码:
$pr = $tst_db->mlm_get_parent(1); // give me the value = 4 (parent_id of id 1)
$pr1 = $tst_db->mlm_get_parent($pr); // give me the value = 7 (parent_id of id 4)
$pr2 = $tst_db->mlm_get_parent($pr1); // give me the value = 5 (parent_id of id 7)
$pr3 = $tst_db->mlm_get_parent($pr2); // give me the value = 2 (parent_id of id 5)
$pr4 = $tst_db->mlm_get_parent($pr3); // give me the value = 0 (becouse id 2 not have parent id)
echo $pr; echo $pr1; echo $pr2; echo $pr3; echo $pr4;它有可能变成一个前程循环。
自动创建($pr(num) = $tst_db->mlm_get_parent($pr(num);)
当某个$pr等于0时停止它吗?
我也需要回显在这个自动循环中创建的所有parent_it。
这就是我驱逐的结果:
id 1的所有parent_id为: 4,7,5,2
提前感谢
发布于 2021-03-17 22:47:56
这将在屏幕上打印出id 1的所有父级:
$id = 1;
while($id != 0) {
$id = $tst_db->mlm_get_parent($id);
echo $id;
}
// this will print: 47520如果您希望将值存储在一个数组中,该数组的键具有问题中所示的确切格式:
$arr = array();
$id = 1;
$pr = 'pr';
$count = 0;
while($id != 0) {
$id = $tst_db->mlm_get_parent($id);
$arr[$pr] = $id;
$ccount++;
$pr = 'pr'.$count;
}
print_r($arr); // prints: Array([pr] => 4 [pr1] => 7 [pr2] => 5 [pr3] => 2 [pr4] => 0)
echo join(",", $arr); // prints 4,7,5,2,0要根据数组键生成独立变量,可以使用萃取物()方法:
extract($arr);
echo $pr; // prints 4
echo $pr3; // prints 2注:
extract()方法将覆盖任何现有变量,其名称与数组的键值相等。extract(),比如用户输入(例如$_GET、$_FILES)。您还可以创建一个foreach循环,迭代已填充的$arr数组:
foreach($arr as $key => $val) {
// Do something here.
// Although this foreach is not necessary since you could
// have already done what you want to do within the above
// while loop
}https://stackoverflow.com/questions/66682284
复制相似问题