我对 php 中的关联数组(二维和三维)很困惑

发布于 2024-09-27 11:24:54 字数 58 浏览 2 评论 0 原文

我正在学习php。尽管谷歌上有很多例子,我仍然对实现二维和三维数组感到困惑。谁能用简单的例子解释一下?

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 anyone explain, in simple terms, with an example please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

來不及說愛妳 2024-10-04 11:24:54

对我来说最简单的例子是将 SQL 表视为多维数组。

该表可能如下所示:

ID | Name | Email
--------------------------
 1 | John | [email protected]
 2 | Jane | [email protected]

数组可能如下所示:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => John
            [2] => [email protected]
        )

    [1] => Array
        (
            [0] => 2
            [1] => Jane
            [2] => [email protected]
        )

)

该表被转换为数组的数组。其中每行由第一个索引访问,每列由第二个索引访问。

因此,如果我想获取“Jane”,我会使用 $array[1][1]

同一个表的关联数组可能如下所示:

Array
(
    [0] => Array
        (
            [ID] => 1
            [Name] => John
            [Email] => [email protected]
        )

    [1] => Array
        (
            [ID] => 2
            [Name] => Jane
            [Email] => [email protected]
        )

)

您将像 $array[1]["Name"] 一样访问“Jane”

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 | [email protected]
 2 | Jane | [email protected]

And the array might look like this:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => John
            [2] => [email protected]
        )

    [1] => Array
        (
            [0] => 2
            [1] => Jane
            [2] => [email protected]
        )

)

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] => [email protected]
        )

    [1] => Array
        (
            [ID] => 2
            [Name] => Jane
            [Email] => [email protected]
        )

)

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

留一抹残留的笑 2024-10-04 11:24:54

这些是嵌套在其他数组中的数组。根据它们嵌套的深度决定它们的维度。

下面分别是一维和二维数组的示例。

$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')

对于 3D 数组,您只需将另一个嵌套数组添加到 2D 数组示例中的第二级数组项之一中即可。

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.

虚拟世界 2024-10-04 11:24:54

还有一种非常简单的方法可以开始使用多维数组。

只需拿一张纸和一支铅笔,然后开始在纸上写下多维数组。一开始它会对你有很大帮助。

它应该看起来像这样。

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",
};

这只是我可视化数组的方式,如果您愿意,您可以想出自己的语法。

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.

时光瘦了 2024-10-04 11:24:54

数组中可以包含任何内容,从整数到字符串,到完整的对象,再到另一个数组。

任何数组本身都称为一维数组。如果您将其视为一排盒子,那么它就是单排。如果一个数组中有另一个数组,那么它就是一个二维数组:它的一个框将是一列,添加另一个维度。

$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] ;

您可以通过索引访问数组及其嵌套数组的元素,但您需要记住哪个数字索引对应于哪条信息。这就是您可以使用关联数组的地方,其中数字索引被唯一的描述性字符串替换:

$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'] ;

差不多就是这样。您可以在此处手册

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

定格我的天空 2024-10-04 11:24:54

简单地试试这个。

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

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

/*

数组在 PHP 中有点不同。您可以将数组元素视为单个变量($anArray['abc'][1]['qwe'] 或 $anArray['abc']['avs'])。您可以像创建单个变量一样创建它们。这是对传统阵列的补充。也支持常规方式。

会发生什么

但是如果你写: $anArray['abc']='something else'; $anArray['abc'] 从那时起只是一个字符串变量, 。所以你不能(或者可能不能,因为我还没有测试它,实际上一切皆有可能)再访问 $anArray['abc'][1]['qwe']

因此,首先尝试将元素视为变量;)

*/

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 ;)

*/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文