php中的二维多维数组到一维数组

发布于 2024-11-27 07:17:56 字数 73 浏览 1 评论 0原文

只是想知道是否有人在 php 中将 2 维数组转换为 1 维数组。我还没有在 php.ini 中找到明确的解释。任何建议将不胜感激。

Just wondering if anyone has transformed a 2 dim array to a one dim array in php. I've yet to come across a clear explanation in php. Any suggestion would be appreciated.

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

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

发布评论

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

评论(7

油饼 2024-12-04 07:17:56

如果您在此处从 Query 获取值,您可以使用 PHP 5.5+ 中支持的数组函数,这可能会对您有所帮助,

$myfield_arr = array_column($query_result, 'myfield_name');

告别循环!享受智能代码。

This might be helpful to you if you fetching values from Query here you can use array function which will support in PHP 5.5+

$myfield_arr = array_column($query_result, 'myfield_name');

Say Good bye to loop! Enjoy Smart Code.

猛虎独行 2024-12-04 07:17:56

试试这个:

function array_2d_to_1d ($input_array) {
    $output_array = array();

    for ($i = 0; $i < count($input_array); $i++) {
      for ($j = 0; $j < count($input_array[$i]); $j++) {
        $output_array[] = $input_array[$i][$j];
      }
    }

    return $output_array;
}

Try this:

function array_2d_to_1d ($input_array) {
    $output_array = array();

    for ($i = 0; $i < count($input_array); $i++) {
      for ($j = 0; $j < count($input_array[$i]); $j++) {
        $output_array[] = $input_array[$i][$j];
      }
    }

    return $output_array;
}
带刺的爱情 2024-12-04 07:17:56

这取决于您的需要,但如果您想将 2d 数组减少为完全填充 2d 平面的 1d 曲线,您可能会寻找空间索引或空间填充曲线。有一些著名但不为人所知的曲线,如 z 曲线、希尔伯特曲线、皮亚诺曲线或摩尔曲线。您可以使用 L 系统写出这样的曲线。

It depends on what you need but if you want to reduce your 2d array to a 1d curve that completley fills the 2d plane you probably looking for a spatial index or a space-filling-curve. There are some famous and not so known like the z-curve, the hilbert curve, the peano curve or the moore curve. You can write such a curve with a L-system.

灯下孤影 2024-12-04 07:17:56

试试这个:

function array_to1d($a) {
    $out = array();
    foreach ($a as $b) {
        foreach ($b as $c) {
            if (isset($c)) {
                $out[] = $c;
            }
        }
    }
    return $out;
}

请注意,它包含一个测试来查看该值是否已设置(非空)。如果数组是具有不同长度的行的数组的转置,则该数组将具有空值某些细胞,如果您试图将这样的野兽线性化,则此检查会很有帮助。

Try this one:

function array_to1d($a) {
    $out = array();
    foreach ($a as $b) {
        foreach ($b as $c) {
            if (isset($c)) {
                $out[] = $c;
            }
        }
    }
    return $out;
}

Notice that it includes a test to see if the value is set (non-null). An array that's a transposition of an array with rows of varying length will have null values in some cells, and this check can be helpful if you're trying to linearize such a beast.

夜访吸血鬼 2024-12-04 07:17:56

PHP 5.3 之后,有一个名为 arrray_walk_recursive 的新函数

$data = [1,2,[3,4],5,[6,7,8],9];
$output = [];
array_walk_recursive($data, function($val) use (&$output) {
    $output[] = $val;
});

请注意 $output通过引用传递

$output 将导致 [1,2,3,4,5,6,7,8,9]< /代码>

After PHP 5.3 there have a new function called arrray_walk_recursive

$data = [1,2,[3,4],5,[6,7,8],9];
$output = [];
array_walk_recursive($data, function($val) use (&$output) {
    $output[] = $val;
});

Notice that the $output is passed by reference

The $output will result in [1,2,3,4,5,6,7,8,9]

一个人的旅程 2024-12-04 07:17:56

每当数组中有很多某物并且您只需要一个某物来代表它们时,您的第一个想法应该是array_reduce()以某种方式。

来自 PHP 文档:

array_reduce — 使用回调函数迭代地将数组减少为单个值

在 2D => 的情况下一维数组转换,某物数组;我们在一个数组中有很多这样的元素,但我们只想要其中一个。

function array2dTo1d($array2d)
{
    return array_reduce($array2d, function($carry, $array) {
        return array_merge($carry, $array);
    }, []);
}

var_dump(array2dTo1d(
    [
        [23423, 5454, 567],
        [4565, 687],
    ]
));

结果:

array(5) {
  [0] =>
  int(23423)
  [1] =>
  int(5454)
  [2] =>
  int(567)
  [3] =>
  int(4565)
  [4] =>
  int(687)
}

Your first thought should be array_reduce() whenever you have lots of something in an array and you just want one something by itself which represents them all in some way.

From the PHP docs:

array_reduce — Iteratively reduce the array to a single value using a callback function

In the case of 2D => 1D array conversion, the something is array; we have lots of them in an array but we just want one of them by itself.

function array2dTo1d($array2d)
{
    return array_reduce($array2d, function($carry, $array) {
        return array_merge($carry, $array);
    }, []);
}

var_dump(array2dTo1d(
    [
        [23423, 5454, 567],
        [4565, 687],
    ]
));

Result:

array(5) {
  [0] =>
  int(23423)
  [1] =>
  int(5454)
  [2] =>
  int(567)
  [3] =>
  int(4565)
  [4] =>
  int(687)
}
走过海棠暮 2024-12-04 07:17:56

矩形二维数组的解决方案确实很简单,但我遇到了问题。我的二维数组由不同长度的一维数组组成:

$myArray = [
    [1, 2, 3, 4],
    [5, 6, 7],
    [8, 9]
];

我想出了更通用的解决方案将任何二维数组转换为一维:

function array2DTo1D($arr2D) {
    $i = 0; $j = 0;
    $arr1D = [];
    while (isset($arr2D[$i][0])) {
        while (isset($arr2D[$i][$j])) {
            $arr1D[] = $arr2D[$i][$j];
            $j++;
        }
        $i++; $j = 0;
    }
    return $arr1D;
}

The solution for rectangular 2D array is simple indeed, but I had a problem. My 2D-array consisted of 1D arrays of different lengths :

$myArray = [
    [1, 2, 3, 4],
    [5, 6, 7],
    [8, 9]
];

I came up with more generalized solution to turn any 2D array into 1D:

function array2DTo1D($arr2D) {
    $i = 0; $j = 0;
    $arr1D = [];
    while (isset($arr2D[$i][0])) {
        while (isset($arr2D[$i][$j])) {
            $arr1D[] = $arr2D[$i][$j];
            $j++;
        }
        $i++; $j = 0;
    }
    return $arr1D;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文