简体   繁体   中英

is there any better way to append multidimensional array in php?

I managed to do this, it works perfectly for my needs, however, I have the feeling there should be something much neater.

$shifts = Shift::all(); //I am working with laravel
$shiftObj = array();
$i =0; //I create this temp var to fill the sub object block
foreach($shifts as $shift){
    $shiftObj[$i]['campaign'] = Campaign::find($shift->campaign_id)->name;
    $shiftObj[$i]['place'] = Places::find($shift->place_id)->name;
    $shiftObj[$i]['shift_date'] = $shift->shift_date;
    $shiftObj[$i]['start_time'] = $shift->start_time;
    $shiftObj[$i]['end_time'] = $shift->end_time;
    $i++;
}
return json_encode($shiftObj);

With this code I get the following response:

[{"campaign":"camp1","place":"store1","shift_date":"2014-12-09","start_time":"2014-12-09 00:00:00","end_time":"2014-12-09 15:01:00"},{"campaign":"camp2","place":"store2","shift_date":"2014-12-02","start_time":"2014-12-02 01:00:00","end_time":"2014-12-02 02:00:00"},{"campaign":"camp3","place":"store3","shift_date":"2014-12-30","start_time":"2014-12-30 16:00:00","end_time":"2014-12-31 05:00:00"}] 

Then I did the following. (Thinking I found "THE better way to do this").

    $shifts = Shift::all();
    $shiftObj = array();

foreach($shifts as $shift){
    $shiftObj[]['campaign'] = Campaign::find($shift->campaign_id)->name;
    $shiftObj[]['place'] = Places::find($shift->place_id)->name;
    $shiftObj[]['shift_date'] = $shift->shift_date;
    $shiftObj[]['start_time'] = $shift->start_time;
    $shiftObj[]['end_time'] = $shift->end_time;

}
return json_encode($shiftObj);

And I got this result:

[{"campaign":"camp1"},{"place":"store1"},{"shift_date":"2014-12-09"},{"start_time":"2014-12-09 00:00:00"},{"end_time":"2014-12-09 15:01:00"},{"campaign":"camp2"},{"place":"store2"},{"shift_date":"2014-12-02"},{"start_time":"2014-12-02 01:00:00"},{"end_time":"2014-12-02 02:00:00"},{"campaign":"camp3"},{"place":"store3"},{"shift_date":"2014-12-30"},{"start_time":"2014-12-30 16:00:00"},{"end_time":"2014-12-31 05:00:00"}]

I hope there is already a duplicate question. Please help me to find the better way to do this, maybe a tutorial or documentation where I can learn, I would appreciate it a lot. Thanks in advance!

The snag with your second approach is every time you do $shiftObj[], you are pushing a new array at the end of $shiftObj containing only one of your properties.

You need to therefore either specify the array key, as you did in your first approach, or define the whole array you are pushing at once like so:

$shifts = Shift::all();
$shiftObj = array();

foreach($shifts as $shift){
  $shiftObj[] = array(
    'campaign' => Campaign::find($shift->campaign_id)->name,
    'place'    => Places::find($shift->place_id)->name,
    'shift_date' => $shift->shift_date,
    'start_time' => $shift->start_time,
    'end_time' => $shift->end_time
  );
}

return json_encode($shiftObj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM