将平面数组的管道分隔值拆分为关联数组

发布于 2024-10-24 21:07:23 字数 414 浏览 2 评论 0原文

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

}

这是行不通的,因为它取代了数据的价值。我怎样才能让它在每个循环中添加一个关联值? $file_data 是一个具有动态大小的数据数组。

//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.

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

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

发布评论

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

评论(6

嘴硬脾气大 2024-10-31 21:07:23

你可以简单地这样做

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

如果你运行的是 php 5.4+,

$data += [$category => $question];

You can simply do this

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

If your're running on php 5.4+

$data += [$category => $question];
娇纵 2024-10-31 21:07:23

我认为您想要 $data[$category] ​​= $question;

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

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

I think you want $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);
梦冥 2024-10-31 21:07:23

在 for 循环之前:

$data = array();

然后在循环中:

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

Before for loop:

$data = array();

Then in your loop:

$data[] = array($catagory => $question);
无敌元气妹 2024-10-31 21:07:23

我知道这是一个老问题,但您可以使用:

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

这会将 array 推到当前 array 的末尾。或者,如果您只是尝试将单个值添加到数组的末尾,而不是更多数组,那么您可以使用以下命令:

array_push($data,$question);

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);
秋日私语 2024-10-31 21:07:23

对于还需要添加到二维关联数组中的任何人,您还可以使用上面给出的答案,并使用这样的代码,

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

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

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

which should print $question

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

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

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

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

which should print $question

戈亓 2024-10-31 21:07:23

要将管道分隔值数组拆分为关联数组,请循环数据,在管道上分解,然后使用数组解构语法声明键变量,然后将管道后值推送到具有定义键的结果数组中。

代码:(演示

$file_data = [
    'foo|bar',
    'bar|food',
    'food|fighters',
];

$result = [];
foreach ($file_data as $value) {
    [$key, $result[$key]] = explode('|', $value);
}

var_export($result);

输出:

array (
  'foo' => 'bar',
  'bar' => 'food',
  'food' => 'fighters',
)

To split your array of pipe-delimited values into an associative array, loop over the data, explode on pipes, then use array destructuring syntax to declare the key variable, then push the post-pipe value into the result array with the defined key.

Code: (Demo)

$file_data = [
    'foo|bar',
    'bar|food',
    'food|fighters',
];

$result = [];
foreach ($file_data as $value) {
    [$key, $result[$key]] = explode('|', $value);
}

var_export($result);

Output:

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