多维数组 PHP 内爆

发布于 2024-10-20 20:16:58 字数 128 浏览 2 评论 0原文

就我的数据结构而言,我有一个 communications 数组,每个 communications_id 本身包含三部分信息:id、score 和 content。

我想内爆这个数组以获得逗号分隔的 id 列表,我该怎么做?

In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content.

I want to implode this array in order to get a comma separated list of ids, how do I do this?

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

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

发布评论

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

评论(5

玩套路吗 2024-10-27 20:16:58

PHP 5.5 更新

PHP 5.5 引入了 array_column 这是整个 array_map 类用法的便捷快捷方式;它也适用于此。

$ids = array_column($communications, 'id');
$output = implode(',', $ids);

原始答案

您需要从通信数组中创建一组仅包含 id 的数组。那么内爆就微不足道了。

提示:该函数为 array_map< /代码>

解决方案:

假设 PHP 5.3,否则您必须将回调编写为字符串。

$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);

Update for PHP 5.5

PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here.

$ids = array_column($communications, 'id');
$output = implode(',', $ids);

Original answer

You need to make an array of just ids out of your array of communications. Then the implode would be trivial.

Hint: the function for that is array_map.

Solution:

Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.

$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);
因为看清所以看轻 2024-10-27 20:16:58

您可以查看 array_walk_recursive 函数 。这是创建递归数组到字符串转换的工作片段:

$array = 
  array(
    "1"    => "PHP code tester Sandbox Online",  
    "foo"  => "bar", 
     5 , 
     5     => 89009, 
    "case" => "Random Stuff", 
    "test" => 
       array(
         "test"  => "test221",
         "test2" => "testitem"
       ),
    "PHP Version" => phpversion()
  );

$string="";

$callback = 
  function ($value, $key) use (&$string) {
     $string .= $key . " = " . $value . "\n";
  };

array_walk_recursive($array, $callback);

echo $string;
## 1 = PHP code tester Sandbox Online
## foo = bar
## 2 = 5
## 5 = 89009
## case = Random Stuff
## test = test221
## test2 = testitem
## PHP Version = 7.1.3

You can have a look to array_walk_recursive function . This is a working snippet of creating of recursive array to string conversion :

$array = 
  array(
    "1"    => "PHP code tester Sandbox Online",  
    "foo"  => "bar", 
     5 , 
     5     => 89009, 
    "case" => "Random Stuff", 
    "test" => 
       array(
         "test"  => "test221",
         "test2" => "testitem"
       ),
    "PHP Version" => phpversion()
  );

$string="";

$callback = 
  function ($value, $key) use (&$string) {
     $string .= $key . " = " . $value . "\n";
  };

array_walk_recursive($array, $callback);

echo $string;
## 1 = PHP code tester Sandbox Online
## foo = bar
## 2 = 5
## 5 = 89009
## case = Random Stuff
## test = test221
## test2 = testitem
## PHP Version = 7.1.3
沫尐诺 2024-10-27 20:16:58

来自 http://snipplr.com/view.php?codeview&id=10187

class Format {
    static public function arr_to_csv_line($arr) {
        $line = array();
        foreach ($arr as $v) {
            $line[] = is_array($v) ? self::arr_to_csv_line($v) : '"' . str_replace('"', '""', $v) . '"';
        }
        return implode(",", $line);
    }

    static public function arr_to_csv($arr) {
        $lines = array();
        foreach ($arr as $v) {
            $lines[] = self::arr_to_csv_line($v);
        }
        return implode("\n", $lines);
    }

}

From http://snipplr.com/view.php?codeview&id=10187:

class Format {
    static public function arr_to_csv_line($arr) {
        $line = array();
        foreach ($arr as $v) {
            $line[] = is_array($v) ? self::arr_to_csv_line($v) : '"' . str_replace('"', '""', $v) . '"';
        }
        return implode(",", $line);
    }

    static public function arr_to_csv($arr) {
        $lines = array();
        foreach ($arr as $v) {
            $lines[] = self::arr_to_csv_line($v);
        }
        return implode("\n", $lines);
    }

}
江南月 2024-10-27 20:16:58

对于其他寻找答案的人来说,这就是我能够做的:

$singleDimensionalArray = array();

foreach($array["1"]["2"]["3"][...] as $value) {
    $singleDimensionalArray[] = $value;
}

我将其与 3 维数组一起使用。

For anyone else looking for an answer, this is what I was able to do:

$singleDimensionalArray = array();

foreach($array["1"]["2"]["3"][...] as $value) {
    $singleDimensionalArray[] = $value;
}

I used this with a 3-dimensional array.

看透却不说透 2024-10-27 20:16:58

此评论基于@jon解决方案,只需添加功能代码块

,但我必须使用循环,因为 array_map 不接受第二个参数

function array_column_implode($data_array = array(),$key = 'id', $delimiter = ',')
{
  if (function_exists('array_column'))
  {
    return implode($delimiter, array_column($data_array, $key));
  }
  else
  {
    $new_data_array = array();
    foreach ($data_array as $value) {
      if (isset($value[$key]))
      {
        $new_data_array[] = $value[$key];
      }
    }
    return implode($delimiter, $new_data_array);
  }
}

this comment based on @jon solution , just add functional code block

but i have to use loop because array_map does not accept second parameter

function array_column_implode($data_array = array(),$key = 'id', $delimiter = ',')
{
  if (function_exists('array_column'))
  {
    return implode($delimiter, array_column($data_array, $key));
  }
  else
  {
    $new_data_array = array();
    foreach ($data_array as $value) {
      if (isset($value[$key]))
      {
        $new_data_array[] = $value[$key];
      }
    }
    return implode($delimiter, $new_data_array);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文