gpt4 book ai didi

php - 规范化关联数组列表以写入 CSV

转载 作者:可可西里 更新时间:2023-11-01 00:57:16 27 4
gpt4 key购买 nike

<分区>

给定一个数组

[
['id' => 1, 'name' => 'foo', 'description' => 'the foo described'],
['id' => 2, 'name' => 'bar'],
['id' => 3, 'description' => 'the one that shall not be named'],
]

即每个元素都是一个关联数组,其中大多数值都是可选的。

将其导出到 CSV 文件的最佳方式是什么?

"id","name","description"
"1","foo","the foo described"
"2","bar",""
"3","","the one that shall not be named"

为此,我可能需要将数组转换为:

[
['id' => 1, 'name' => 'foo', 'description' => 'the foo described'],
['id' => 2, 'name' => 'bar', 'description' => ''],
['id' => 3, 'name' => '', 'description' => 'the one that shall not be named'],
]

如果有帮助:

  • 键总是以相同的顺序
  • 如有必要,可以生成所有可能 key 的列表

我可以应用 PHP 数组函数的任何技巧吗?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com