就我的数据结构而言,我有一个通信数组,每个communications_id本身包含三条信息: id、分数和内容。
为了得到一个逗号分隔的ids列表,我想内爆这个数组,我该怎么做呢?
发布于 2014-08-27 20:50:51
你可以看看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
发布于 2011-03-09 17:45:52
来自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);
}
}
发布于 2014-06-03 01:41:06
对于其他正在寻找答案的人来说,这是我能够做到的:
$singleDimensionalArray = array();
foreach($array["1"]["2"]["3"][...] as $value) {
$singleDimensionalArray[] = $value;
}
我将其用于一个三维数组。
https://stackoverflow.com/questions/5249876
复制相似问题