I'm iterating over an array of courses, which all contain category IDs in the form of strings. I'm trying ro group them all by category using a matrix, but I'm having trouble doing that.
我正在迭代一系列课程,这些课程都包含字符串形式的类别ID。我正在尝试使用矩阵按类别对它们进行分组,但是我很难做到这一点。
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?
如果找到新密钥,如何在矩阵中创建新行?
2 个解决方案
#1
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);
}
#2
Let's start with PHP's Arrays documentation:
让我们从PHP的Arrays文档开始:
An array in PHP is actually an ordered map. A map is a type that associates values to keys.
PHP中的数组实际上是一个有序映射。映射是将值与键关联的类型。
Something that will be invaluable to you when working with arrays is the print_r
function. var_dump
is also useful. You will be able to see array structure as its stored in PHP. Here's some useful things to know with arrays.
使用数组时对你来说非常宝贵的东西就是print_r函数。 var_dump也很有用。您将能够看到存储在PHP中的数组结构。这里有一些有关数组的有用信息。
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
. 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. Note that with print_r
you generally want to surround the output with <pre>
tags, as such:
您看到的空[]表示我在键2/11中添加了更多元素。如果我想要我也可以命名那些键,但我不打算这样做。使用print_r(如上所述)我现在将以人类可读的格式打印出阵列。请注意,对于print_r,您通常希望用
标记包围输出,如下所示:
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
)
#1
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);
}
#2
Let's start with PHP's Arrays documentation:
让我们从PHP的Arrays文档开始:
An array in PHP is actually an ordered map. A map is a type that associates values to keys.
PHP中的数组实际上是一个有序映射。映射是将值与键关联的类型。
Something that will be invaluable to you when working with arrays is the print_r
function. var_dump
is also useful. You will be able to see array structure as its stored in PHP. Here's some useful things to know with arrays.
使用数组时对你来说非常宝贵的东西就是print_r函数。 var_dump也很有用。您将能够看到存储在PHP中的数组结构。这里有一些有关数组的有用信息。
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
. 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. Note that with print_r
you generally want to surround the output with <pre>
tags, as such:
您看到的空[]表示我在键2/11中添加了更多元素。如果我想要我也可以命名那些键,但我不打算这样做。使用print_r(如上所述)我现在将以人类可读的格式打印出阵列。请注意,对于print_r,您通常希望用
标记包围输出,如下所示:
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
)