Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4: How do I use shorthand array syntax for multidimensional arrays? [closed]

Tags:

php

$ar = [
    array('select' => 'ven_id, ven_name'),
    array('conditions' => array(
        ['col=?', $value],
        ['col=?', $value]
    ))
];

The above code is halfway there! How do i alter this so that the nested arrays also use shorthand?

Thanks!

like image 999
Haroldo Avatar asked Jun 06 '13 10:06

Haroldo


People also ask

What is the syntax of declaring multidimensional array?

The basic form of declaring a two-dimensional array of size x, y: Syntax: data_type array_name[x][y]; Here, data_type is the type of data to be stored.

How do you handle multidimensional arrays?

Creating Multidimensional Arrays You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

What is multidimensional array in PHP explain it with simple PHP code?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

What is short array syntax?

Short array syntax was introduced in PHP 5.4 and was added to the Drupal code standards for Drupal 8. Short array syntax is cleaner and is consistent with other programming languages, such as Python. It is also easier to see closing array statement from the closing brackets of a function.


2 Answers

Replace array() with []:

$ar = [
    ['select' => 'ven_id, ven_name'],
    ['conditions' => [
        ['col=?', $value],
        ['col=?', $value]
    ]]
];
like image 66
Michael Robinson Avatar answered Sep 27 '22 23:09

Michael Robinson


I maybe missing something here but why not using the following code (as you already using []):

<?php

$ar = [
    ['select' => 'ven_id, ven_name'],
    ['conditions' => [
        ['col=?', $value],
        ['col=?', $value]
    ]],
];

var_dump($ar);

Can be tested here

like image 29
hek2mgl Avatar answered Sep 27 '22 23:09

hek2mgl