简体   繁体   中英

build dynamic associative multi-dimensional array

I have seen a lot of questions regarding multi-dimensional arrays, and how to build them, and whilst trying some of the answers I still get an error. So I am thinking what I am trying to do is not really possible, and perhaps I should use JSON instead?

I have data from a source which is returned as a JSON object. It has lots data in it, of which i only need a small amount. Now I need it sorted into dates:

 $temp_array = array();
        foreach ($data as $booking_id => $booking) {
            $setdate = '';
            $b = $this->getBooking($booking_id);
            foreach ($b as $k => $v) {
                $setdate = date('d-m-Y', $booking['start_date']);
                if(!in_array($setdate, $temp_array)) {
                    $temp_array = array('date' => $setdate);
                }
                $temp_array['date'][] = array($k => $v);
            }
        }

I need the date to be the first node of the array, and the data to be in the second node. When a new date is found in the iteration, it creates another new node:

[0] => 1/10/16
           [0] => array( ['key'] =>['value'])
           [1] => array( ['key'] =>['value'])
           [2] => array( ['key'] =>['value'])
[1] => 7/10/16
           [0] => array( ['key'] =>['value'])
           [1] => array( ['key'] =>['value'])
           [2] => array( ['key'] =>['value'])
[2] => 14/10/16
           [0] => array( ['key'] =>['value'])
           [1] => array( ['key'] =>['value'])
           [2] => array( ['key'] =>['value'])
[3] => 21/10/16
           [0] => array( ['key'] =>['value'])
           [1] => array( ['key'] =>['value'])
           [2] => array( ['key'] =>['value'])

I seem unable to get anywhere with it as the errors I get refer to [] operator not supported for strings or Warning: Attempt to modify property of non-object in depending on what other examples on here I have found.

I think the question here, is that the data is not in any order, so it may populate node [0], and then [3], and then back to [0], and so on.

Any ideas what on earth I am doing wrong ?

Thanks

Addy

Update

Using this code, which is the middle foreach:

 $b = $this->getBooking($booking_id);
 foreach ($b as $k => $v) {
     $setdate = date('d-m-Y', strtotime($booking['date_desc']));
     if(!in_array($setdate, $temp_dates)) {
         array_push($temp_dates, $setdate);
         $temp_array[]['date'] = $setdate;
     }
 }

Gives me this result:

Array ( [0] => Array ( [date] => 22-10-2016 )
        [1] => Array ( [date] => 01-10-2016 ) 
        [2] => Array ( [date] => 08-10-2016 ) 
        [3] => Array ( [date] => 15-10-2016 )
)

Which is correct. So now, I need to add, each time a date arrives which $tem_array has a heading for, add another array:

Array ( [0] => Array ( [date] => 22-10-2016 ) => [0] => $data
                                                 [1] => $data
                                                 [2] => $data
        [1] => Array ( [date] => 01-10-2016 ) => [0] => $data
                                                 [1] => $data 
                                                 [2] => $data
        [2] => Array ( [date] => 08-10-2016 ) => [0] => $data
        [3] => Array ( [date] => 15-10-2016 ) => [0] => $data
                                                 [1] => $data
)

Hope this explains it a little better. I have the first bit correct, but when a date is already in there, I need to move up a node.

I think I was over complicating it. This is the final code which did want I wanted.

function sortcf($data) {
        $temp_array = array();
        $temp_dates = array();
        foreach ($data as $booking_id => $booking) {
            $b = $this->getBooking($booking_id);
            $setdate = date('d-m-Y', strtotime($booking['date_desc']));
            $temp_array[$setdate][] = ($b);
        }
        $this->wcg_am_data = $temp_array;
    }

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