大佬教程收集整理的这篇文章主要介绍了php-标准化关联数组列表以写入CSV,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
给定形式的数组
[
['id' => 1, 'name' => 'foo', 'description' => 'the foo described'],
['id' => 2, 'name' => 'bar'],
['id' => 3, 'description' => 'the one that shall not be named'],
]
即,每个元素都是一个关联数组,其中大多数值是可选的.
"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'],
]
如果有帮助:
>键总是按相同顺序
>如有必要,可以生成所有可能键的列表
解决方法:
您可以将array_fill_keys
、array_map
和array_merge
用于您的问题:
<?PHP
$keys = ['id', 'name', 'description'];
$template = array_fill_keys($keys, '');
$array = [
['id' => '1', 'name' => 'foo', 'description' => 'description 1'],
['id' => '2', 'name' => 'bar'],
['id' => '3', 'description' => 'description 2']
];
$normalized = array_map(function($item) use ($templatE) {
return array_merge($template, $item);
}, $array);
以上是大佬教程为你收集整理的php-标准化关联数组列表以写入CSV全部内容,希望文章能够帮你解决php-标准化关联数组列表以写入CSV所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。