【发布时间】:2023-03-09 03:49:02
【问题描述】:
我有一个看似平淡无奇的问题,我很欣赏以前打开过类似的线程,但我无法让它工作,所以请多多包涵:
假设我有一个数组$field,其中包含 mySQL INSERT 语句的所有字段名称。然后我有一个关联数组$types,其中包含这些字段的(mysqliprepared statement)类型,如下所示:
<?php
$fields = ["id","name","val","city"];
$type = ["name"=>"s","id"=>"i","city"=>"s","val"=>"d"];
$type 数组将 $fields 的所有值作为键,但它们的顺序不一定相同。我将如何使用所有字段的类型缩写生成一个新数组..
$fieldtypes = ["s", "i", "d", "s"]
对于准备好的语句,我可以将其分解为正确的类型字符串"sids"。
我一直在尝试
$try = array_replace(array_flip($fields),$type);
正如here 建议的那样,它适用于内爆功能,因为数组元素现在的顺序正确......
array(4) { ["id"]=> string(1) "i" ["name"]=> string(1) "s" ["val"]=> string(1) "d" ["city"]=> string(1) "s" }
...但我很想知道如何生成一个数组,该数组也可以作为正确的键,如下所示:
array(4) { ["0"]=> string(1) "i" ["1"]=> string(1) "s" ["2"]=> string(1) "d" ["3"]=> string(1) "s" }
【问题讨论】:
-
是什么让这些键“正确”?无论如何,只需在结果数组上调用
array_values就可以了。 -
以正确的顺序初始化
$type数组?
标签: php arrays sorting prepared-statement