开发者

I am so confused with associative arrays in php (two and 3 dimensional)

开发者 https://www.devze.com 2023-01-20 01:21 出处:网络
I am learning php. In spite of so many examples on google, I am still confused about implementing arrays which are two dimensional and three dimensional. Can开发者_如何学C anyone explain, in simple te

I am learning php. In spite of so many examples on google, I am still confused about implementing arrays which are two dimensional and three dimensional. Can开发者_如何学C anyone explain, in simple terms, with an example please?


The easiest example for me was thinking of a SQL table as a multidimensional array.

The table might look like this:

ID | Name | Email
--------------------------
 1 | John | john@email.com
 2 | Jane | jane@email.com

And the array might look like this:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => John
            [2] => john@email.com
        )

    [1] => Array
        (
            [0] => 2
            [1] => Jane
            [2] => jane@email.com
        )

)

The table is turned into an array of arrays. Where each row is accessed by the first index and each column by the second index.

So If I wanted to get "Jane" I would use $array[1][1]

An associative array of that same table might look like this:

Array
(
    [0] => Array
        (
            [ID] => 1
            [Name] => John
            [Email] => john@email.com
        )

    [1] => Array
        (
            [ID] => 2
            [Name] => Jane
            [Email] => jane@email.com
        )

)

You would access "Jane" like $array[1]["Name"]


These are arrays which are nested in other arrays. Depending on how deep they are nested determines what dimensional they are.

Here is an example of a 1D and 2D array, respectively.

$arr =
array(
   'item1' => 543,
   654 => true,
   'xas' => 0.54
);
// Accessing $arr[654] (returns true) 
$arr2 = array(
   array
   (
      'a' => 54,
      'b' => 'Hello'
   ),
   array
   (
       'itemx' => true,
       954 => 'hello'
   )
);
// Accessing $arr[0]['b'] (equal to 'Hello')

For a 3D array, you simply add another nested array into one of the 2nd level array items in the 2D array example.


There also is a very simple way to get started with multidimensional arrays.

Simply take a sheet and a pencil and start writing your multidimensional array down on paper first. It will help you a lot in the beginning.

It should look something like this.

ARRAY0 {
        key0.0 => "value0.0",
        key0.1 => "value0.1",
        key0.2 => ARRAY1 {
                          key1.0 => "value1.0",
                          key1.1 => ARRAY2 {
                                            key2.0 => "value2.0",
                                            key2.1 => "value2.1",
                                    },
                          key1.2 => "value1.2",
                  },
        key0.3 => "value0.3",
};

This is just my way of visualizing the arrays you can come up with your own syntax if you want.


An array can have anything in it, from an integer to a string, to a full blown object, to another array.

Any array on its own is called a 1-dimensional array. If you think of it as a row of boxes, then it's a single row. If an array has another array in it then it's a 2-dimensional array: one of its boxes will be a column, adding another dimension.

$users = array() ;

$jim = array('Jim', 'Smith') ;

$users[] = $jim ;

//or in one step
$users = array(array('Jim', 'Smith')) ;

//then to access the first user:
$jim = $users[0]; 

//and his firstname:
$jimsname = $users[0][0] ;
//or
$jimsname = $jim[0] ;

You can access elements of the array and its nested arrays by indices, but you need to remember which numeric index corresponds to which piece of information. That's where you can use associative arrays, where the numeric indices are replaced by unique descriptive strings:

$users = array() ;

$jim = array(
    'firstname' => 'Jim', 
    'lastname' => 'Smith'
) ;

$users['jim'] = $jim ;

//then to access jim:
$jim = $users['jim']; 

//and his firstname:
$jimsname = $users['jim']['firstname'] ;
//or
$jimsname = $jim['firstname'] ;

That's pretty much it. You can see more here and in the manual


try this simply.

$anArray['abc'][1]['qwe']='this is a value';
$anArray['abc']['avs']='another value';

echo $anArray['abc'][1]['qwe'];
echo $anArray['abc']['avs'];

/*

Array is a bit different in PHP. You can think of array elements as single variables ($anArray['abc'][1]['qwe'] or $anArray['abc']['avs']). And you can create them like single variables. This is an addition to conventional arrays. Conventional way is also supported.

But what happens if you write : $anArray['abc']='something else';

$anArray['abc'] is just a string variable from that point. So you cannot (or may not as I didn't test it yet and practically everything is possible) access $anArray['abc'][1]['qwe'] anymore.

So try to think the elements like variables, first ;)

*/

0

精彩评论

暂无评论...
验证码 换一张
取 消