4 ms·
Also arrays of arrays have a much more compact syntax: $a = [1 => [4, 5, 6], 2 => [7, 8, 9], 3 => [10, 11, 12]]; $a = array(1 => array(4, 5, 6), 2 => arra
by program 15y ago
Also arrays of arrays have a much more compact syntax:
$a = [1 => [4, 5, 6], 2 => [7, 8, 9], 3 => [10, 11, 12]];
$a = array(1 => array(4, 5, 6), 2 => array(7, 8, 9), 3 => array(10, 11, 12));
- nknight 15y agoUh, why wouldn't [[4, 5, 6], [7, 8, 9], [10, 11, 12]] work? I know PHP has a weird history of rebelling against its C-family-syntax heritage, but requiring the syntax in your example would seem specifically calculated just to make developers' lives harder.
- inji 15y agoI think it might? http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/tests/lang/array_shortcut_005.phpt?view=markup&pathrev=313641 http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/tests...
- paradoja 15y agoWell, arrays start with index 0, so if the indexes where 1 less it would be the same. Your example would be equivalent to [0 => [4, 5, 6], 1 => [7, 8, 9], 2 => [10, 11, 12]]
- abarrett 15y agoIn addition, you only need to specify the first key to create a one-based array. Indexing will carry on from there. [1 => [4, 5, 6], [7, 8, 9], [10, 11, 12]]
- joelhaasnoot 15y agoYour example would work, but there's a reason you might want different keys. My company's current web app is written in CakePHP, which has a love for arrays. Some of my queries look like this: $booking = $this->Booking->find('first', array('conditions' => array('Booking.booking_id' => $id), 'contain' => array('Review', 'Desk' => array('fields' => array('provider_id', 'category_id'), 'Provider' => array('city', 'name'), 'ProviderCategory' => array('fields' => array('provider_id', 'category_id'), 'DeskCategory' => 'name'))))); It's essentially not readable, but with [] it would work much better, and I still want to give keys to arrays.
- josegonzalez 15y agoAs an FYI, you might want to look into custom find methods if you haven't already: https://github.com/josegonzalez/documentation/blob/master/03-good-cake/01-models.textile https://github.com/josegonzalez/documentation/blob/master/03...