多维数组 PHP 内爆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5249876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multidimensional Array PHP Implode
提问by Spencer
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.
就我的数据结构而言,我有一个通信数组,每个communication_id 本身包含三条信息:id、分数和内容。
I want to implode this array in order to get a comma separated list of ids, how do I do this?
我想内爆这个数组以获得一个逗号分隔的 id 列表,我该怎么做?
回答by Jon
Update for PHP 5.5
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.
PHP 5.5 引入了array_column
这是一种方便的快捷方式来使用整个类array_map
;这里也适用。
$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.
您需要从通信数组中制作一组仅包含 id 的数组。那么内爆将是微不足道的。
Hint:the function for that is array_map
.
提示:该函数是array_map
.
Solution:
解决方案:
Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.
假设 PHP 5.3,否则您必须将回调编写为字符串。
$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);
回答by KB9
You can have a look to array_walk_recursivefunction . This is a working snippet of creating of recursive array to string conversion :
你可以看看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
回答by Nick
From http://snipplr.com/view.php?codeview&id=10187:
来自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);
}
}
回答by JonaK
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.
我将它与 3 维数组一起使用。
回答by Bdwey
this comment based on @jon solution , just add functional code block
此评论基于@jon 解决方案,只需添加功能代码块
but i have to use loop because array_map does not accept second parameter
但我必须使用循环,因为 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);
}
}