按值前缀对平面数组进行分组并填充索引数组的索引数组

发布于 2024-12-27 18:57:43 字数 924 浏览 0 评论 0原文

我有一个像这样的数组:

[
    'ing_1_ing',
    'ing_1_amount',
    'ing_1_det',
    'ing_1_meas',
    'ing_2_ing',
    'ing_2_amount',
    'ing_2_det',
    'ing_2_meas',
]

我想将这些值分组到一个这样的数组中:

Array (
    [0] => Array(
        [0] => ing_1_ing
        [1] => ing_1_amount
        [2] => ing_1_det
        [3] => ing_1_meas
    )
    [1] => Array(
        [0] => ing_2_ing
        [1] => ing_2_amount
        [2] => ing_2_det
        [3] => ing_2_meas
    )
)

可能还有许多其他这样命名的项目: ing_NUMBER_type

如何按照我想要的方式将第一个数组分组它?我尝试过这个,但由于某种原因,strpos()有时会失败:

$i = 1;     
foreach ($firstArray as $t) {
    if (strpos($t, (string)$i)) {
        $secondArray[--$i][] = $t;
    } else {
        $i++;
    }
}

出了什么问题?

I have an array like this:

[
    'ing_1_ing',
    'ing_1_amount',
    'ing_1_det',
    'ing_1_meas',
    'ing_2_ing',
    'ing_2_amount',
    'ing_2_det',
    'ing_2_meas',
]

And I want to group the values into an array like this:

Array (
    [0] => Array(
        [0] => ing_1_ing
        [1] => ing_1_amount
        [2] => ing_1_det
        [3] => ing_1_meas
    )
    [1] => Array(
        [0] => ing_2_ing
        [1] => ing_2_amount
        [2] => ing_2_det
        [3] => ing_2_meas
    )
)

There may be many other items named like that: ing_NUMBER_type

How do I group the first array to the way I want it? I tried this, but for some reason, strpos() sometimes fails:

$i = 1;     
foreach ($firstArray as $t) {
    if (strpos($t, (string)$i)) {
        $secondArray[--$i][] = $t;
    } else {
        $i++;
    }
}

What is wrong?

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

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

发布评论

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

评论(4

黄昏下泛黄的笔记 2025-01-03 18:57:43

这取决于您想要实现的目标,如果您想按块分割数组,请使用 array_chunk 方法,如果您尝试基于数字创建多维数组,您可以使用 sscanf循环中的方法来解析值:

$result = array();

foreach ($firstArray as $value)
{
    $n = sscanf($value, 'ing_%d_%s', $id, $string);

    if ($n > 1)
    {
        $result[$id][] = $value;
    }
}

It depends what you are trying to achieve, if you want to split array by chunks use array_chunk method and if you are trying to create multidimensional array based on number you can use sscanf method in your loop to parse values:

$result = array();

foreach ($firstArray as $value)
{
    $n = sscanf($value, 'ing_%d_%s', $id, $string);

    if ($n > 1)
    {
        $result[$id][] = $value;
    }
}
长梦不多时 2025-01-03 18:57:43
<?php
$ary1 = array("ing_1_ing","ing_1_amount","ing_1_det","ing_1_meas","ing_2_ing","ing_2_amount","ing_2_det","ing_2_meas");
foreach($ary1 as $val)
{
    $parts = explode("_",$val);
    $ary2[$parts[1]][]=$val;
}
?>

这将创建:

Array
(
    [1] => Array
        (
            [0] => ing_1_ing
            [1] => ing_1_amount
            [2] => ing_1_det
            [3] => ing_1_meas
        )

    [2] => Array
        (
            [0] => ing_2_ing
            [1] => ing_2_amount
            [2] => ing_2_det
            [3] => ing_2_meas
        )

)
<?php
$ary1 = array("ing_1_ing","ing_1_amount","ing_1_det","ing_1_meas","ing_2_ing","ing_2_amount","ing_2_det","ing_2_meas");
foreach($ary1 as $val)
{
    $parts = explode("_",$val);
    $ary2[$parts[1]][]=$val;
}
?>

This creates:

Array
(
    [1] => Array
        (
            [0] => ing_1_ing
            [1] => ing_1_amount
            [2] => ing_1_det
            [3] => ing_1_meas
        )

    [2] => Array
        (
            [0] => ing_2_ing
            [1] => ing_2_amount
            [2] => ing_2_det
            [3] => ing_2_meas
        )

)
烟花肆意 2025-01-03 18:57:43

我要做的事情是这样的:

$result = array();
foreach ($firstArray as $value)
{
  preg_match('/^ing_(\d+)_/', $value, $matches);
  $number = $matches[1];
  if (!array_key_exists($number, $result))
    $result[$number] = array();
  $result[$number][] = $value;
}

基本上,您迭代第一个数组,查看其中有什么数字,然后将其放在最终数组中的正确位置。

编辑。如果您知道数字始终从 1 开始,则可以将 $number = $matches[1]; 替换为 $number = $matches[ 1] - 1;,这样您将得到与您发布的示例完全相同的结果。

What I'd do is something like this:

$result = array();
foreach ($firstArray as $value)
{
  preg_match('/^ing_(\d+)_/', $value, $matches);
  $number = $matches[1];
  if (!array_key_exists($number, $result))
    $result[$number] = array();
  $result[$number][] = $value;
}

Basically you iterate through your first array, see what number is there, and put it in the right location in your final array.

EDIT. If you know you'll always have the numbers start from 1, you can replace $number = $matches[1]; for $number = $matches[1] - 1;, this way you'll get exactly the same result you posted as your example.

我也只是我 2025-01-03 18:57:43

每次遇到新组时,将新组引用推送到结果数组中。这样,您可以简单地将元素推入引用中,而无需查找或跟踪每个组的索引。 演示

$result = [];
foreach ($array as $v) {
    sscanf($v, 'ing_%d', $group);
    if (!isset($ref[$group])) {
        $result[] =& $ref[$group];
    }
    $ref[$group][] = $v;
}
var_export($result);

输出:

array (
  0 => 
  array (
    0 => 'ing_1_ing',
    1 => 'ing_1_amount',
    2 => 'ing_1_det',
    3 => 'ing_1_meas',
  ),
  1 => 
  array (
    0 => 'ing_2_ing',
    1 => 'ing_2_amount',
    2 => 'ing_2_det',
    3 => 'ing_2_meas',
  ),
)

Push a new group reference into the result array each time a new group is encountered. This way you can simply push elements into the reference instead of needing to find or keep track of the index of each group. Demo

$result = [];
foreach ($array as $v) {
    sscanf($v, 'ing_%d', $group);
    if (!isset($ref[$group])) {
        $result[] =& $ref[$group];
    }
    $ref[$group][] = $v;
}
var_export($result);

Output:

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