PHP-创建数字为字符串的数组
我有这个方法在php中创建一个简单的jsonPHP-创建数字为字符串的数组,php,arrays,json,Php,Arrays,Json,我有这个方法在php中创建一个简单的json $array = array_merge($array, array($cf => $nome)); echo json_encode( $array ); 只有当cf不是数字时,它才起作用,例如: $cf = "12345"; $nome = "ASDS"; 结果是: ["ASDS"] {AS123:ASDS} 但如果我在“AS123”中更改cf,结果是: ["ASDS"] {AS123:ASDS} 完整代码为: w
$array = array_merge($array, array($cf => $nome));
echo json_encode( $array );
只有当cf
不是数字时,它才起作用,例如:
$cf = "12345";
$nome = "ASDS";
结果是:
["ASDS"]
{AS123:ASDS}
但如果我在“AS123”中更改cf,结果是:
["ASDS"]
{AS123:ASDS}
完整代码为:
while ( $row = mysqli_fetch_assoc( $query ) ) {
$cf = $row[ "cf" ];
$nome = $row[ 'nome' ];
$array = array_merge($array, array($cf => $nome));
}
echo json_encode( $array );
现在我需要将这个cf
和nome
转换成字符串,因为我在数字和空间方面有一些问题,我无法更改json结构,因为它是某些应用程序的API
我认为最终结果应该是这样的:
{"AS123":"ASDS"}
我认为您不需要
array\u merge
,您可以执行以下操作:
$array = array();
while ( $row = mysqli_fetch_assoc( $query ) ) {
$cf = $row[ "cf" ];
$nome = $row[ 'nome' ];
$array[$cf] = $nome;
}
echo json_encode( $array );
这将导致:
{"12345":"ASDS"}
非常感谢=)很乐意帮忙:)