sgj773624926
之前看错问题了。。。
暂时删掉了之前的答案- sgj773624926 2016-07-24 已签到连续签到2天,获得了10个金钱
- sgj773624926 回答了问题 数组求和,有多少种方法
说两种我觉得比较优雅的方法吧,其实也可以算作一种~~
$init = array( 'a' => '0', 'b' => '0', 'c' => '0', 'd' => '0', ); // 方法一 $result = array_reduce($arr, function($prev, $next) { return array( 'a' => $prev['a'] + $next['a'], 'b' => $prev['b'] + $next['b'], 'c' => $prev['c'] + $next['c'], 'd' => $prev['d'] + $next['d'] ); }, $init); // 方法二 $keys = ['a', 'b', 'c', 'd']; $result = array_reduce($arr, function($prev, $next) use($keys) { $sumResult = []; foreach ($keys as $key) { $sumResult[$key] = $prev[$key] + $next[$key]; } return $sumResult; }, $init);
- sgj773624926 回答了问题 求元素深度
大概应该是这个样子:
function initDepth(&$data, $currentDepth) { if (!empty($data['label'])) { $data['depth'] = $currentDepth; if (!empty($data['items'])) { $currentDepth++; $length = count($data['items']) for ($i=0; $i < $length; $i++) { initDepth($data['items'][$i], $currentDepth); } } } } initDepth($data, 1);
还有一点要吐槽一下,var_dump出来的数据,不能直接使用,写起代码来很费事,还要自己转换,建议以后提问题的时候,给出直接能放到php中使用的数据,可以考虑使用var_export方法输出
- sgj773624926 2016-07-23 已签到连续签到1天,获得了5个金钱
- sgj773624926 2016-07-20 已签到连续签到2天,获得了10个金钱
- sgj773624926 2016-07-19 已签到连续签到1天,获得了5个金钱
- sgj773624926 2016-07-17 已签到连续签到2天,获得了10个金钱
- sgj773624926 回答了问题 yii2.0 总是load 失败
可以看一下源码,什么时候load方法会返回false。
public function load($data, $formName = null) { // 如果存在 yii 的 form,就使用该 form,否则就拿到所在类的名称(不含 namespace) $scope = $formName === null ? $this->formName() : $formName; if ($scope === '' && !empty($data)) { // 如果 $scope 为空字符串,且 $data不为空,就设置属性 // 即 $formName 为空字符串,且 $data不为空 $this->setAttributes($data); return true; } elseif (isset($data[$scope])) { // 否则,必须存在 $data[$scope],使用 $data[$scope] 去设置属性 $this->setAttributes($data[$scope]); return true; } else { return false; } }
当不满足
$scope === '' && !empty($data)
和isset($data[$scope])
这两个条件的时候会返回false。
这里你没有粘贴调用load时的写法,所以你自己对照一下,应该很快能找到原因 - sgj773624926 2016-07-16 已签到连续签到1天,获得了5个金钱