将单元素数组的数组转换为一维数组

发布于 2024-12-25 08:56:17 字数 217 浏览 2 评论 0原文

我有这种包含单元素数组的数组:

$array = [[88868], [88867], [88869], [88870]];

我需要将其转换为一维数组。

期望的输出:

[88868, 88867, 88869, 88870]

是否有任何内置/本机 PHP 功能可用于此数组转换?

I have this kind of an array containing single-element arrays:

$array = [[88868], [88867], [88869], [88870]];

I need to convert this to one dimensional array.

Desired output:

[88868, 88867, 88869, 88870]

Is there any built-in/native PHP functionality for this array conversion?

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

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

发布评论

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

评论(8

青衫儰鉨ミ守葔 2025-01-01 08:56:17

对于您有限的用例,这可以做到:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

当子数组有很多条目时,这可以更通用:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);

For your limited use case, this'll do it:

$oneDimensionalArray = array_map('current', $twoDimensionalArray);

This can be more generalized for when the subarrays have many entries to this:

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
久夏青 2025-01-01 08:56:17

PHP array_merge文档函数可以展平你的数组:

$flat = call_user_func_array('array_merge', $array);

如果原始数组的深度高于 2 层,PHP 中的 SPL 有一个 RecursiveArrayIterator 您可以使用它来展平它:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

另请参阅: 将多维数组变成一维数组

The PHP array_merge­Docs function can flatten your array:

$flat = call_user_func_array('array_merge', $array);

In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:

$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);

See as well: Turning multidimensional array into one-dimensional array

如梦亦如幻 2025-01-01 08:56:17

尝试:

$new_array = array();
foreach($big_array as $array)
{
    foreach($array as $val)
    {
        array_push($new_array, $val);
    }    
}

print_r($new_array);

try:

$new_array = array();
foreach($big_array as $array)
{
    foreach($array as $val)
    {
        array_push($new_array, $val);
    }    
}

print_r($new_array);
土豪我们做朋友吧 2025-01-01 08:56:17
$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}
$oneDim = array();
foreach($twoDim as $i) {
  $oneDim[] = $i[0];
}
鸠书 2025-01-01 08:56:17

是的。

$values = array(array(88868), array(88867), array(88869), array(88870));
foreach ($values as &$value) $value = $value[0];

http://codepad.org/f9KjbCCb

Yup.

$values = array(array(88868), array(88867), array(88869), array(88870));
foreach ($values as &$value) $value = $value[0];

http://codepad.org/f9KjbCCb

魔法少女 2025-01-01 08:56:17

对于二维数组,这也适用:

array_merge(...$twoDimensionalArray)

For a two dimensional array this works as well:

array_merge(...$twoDimensionalArray)

清晰传感 2025-01-01 08:56:17
foreach($array as $key => $value){
   //check that $value is not empty and an array
   if (!empty($value) && is_array($value)) {
      foreach ($value as $k => $v) {
         //pushing data to new array
         $newArray[] = $v;
      }
   }
}
foreach($array as $key => $value){
   //check that $value is not empty and an array
   if (!empty($value) && is_array($value)) {
      foreach ($value as $k => $v) {
         //pushing data to new array
         $newArray[] = $v;
      }
   }
}
压抑⊿情绪 2025-01-01 08:56:17

虽然之前用于关闭此页面的页面上的一些答案 确实有适合这个问题的答案(例如 array_merge(...$array) )。由于输入数据结构的原因,针对这个特定问题的一些技术不属于其他页面。

这里的示例数据结构是单元素索引数组的数组。

var_export(array_column($array, 0));

这就是这个问题所需要的一切。


如果您曾经有过一次愚蠢的工作面试,要求您在不调用任何函数的情况下进行面试,您可以使用语言构造 (foreach()) 并使用“数组解构”语法将值推入结果中变量甚至无需为循环编写主体。 (演示

$result = [];
foreach ($array as [$result[]]);
var_export($result);

Laravel 还有一个扁平化辅助方法:Arr::flatten()

While some of the answers on the page that was previously used to close this page did have answers that suited this question (like array_merge(...$array)). There are techniques for this specific question that do not belong on the other page because of the input data structure.

The sample data structure here is an array of single-element, indexed arrays.

var_export(array_column($array, 0));

Is all that this question requires.


If you ever have a daft job interview that asks you to do it without any function calls, you can use a language construct (foreach()) and use "array destructuring" syntax to push values into a result variable without even writing a body for the loop. (Demo)

$result = [];
foreach ($array as [$result[]]);
var_export($result);

Laravel also has a flattening helper method: Arr::flatten()

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