如何将 2 维字符串数组转换为 1 维修剪后的唯一值数组?

发布于 2024-12-04 04:06:31 字数 420 浏览 1 评论 0原文

我有一个数组,其值都是特定格式的数组,如下所示:

Array
(
    [0] => Array
        (
            [0] => '8227'
            [1] => ' 8138'
        )

    [1] => Array
        (
            [0] => '8227'
            [1] => ' 8138'
            [2] => ' 7785'
        )

)

我想要这个:

Array
(
    [0] => 8227
    [1] => 8138
    [2] => 7785
)

我该怎么做?

I have an array whose values are all arrays of a specific format that looks like this:

Array
(
    [0] => Array
        (
            [0] => '8227'
            [1] => ' 8138'
        )

    [1] => Array
        (
            [0] => '8227'
            [1] => ' 8138'
            [2] => ' 7785'
        )

)

and I would like to have this:

Array
(
    [0] => 8227
    [1] => 8138
    [2] => 7785
)

How can I do this ?

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

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

发布评论

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

评论(5

凹づ凸ル 2024-12-11 04:06:31
$result = array();
foreach ($input as $sub) { // Loop outer array
  foreach ($sub as $val) { // Loop inner arrays
    $val = trim($val);
    if (!in_array($val, $result)) { // Check for duplicates
      $result[] = $val; // Add to result array
    }
  }
}
$result = array();
foreach ($input as $sub) { // Loop outer array
  foreach ($sub as $val) { // Loop inner arrays
    $val = trim($val);
    if (!in_array($val, $result)) { // Check for duplicates
      $result[] = $val; // Add to result array
    }
  }
}
海的爱人是光 2024-12-11 04:06:31
$result = array();
foreach($array as $arr){
    $result = array_merge($result, $arr);
}

$result = array_unique($result);
$result = array();
foreach($array as $arr){
    $result = array_merge($result, $arr);
}

$result = array_unique($result);
小苏打饼 2024-12-11 04:06:31

array_merge_recursive() 可用于展平数组。然后, array_unique() 获取唯一值,使用 array_values() 来“重新索引”结果数组。

$flat = call_user_func_array('array_merge_recursive', $subject);
$uniq = array_values(array_unique($flat));

array_merge_recursive() can be used to flatten the array. Then, array_unique() to get the unique values, with array_values() to "reindex" the resultant array.

$flat = call_user_func_array('array_merge_recursive', $subject);
$uniq = array_values(array_unique($flat));
只为守护你 2024-12-11 04:06:31
<?php
$array = array(
0 => array(
    0 => 8227,
    1 => 8138
),
1 => array(
    0 => 8227,
    1 => 8138,
    2 => 7785
)
);

$newArray = array();
array_walk_recursive($array, function($item, $key) use(&$newArray) {
if(!in_array($item, $newArray)) {
    $newArray[] = $item;
}
});

print_r($newArray);
?>    
<?php
$array = array(
0 => array(
    0 => 8227,
    1 => 8138
),
1 => array(
    0 => 8227,
    1 => 8138,
    2 => 7785
)
);

$newArray = array();
array_walk_recursive($array, function($item, $key) use(&$newArray) {
if(!in_array($item, $newArray)) {
    $newArray[] = $item;
}
});

print_r($newArray);
?>    
苏大泽ㄣ 2024-12-11 04:06:31

我不喜欢迭代调用 in_array() 的想法,因为它可能会对大数组造成一些阻力。

现在,我遵循的方法可能不会设置任何速度记录(我没有费心进行基准测试),但我想我会发布一些非正统的方法,以防它们激发未来的读者。

方法#1:

  • 将多维数组转换为 json 字符串
  • 将字符串拆分为所有非数字子字符串(这也会修剪值)
  • 使用 array_flip() 消除重复项 使用 array_flip()
  • 重新索引结果数组array_keys()(输出值为整数类型)

方法#2:

  • 将多维数组转换为 json 字符串,
  • 提取单词(在本例中包括数字和没有任何 项
  • 使用 array_flip() 消除重复
  • ,使用 array_keys() 重新索引结果数组(输出值是整数类型)

代码:(Demo)

$array = [['8227', '8138'], [' 8227', ' 8138', ' 7785']];

echo "regex method: ";

var_export(
    array_keys(
        array_flip(
            preg_split(
                '/\D+/',
                json_encode($array),
                0,
                PREG_SPLIT_NO_EMPTY
            )
        )
    )
);

echo "\n\nnon-regex method: ";

var_export(
    array_keys(
        array_flip(
            str_word_count(
                json_encode($array),
                1,
                '0..9'
            )
        )
    )
);

输出:

regex method: array (
  0 => 8227,
  1 => 8138,
  2 => 7785,
)

non-regex method: array (
  0 => 8227,
  1 => 8138,
  2 => 7785,
)

如果这些方法中的任何一个执行良好,那将是因为它们不进行迭代函数调用。

ps 好吧,因为我很好奇,我只是对我的两种方法和 DaveRandom 的方法做了一个非常小的/非官方的速度测试(仅使用 OP 的数据进行了 1000 次迭代 - 我不想滥用 3v4l.org)和...

  • 方法#1 的运行速度与方法 #2 一样快
  • 方法 #1 和方法 #2 的运行速度都比 DaveRandom 的方法快。

再次,我将声明,为微优化而制造的测试可能毫无意义,如果它确实很重要,则应该对您的真实数据进行真正的测试。如果您只是喜欢本页上另一个答案的逻辑流程,我完全尊重这一点。

I don't like the idea of iterated calls of in_array() since it can cause a bit of drag on big arrays.

Now, my methods to follow are probably not going to set any speed records (I didn't bother to benchmark), but I thought I would post a couple of unorthodox approaches in case they may inspire future readers.

Method #1:

  • convert the multi-dimensional array to a json string
  • split the string on all non-digital substrings (this also trims the values)
  • eliminate duplicates using array_flip()
  • re-index the resultant array using array_keys() (output values are integer-type)

Method #2:

  • convert the multi-dimensional array to a json string
  • extract the words (which in this case include numbers and there aren't any letters to worry about)
  • eliminate duplicates using array_flip()
  • reindex the resultant array using array_keys() (output values are integer-type)

Code: (Demo)

$array = [['8227', '8138'], [' 8227', ' 8138', ' 7785']];

echo "regex method: ";

var_export(
    array_keys(
        array_flip(
            preg_split(
                '/\D+/',
                json_encode($array),
                0,
                PREG_SPLIT_NO_EMPTY
            )
        )
    )
);

echo "\n\nnon-regex method: ";

var_export(
    array_keys(
        array_flip(
            str_word_count(
                json_encode($array),
                1,
                '0..9'
            )
        )
    )
);

Output:

regex method: array (
  0 => 8227,
  1 => 8138,
  2 => 7785,
)

non-regex method: array (
  0 => 8227,
  1 => 8138,
  2 => 7785,
)

If either of these methods perform well, it will be because they don't make iterated function calls.

p.s. Okay, because I was curious, I just did a very small / unofficial speed test on my two methods and DaveRandom's method (only 1000 iterations using the OP's data - I didn't want to abuse 3v4l.org) and...

  • Method #1 ran about as fast as Method #2
  • Both Method #1 and Method #2 ran faster than DaveRandom's method.

Again, I'll state that fabricated tests for micro-optimization may be pointless and real tests should be done on your REAL data IF it is actually important. If you merely prefer the logical flow of another answer on this page, I totally respect that.

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