这里我想制作一个关联数组。
我的代码:
while($row = mysql_fetch_assoc($mysql)){
$data[] = $row;
print_r($row);
}
的print_r($行);
Array
(
[employeeTraveld] => 1
[total_Trip] => 23
)
Array
(
[employeeTraveld] => 2
[total_Trip] => 9
)
Array
(
[employeeTraveld] => 3
[total_Trip] => 8
)
Array
(
[employeeTraveld] => 4
[total_Trip] => 7
)
使用上面这个数组,我希望得到我的预期输出:
employeeTraveld
1
表示我必须更改键值SingleemployeeTraveld
,值为23
(total_Trip)
employeeTraveld
2
表示我必须更改键值TwoemployeeTraveld
,值为9
(total_Trip)
employeeTraveld
3
表示我必须更改键值ThreeemployeeTraveld
,值为8
(total_Trip)
employeeTraveld
4
表示我必须更改键值FouremployeeTraveld
,值为7
(total_Trip)
最后我的预期结果应该是这样的:
Array
(
[SingleemployeeTraveld] => 23
[TwoemployeeTraveld] => 9
[ThreeemployeeTraveld] => 8
[FouremployeeTraveld] => 7
)
我的更新代码
include 'dbconfig.php';
require('Numbers/Words.php');
date_default_timezone_set('Asia/Kolkata');
$sql = "SELECT EmpId as employeeTraveld, Count(tripId) AS total_Trip
FROM
(
SELECT COUNT(empID) AS empID, tm.tripID
FROM trip_member as tm
INNER JOIN trip_details as td
ON tm.tripId = td.tripId
WHERE tripDate BETWEEN '$today' AND '$today'
GROUP BY
tripid
) AS trip_member
GROUP BY
EMPID
ORDER BY
EMPID";
$mysql = mysql_query($sql);
while($row = mysql_fetch_assoc($mysql)){
$newArray = new array();
foreach($row as $key=>$value){
$numberToWord = new Numbers_Words();
$newkeyWord = ucfirst($numberToWords->toWords($value["employeeTraveld"]));
$newkey = $newkeyWord.$value["employeeTraveld"];
$newArray[$newkey] = $value["total_Trip"];
}
}
print_r($newArray);
答案 0 :(得分:1)
select station_name, substr(to_char(arrival_time), 0, 8)
from table;
答案 1 :(得分:1)
将数字转换为单词 - 在使用此功能的帮助下检查https://www.phptpoint.com/convert-number-into-words-in-php/,我试图弄明白:
function numberTowords($num) {
$ones = array(
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
10 => "ten",
11 => "eleven",
12 => "twelve",
13 => "thirteen",
14 => "fourteen",
15 => "fifteen",
16 => "sixteen",
17 => "seventeen",
18 => "eighteen",
19 => "nineteen"
);
$tens = array(
1 => "ten",
2 => "twenty",
3 => "thirty",
4 => "forty",
5 => "fifty",
6 => "sixty",
7 => "seventy",
8 => "eighty",
9 => "ninety"
);
$hundreds = array(
"hundred",
"thousand",
"million",
"billion",
"trillion",
"quadrillion"
); //limit t quadrillion
$num = number_format($num, 2, ".", ",");
$num_arr = explode(".", $num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",", $wholenum));
krsort($whole_arr);
$rettxt = "";
foreach ($whole_arr as $key => $i) {
if ($i < 20) {
$rettxt .= $ones[$i];
} elseif ($i < 100) {
$rettxt .= $tens[substr($i, 0, 1)];
$rettxt .= "" . $ones[substr($i, 1, 1)];
} else {
$rettxt .= $ones[substr($i, 0, 1)] . "" . $hundreds[0];
$rettxt .= "" . $tens[substr($i, 1, 1)];
$rettxt .= "" . $ones[substr($i, 2, 1)];
}
if ($key > 0) {
$rettxt .= "" . $hundreds[$key] . "";
}
}
if ($decnum > 0) {
$rettxt .= "and";
if ($decnum < 20) {
$rettxt .= $ones[$decnum];
} elseif ($decnum < 100) {
$rettxt .= $tens[substr($decnum, 0, 1)];
$rettxt .= "" . $ones[substr($decnum, 1, 1)];
}
}
return $rettxt;
}
$arr = array(
array(
'employeeTraveld' => 1,
'total_Trip' => 23
),
array(
'employeeTraveld' => 2,
'total_Trip' => 9
),
array(
'employeeTraveld' => 3,
'total_Trip' => 8
),
array(
'employeeTraveld' => 4,
'total_Trip' => 7
),
);
foreach ($arr as $subArr) {
$numText = numberTowords($subArr['employeeTraveld']);
$newKey = ucfirst($numText) . 'employeeTraveld';
$newArray[$newKey] = $subArr['total_Trip'];
}
echo "<pre>";
print_r($newArray);
<强>更新强>
你可以在foreach
之后写下这些行:
if(!empty($newArray)){
$data['status'] = 'success';
$data['data'] = array($newArray);
echo json_encode($data);
}
答案 2 :(得分:0)
require('Numbers/Words.php');
$newArray = new array();
foreach($row as $key=>$value){
$numberToWord = new Numbers_Words();
$newkeyWord = ucfirst($numberToWords->toWords($value["employeeTraveld"]));
$newkey = $newkeyWord.$value["employeeTraveld"];
$newArray[$newkey] = $value["total_Trip"];
}
print_r($newArray);
您可以使用此脚本。首先循环遍历数组。您有一个数值,因此您需要从中获取文本。我,1对1,2对2,等等。
为此目的,我们使用$numberToWord = new Numbers_Words();
然后$newkeyWord = ucfirst($numberToWords->toWords($value["employeeTraveld"]));
这里toWords返回一个如果数字是1,如果字符串是2则返回2,依此类推。我们使用ucfirst将第一个字母转换为大写字母。最后,当我们准备好密钥时,即OneemployeeTraveld,TwoemployeeTraveld等,我们在newArray中为该密钥添加了相应的total_Trip
。您将在newArray中获得预期的输出。
以下是Numbers_Words()
的文档链接
你需要使用这个包。将它包含在你的文件中。
答案 3 :(得分:0)
请尝试此
$ finalData = array();
while($ row = mysql_fetch_assoc($ mysql)){
$ finalData [] = array( convertToWord($行[&#39; employeeTraveld&#39;])。&#39; employeeTraveld&#39; =&GT; $行[&#39;值&#39;] );
}
的print_r($ FinalData的);
function convertToWord($ number){
$ f = new NumberFormatter(&#34; en&#34;,NumberFormatter :: SPELLOUT);
返回$ f-&gt;格式($ number);
}