我有一个数组变量$ data,如下所示:
$data = array (
'deptIDid' =>array(
'userid' =>array(
'12:00' => array(
'bookname'= 'Chemistry',
'return'= 'false'
)
'12:01' => array(),
'12:02' => array(),
'12:03' => array(),
'12:04' => array(),
'12:05' => array(),
'12:06' => array(),
'12:07' => array(
'bookname'= ''
'return'= 'true'
),
'12:08' => array(
'bookname'= 'Math',
'return'= 'false'
),
'12:09' => array(),
'12:10' => array(),
'12:11' => array(),
'12:12' => array(),
),
)
)
逻辑是用户在部门库中借了一本书。在时间数组中,有两个元素userid
和return
。我需要做的是时间数组没有值。它应该添加一个类似于早期的值,直到return
值变为true
。
输出应为:
$data = array (
'deptIDid' =>array(
'userid' =>array(
'12:00' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'
)
'12:01' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'
),
'12:02' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'),
'12:03' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'),
'12:04' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'),
'12:05' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'),
'12:06' => array(
'bookname'=> 'Chemistry',
'return'=> 'false'),
'12:07' => array(
'bookname'=> 'Chemistry'
'return'=> 'true'
),
'12:08' => array(
'bookname'=> 'Math',
'return'=> 'false'
),
'12:09' => array(
'bookname'=> 'Math',
'return'=> 'false'
),
'12:10' => array(
'bookname'=> 'Math',
'return'=> 'false'
),
'12:11' => array(
'bookname'=> 'Math',
'return'=> 'false'
),
'12:12' => array(
'bookname'=> 'Math',
'return'=> 'false'
),
),
)
)
答案 0 :(得分:0)
试试这个:
检查数组是否为空,然后按如下所述添加数组:
$val = '';
foreach($data['deptIDid']['userid'] as $key=>$d) {
if(trim($data['deptIDid']['userid'][$key]['bookname']) != '') {
$val= $data['deptIDid']['userid'][$key]['bookname'];
}
if(empty($d) || $data['deptIDid']['userid'][$key]['bookname'] == '') {
$data['deptIDid']['userid'][$key] = array('bookname'=> $val,'return'=> 'false');
}
}
print '<pre>';print_r($data);
exit;
输出:
Array
(
[deptIDid] => Array
(
[userid] => Array
(
[12:00] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:01] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:02] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:03] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:04] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:05] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:06] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:07] => Array
(
[bookname] => Chemistry
[return] => false
)
[12:08] => Array
(
[bookname] => Math
[return] => false
)
[12:09] => Array
(
[bookname] => Math
[return] => false
)
[12:10] => Array
(
[bookname] => Math
[return] => false
)
[12:11] => Array
(
[bookname] => Math
[return] => false
)
[12:12] => Array
(
[bookname] => Math
[return] => false
)
)
)
)
答案 1 :(得分:0)
调整了Dhara的代码:
$prevData = [];
foreach($data['deptIDid']['userid'] as $key => $d) {
if(empty($d)) {
$data['deptIDid']['userid'][$key] = $prevData;
} else {
$prevData = $d;
}
}
答案 2 :(得分:0)
foreach($data['deptIDid']['userid'] as $key=>$val)
{
if(count($val)>0)
{
if($val['bookname']=="")
{
$val['bookname']=$tamp['bookname'];
}
else if($val['return'] == "")
{
$val['return']=$tamp['return'];
}
$tamp = $val;
$tarray[$key] = $val;
}
else
{
$tarray[$key]=$tamp;
}
}