Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mySQL将titlecase生成为JSON数组| PHP_Php_Mysql - Fatal编程技术网

mySQL将titlecase生成为JSON数组| PHP

mySQL将titlecase生成为JSON数组| PHP,php,mysql,Php,Mysql,如何在title-case中表示mySQL查询生成的JSON数组 这意味着,如果数据库中的记录显示为John smith或John smith,则数组中的记录将显示为John smith 目前,这就是我目前的情况: $stmt = $pdo->query(" `first`,`last`) FROM `Table1` "); $stmt->execute([]); $row = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_en

如何在title-case中表示mySQL查询生成的JSON数组

这意味着,如果数据库中的记录显示为John smith或John smith,则数组中的记录将显示为John smith

目前,这就是我目前的情况:

$stmt = $pdo->query(" `first`,`last`)
FROM `Table1` ");

$stmt->execute([]);

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($row);

您可以使用函数在PHP中执行相同的操作。使用它的功能

$a = 'John SNOW';
$strCamelCase = ucwords(strtolower($a));

Output:
John Snow
因此,您可以执行以下操作

$sql = "SELECT `first`,`last` FROM `Table1`";
$arrNames = [];
foreach ($pdo->query($sql) as $row) {
    $arrNames[] = [ 
                    'first' => ucwords(strtolower($row['first'])),
                    'last' => ucwords(strtolower($row['last']))
                  ];
}

echo json_encode($arrNames);

如何将其放入数组?不完全是这样,我需要将它们放入数组中,如下所示:
[{“first”:“Jane”,“last”:“Menson”},{“first”:“Jake”,“last”:“Pam”}]
。名字和姓氏不应重叠also@Ken,请参阅我的最新答案。如果对您有效,
接受答案
。是否有任何方法为所应用的行添加异常
ucwords(strtolower
?例如,如果一条记录说JASON将其保留为JASON,不要将其更改为JASON。只需比较
strtoupper('JASON')='JASON'
,如果为true,则不要应用此项,否则请使用它。