向关联数组添加项

时间:2022-06-12 04:00:41
//go through each question
foreach($file_data as $value) {
   //separate the string by pipes and place in variables
   list($category, $question) = explode('|', $value);

   //place in assoc array
   $data = array($category => $question);
   print_r($data);

}

This is not working as it replaces the value of data. How can I have it add an associative value each loop though? $file_data is an array of data that has a dynamic size.

这并不起作用,因为它替换了数据的值。如何让它在每个循环中添加一个关联值?$file_data是一个具有动态大小的数据数组。

5 个解决方案

#1


73  

I think you want $data[$category] = $question;

我想你想要$data[$category] = $question;

Or in case you want an array that maps categories to array of questions:

或者如果你想要一个数组类映射到数组的问题:

$data = array();
foreach($file_data as $value) {
    list($category, $question) = explode('|', $value, 2);

    if(!isset($data[$category])) {
        $data[$category] = array();
    }
    $data[$category][] = $question;
}
print_r($data);

#2


57  

You can simply do this

你可以这样做

$data += array($category => $question);

If your're running on php 5.4+

如果您正在运行php 5.4+

$data += [$category => $question];  //here was incorrect bracket

#3


23  

before for loop :

在for循环:

$data = array();

then in your loop:

然后在你的循环:

$data[] = array($catagory => $question);

#4


2  

For anyone that also need to add into 2d associative array, you can also use answer given above, and use the code like this

对于任何需要添加到2d关联数组的人,您也可以使用上面给出的答案,并使用如下代码

 $data[$category]["test"] = $question

you can then call it (to test out the result by:

然后可以调用它(通过以下方法测试结果:

echo $data[$category]["test"];

which should print $question

应该打印美元问题吗

#5


0  

I know this is an old question but you can use:

我知道这是个老问题,但你可以用:

array_push($data, array($category => $question);

This will push the array onto the end of your current array. Or if you are just trying to add single values to the end of your array, not more arrays then you can use this:

这会将数组推到当前数组的末尾。或者,如果你只是想在数组的末尾添加一个值,而不是更多的数组,那么你可以这样使用:

array_push($data,$question);

#1


73  

I think you want $data[$category] = $question;

我想你想要$data[$category] = $question;

Or in case you want an array that maps categories to array of questions:

或者如果你想要一个数组类映射到数组的问题:

$data = array();
foreach($file_data as $value) {
    list($category, $question) = explode('|', $value, 2);

    if(!isset($data[$category])) {
        $data[$category] = array();
    }
    $data[$category][] = $question;
}
print_r($data);

#2


57  

You can simply do this

你可以这样做

$data += array($category => $question);

If your're running on php 5.4+

如果您正在运行php 5.4+

$data += [$category => $question];  //here was incorrect bracket

#3


23  

before for loop :

在for循环:

$data = array();

then in your loop:

然后在你的循环:

$data[] = array($catagory => $question);

#4


2  

For anyone that also need to add into 2d associative array, you can also use answer given above, and use the code like this

对于任何需要添加到2d关联数组的人,您也可以使用上面给出的答案,并使用如下代码

 $data[$category]["test"] = $question

you can then call it (to test out the result by:

然后可以调用它(通过以下方法测试结果:

echo $data[$category]["test"];

which should print $question

应该打印美元问题吗

#5


0  

I know this is an old question but you can use:

我知道这是个老问题,但你可以用:

array_push($data, array($category => $question);

This will push the array onto the end of your current array. Or if you are just trying to add single values to the end of your array, not more arrays then you can use this:

这会将数组推到当前数组的末尾。或者,如果你只是想在数组的末尾添加一个值,而不是更多的数组,那么你可以这样使用:

array_push($data,$question);