Berry Strawberry Berry Strawberry - 5 months ago 13
PHP Question

Add new array elements in PHP

I have an array variable $data as below:

$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(),
),
)
)


The logic is the user borrowed a book in the department library. Inside the time array there is two elements
userid
and
return
. What I need to do is when the time array does not have a value. It should add a value similar from the early time until the
return
value become
true
.
The output should be:

$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'
),
),
)
)

Answer

Try this:

Check if array empty then add array as described below:

$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;

Output:

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
                        )

                )

        )

)