How do I declare a two dimensional array?

前端 未结 14 1526
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 21:46

What\'s the easiest way to create a 2d array. I was hoping to be able to do something similar to this:

declare int d[0..m, 0..n]
相关标签:
14条回答
  • 2020-11-29 22:02

    Firstly, PHP doesn't have multi-dimensional arrays, it has arrays of arrays.

    Secondly, you can write a function that will do it:

    function declare($m, $n, $value = 0) {
      return array_fill(0, $m, array_fill(0, $n, $value));
    }
    
    0 讨论(0)
  • 2020-11-29 22:02

    atli's answer really helped me understand this. Here is an example of how to iterate through a two-dimensional array. This sample shows how to find values for known names of an array and also a foreach where you just go through all of the fields you find there. I hope it helps someone.

    $array = array(
        0 => array(
            'name' => 'John Doe',
            'email' => 'john@example.com'
        ),
        1 => array(
            'name' => 'Jane Doe',
            'email' => 'jane@example.com'
        ),
    );
    
    foreach ( $array  as $groupid => $fields) {
        echo "hi element ". $groupid . "\n";
        echo ". name is ". $fields['name'] . "\n";
        echo ". email is ". $fields['email'] . "\n";
        $i = 0;
        foreach ($fields as $field) {
             echo ". field $i is ".$field . "\n";
            $i++;
        }
    }
    

    Outputs:

    hi element 0
    . name is John Doe
    . email is john@example.com
    . field 0 is John Doe
    . field 1 is john@example.com
    hi element 1
    . name is Jane Doe
    . email is jane@example.com
    . field 0 is Jane Doe
    . field 1 is jane@example.com
    
    0 讨论(0)
  • 2020-11-29 22:02

    And for me the argument about whether an array should be sparse or not depends on the context.

    For example, if $a[6][9] is not populated is the equivalent to $a[6][9] being populated with for example with "" or 0.

    0 讨论(0)
  • 2020-11-29 22:03

    You can also create an associative array, or a "hash-table" like array, by specifying the index of the array.

    $array = array(
        0 => array(
            'name' => 'John Doe',
            'email' => 'john@example.com'
        ),
        1 => array(
            'name' => 'Jane Doe',
            'email' => 'jane@example.com'
        ),
    );
    

    Which is equivalent to

    $array = array();
    
    $array[0] = array();
    $array[0]['name'] = 'John Doe';
    $array[0]['email'] = 'john@example.com';
    
    $array[1] = array();
    $array[1]['name'] = 'Jane Doe';
    $array[1]['email'] = 'jane@example.com';
    
    0 讨论(0)
  • 2020-11-29 22:03

    Just declare? You don't have to. Just make sure variable exists:

    $d = array();
    

    Arrays are resized dynamically, and attempt to write anything to non-exsistant element creates it (and creates entire array if needed)

    $d[1][2] = 3;
    

    This is valid for any number of dimensions without prior declarations.

    0 讨论(0)
  • 2020-11-29 22:09

    If you want to quickly create multidimensional array for simple value using one liner I would recommend using this array library to do it like this:

    $array = Arr::setNestedElement([], '1.2.3', 'value');
    

    which will produce

    [
      1 => [
        2 => [
          3 => 'value'
        ]
      ]
    ]
    
    0 讨论(0)
提交回复
热议问题