PHP 将文本分解为多维数组

发布于 2024-12-07 00:02:21 字数 1437 浏览 0 评论 0原文

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

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

发布评论

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

评论(4

初相遇 2024-12-14 00:02:21

多么好的格式啊!我想我仍然不明白,但无论如何我都会尝试回答...

  1. 的时间表数组
  2. 使用 explode 和“\n”,您将获得该数组中每个元素 ,将其替换为带有 'Q' 的 explode 本身,您将获得一个二维数组,
  3. 该数组的每个元素的每个元素都将包含在 explode 中,将该元素替换为 explode > 本身带有 ' '

尝试做如果您遇到问题,请使用您想出的代码编辑您的问题。

What a nice format! I think I still don't get it, but I'll try answering anyway...

  1. use explode with "\n" and you'll get an array of timetables
  2. for each element of this array, replace it with an explode of itself with 'Q' and you'll have a 2-dimensional array
  3. for each element of each element of this array, replace the element with an explode of itself with ' '

Try to do this and if you're having trouble, edit your question with the code you'd come up with.

不回头走下去 2024-12-14 00:02:21

没有真正理解你的琴弦是如何工作的;这段代码应该可以完成这项工作。

$timetables = explode("\n", $source);

foreach($timetables as $tablekey => $days)
{
    $timetables[$tablekey] = explode('Q', $days);

    foreach($timetables[$tablekey] as $daykey => $hours)
        $timetables[$tablekey][$daykey] = explode(' ', $hours)
}

print_r($timetables, true);

Without really understanding how your strings work; This code should do the job.

$timetables = explode("\n", $source);

foreach($timetables as $tablekey => $days)
{
    $timetables[$tablekey] = explode('Q', $days);

    foreach($timetables[$tablekey] as $daykey => $hours)
        $timetables[$tablekey][$daykey] = explode(' ', $hours)
}

print_r($timetables, true);
情话墙 2024-12-14 00:02:21
$x = //...
$x = explode("\n", $x);
foreach($x as $k => $v) {
  $x[$k] = explode("Q", $x[$k]);
  foreach($x[$k] as $kk => $vv) {
    $x[$k][$kk] = explode(" ", $x[$k][$kk]);
  }
}

使用 array_map 我想你会得到更好的代码。

$x = //...
$x = explode("\n", $x);
foreach($x as $k => $v) {
  $x[$k] = explode("Q", $x[$k]);
  foreach($x[$k] as $kk => $vv) {
    $x[$k][$kk] = explode(" ", $x[$k][$kk]);
  }
}

With array_map I think you wiil get somewhat nicer code.

烟雨扶苏 2024-12-14 00:02:21

这是一个采用分隔符数组的递归函数:

function multi_explode($delimiters, $val) {
    $delimiter = array_shift($delimiters);
    if (!empty($delimiter)) {
        $val = explode($delimiter, $val);
        foreach($val as $key => $valval) {
            $val[$key] = multi_explode($delimiters, $valval);
        }
    }
    return $val;
}

Here is a recursive function that takes an array of delimiters:

function multi_explode($delimiters, $val) {
    $delimiter = array_shift($delimiters);
    if (!empty($delimiter)) {
        $val = explode($delimiter, $val);
        foreach($val as $key => $valval) {
            $val[$key] = multi_explode($delimiters, $valval);
        }
    }
    return $val;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文