- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我这里有这段代码,它给了我正在寻找的结果,一个格式良好的值树。
$todos = $this->db->get('todos'); //store the resulting records
$tree = array(); //empty array for storage
$result = $todos->result_array(); //store results as arrays
foreach ($result as $item){
$id = $item['recordId'];
$parent = $item['actionParent'];
$tree[$id] = isset($tree[$id]) ? $item + $tree[$id] : $item;
$tree[$parent]['_children'][] = &$tree[];
}
echo '<pre>';
print_r($tree);
echo '</pre>';
当我将 foreach 中的代码放入这样的函数中时,我得到一个空数组。我错过了什么?
function adj_tree($tree, $item){
$id = $item['recordId'];
$parent = $item['actionParent'];
$tree[$id] = isset($tree[$id]) ? $item + $tree[$id] : $item;
$tree[$parent]['_children'][] = &$tree[];
}
$todos = $this->db->get('todos'); //store the resulting records
$tree = array(); //empty array for storage
$result = $todos->result_array(); //store results as arrays
foreach ($result as $item){
adj_tree($tree, $item);
}
echo '<pre>';
print_r($tree);
echo '</pre>';
最佳答案
最简单的方法是将$tree
传递给函数by reference .考虑更改代码中的以下行
function adj_tree($tree, $item)
到
function adj_tree(&$tree, $item)
这是因为在您的代码中,$tree
作为原始 $tree
的副本在函数 adj_tree
中传递。当您通过引用传递时,将传递原始的,并且函数 adj_tree
对其进行的更改会在调用后反射(reflect)出来。
第二个(不是首选)替代方案是让您的函数返回修改后的树,这样您的函数将如下所示:
function adj_tree($tree, $item) {
$id = $item['recordId'];
$parent = $item['actionParent'];
$tree[$id] = isset($tree[$id]) ? $item + $tree[$id] : $item;
$tree[$parent]['_children'][] = &$tree[];
return $tree; // this is the line I have added
}
你的 foreach
循环将是这样的:
foreach ($result as $item){
$tree = adj_tree($tree, $item);
}
关于PHP 代码一直有效,直到我将它变成一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308530/
我有一个在 Android 市场上相当流行的应用程序,它允许数以万计的用户按下一个按钮并向它发出语音命令。然后我就可以做很多不同的事情,比如给他们提供当前的天气预报等等...... 无论如何,我的应用
令人惊讶的是,标题基本上解释了它。我们有一个我们的客户制作的页面,我们正在重新创建该页面。 页面高度会一直增加,直到(我假设是这样)浏览器达到它的极限。我已经尝试过 Firebug 和 W3 验证器,
我是 react-native 的新手,试图创建我自己的组件,但它一直显示一个空屏幕。 这是我的组件代码 class BoxComponent extends Component { cons
我正在为我的 PHP 元素创建一个非常简单的博客,但遇到了一个简单的问题。我无法让我的页眉图像一直 float 。我有一个横幅,左边有一些文字,我有一个 1px 的切片,在可以选择的任何分辨率的宽度上
为什么我可以在另一个 Controller 的 View 中访问一个 Controller 的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它? 最佳答案 @George Schreib
我正在使用带有最新 ADT 插件的 Eclipse Kepler SP2。每隔一分钟 Eclipse 就会说“为 Android 4.4.2 加载数据”并阻止我想做的一切。我在不同的文件夹中有几个 E
我是一名优秀的程序员,十分优秀!