简体   繁体   English

在PHP中向多维数组添加元素

[英]Adding element to a multidimensional array in PHP

I'm iterating over an array of courses, which all contain category IDs in the form of strings. 我正在遍历一系列课程,所有课程都包含字符串形式的类别ID。 I'm trying ro group them all by category using a matrix, but I'm having trouble doing that. 我正在尝试使用矩阵按类别对它们进行分组,但是这样做很麻烦。

Here's an example of what I want: 这是我想要的示例:

$courses_by_code = [[2/11]=>[course1],[course2]], [[3/12]=>[course3], [course4]]

So my questions are: 所以我的问题是:

How do I add an element that has a key that's already in the matrix to the array that corresponds to that key? 如何将具有已经在矩阵中的键的元素添加到与该键对应的数组中?

How do I create a new line in the matrix in case I find a new key? 如果找到新密钥,如何在矩阵中创建新行?

I am not sure I understood 100%, but from what I understood you should do something like: 我不确定我是否100%理解,但据我了解,您应该执行以下操作:

$keyThatMightExist // '2/11' for example
if(isset($courses_by_code[$keyThatMightExist])) {
    $courses_by_code[$keyThatMightExist][] = $newCourseToAdd;
} else {
    $courses_by_code[$keyThatMightExist] = array($newCourseToAdd);
}

Let's start with PHP's Arrays documentation: 让我们从PHP的Arrays文档开始:

An array in PHP is actually an ordered map. PHP中的数组实际上是有序映射。 A map is a type that associates values to keys. 映射是一种将值与键相关联的类型。

Something that will be invaluable to you when working with arrays is the print_r function. 使用数组时,对您来说无价之宝是print_r函数。 var_dump is also useful. var_dump也很有用。 You will be able to see array structure as its stored in PHP. 您将能够看到存储在PHP中的数组结构。 Here's some useful things to know with arrays. 这是一些关于数组的有用知识。

Let's answer some questions now: 现在让我们回答一些问题:

How do I add an element that has a key that's already in the matrix to the array that corresponds to that key? 如何将具有已经在矩阵中的键的元素添加到与该键对应的数组中? How do I create a new line in the matrix in case I find a new key? 如果找到新密钥,如何在矩阵中创建新行?

$courses = []; // initialize
$courses['2/11'][] = 'course1';
$courses['2/11'][] = 'course2';
$courses['3/12'][] = 'course3';
$courses['3/12'][] = 'course4';

The empty [] you see indicates that I'm adding more elements to the key 2/11 . 您看到的空[]表示我正在向键2/11添加更多元素。 If I wanted I could also name those keys, but I'm not going to do that. 如果我愿意,我也可以为这些键命名,但是我不会这样做。 Using print_r (described above) I will now print the array out in a human-readable format. 现在,使用print_r (如上所述),以人类可读的格式打印出阵列。 Note that with print_r you generally want to surround the output with <pre> tags, as such: 请注意,对于print_r您通常希望将输出用<pre>标记括起来,例如:

echo "<pre>";
print_r($courses);
echo "</pre>";

And here is my output: 这是我的输出:

Array
(
    [2/11] => Array
        (
            [0] => course1
            [1] => course2
        )

    [3/12] => Array
        (
            [0] => course3
            [1] => course4
        )

)

Here is how I can access these elements: 这是我可以访问这些元素的方式:

echo $courses['2/11'][1]; // this is course2
echo "<br>";
print_r($courses['3/12']);    // this is the entire '3/12' array

Result: 结果:

course2
Array
(
    [0] => course3
    [1] => course4
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM