2

Here I want to make an associative array.

My code:

while($row = mysql_fetch_assoc($mysql)){
    $data[] = $row;
    print_r($row);
}

print_r($row);

Array
(
    [employeeTraveld] => 1
    [total_Trip] => 23
)
Array
(
    [employeeTraveld] => 2
    [total_Trip] => 9
)
Array
(
    [employeeTraveld] => 3
    [total_Trip] => 8
)
Array
(
    [employeeTraveld] => 4
    [total_Trip] => 7
)

Using this above array, I want to make my expected output like:

employeeTraveld is 1 means I have to change the key value like SingleemployeeTraveld and value is 23 (total_Trip)


employeeTraveld is 2 means I have to change the key value like TwoemployeeTraveld and value is 9 (total_Trip)


employeeTraveld is 3 means I have to change the key value like ThreeemployeeTraveld and value is 8 (total_Trip)


employeeTraveld is 4 means I have to change the key value like FouremployeeTraveld and value is 7 (total_Trip)

Finally my expected result should come like this:

Array
(
    [SingleemployeeTraveld] => 23
    [TwoemployeeTraveld] => 9
    [ThreeemployeeTraveld] => 8
    [FouremployeeTraveld] => 7
)

My Updated Code

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);
4
  • Quote formatting in questions is meant to identify quoted text like from a document/reference/resource. It is not mean to be used a highlighter. Commented Mar 22, 2018 at 5:56
  • Please stop using mysql_ functions. Modernize your applications with mysqli or pdo. Commented Mar 22, 2018 at 5:59
  • 1
    I wonder why you aren't using One where you are using Single. Do you know if there is an upper limit to how many number-to-English translations you will need to do? Might you actually need to convert, say, 216 to TwoHundredSixteen? If your project will not get out-of-hand with the number-to-English assignments, you can hardcode a lookup array to avoid downloading a package. Commented Mar 22, 2018 at 6:01
  • @Kani are you going to respond to my request for clarification? Commented Mar 22, 2018 at 6:09

4 Answers 4

1

Convert number into words - plz check https://www.phptpoint.com/convert-number-into-words-in-php/ with the help of using this function, I tried to figure it out:

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);

Demo

Update

you can write these lines after foreach:

if(!empty($newArray)){
    $data['status'] = 'success';
    $data['data'] = array($newArray);
    echo json_encode($data);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Although this is probably the best answer on the page so far, this doesn't generate the desired Single string.
$ones = array( 1 => "one",...) can update "single" in place of "one".
But then that will have a knock-on effect with larger numbers generated dynamically ...ninety-single
thats why I used "one", this is also your question to @kani in comment, I think he can explain it better.
@Bhaskar Jain I need output like this how can i get? { "status": "success", "data": [{ "SingleemployeeTraveld": "10", "TwoemployeeTraveld": "7", "ThreeemployeeTraveld": "4", "FouremployeeTraveld": "4", }] }
|
1
$newarray = array();
foreach($array as $array2){

    if($array2['employeeTraveld'] == 1){
        $newarray['SingleemployeeTraveld'][] =  $array2['total_Trip'];
    }
    if($array2['employeeTraveld'] == 2){
        $newarray['TwoemployeeTraveld'][] =  $array2['total_Trip'];
    }
    if($array2['employeeTraveld'] == 3){
        $newarray['ThreeemployeeTraveld'][] =  $array2['total_Trip'];
    }
    if($array2['employeeTraveld'] == 4){
        $newarray['FouremployeeTraveld'][] =  $array2['total_Trip'];
    }
}
echo json_encode($newarray);

1 Comment

Code-only answers are low-value on StackOverflow because they do a poor job of educating the OP and future researchers. Please improve your post with the intent to educate.
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);

You can use this script. First loop through the array. You have a numeric values so you need to get the text from that. I,e 1 to One, 2 to Two, etc.

For this purpose we have use $numberToWord = new Numbers_Words(); and then $newkeyWord = ucfirst($numberToWords->toWords($value["employeeTraveld"])); here toWords return one if the number is 1, two if the string is 2 and so on. We have used ucfirst to convert first letter into capital. Finally when we have our key ready i.e OneemployeeTraveld, TwoemployeeTraveld, etc, we have added respective total_Trip to that key in newArray. You will get the expected output in newArray.

Here is a documentation link for Numbers_Words() You need to use this package for that.Include this in your file.

Number_Words

Ucfirst

3 Comments

I tried like this but nothing is displaying $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);
I want to download any package and i have to include in my files ?
Download pear package in your pc. Move Number folder in your project folder.Include words.php in your file like require('Numbers/Words.php'); please check updated answer. @KaniR
0

Please try This

$finalData = array();

while($row = mysql_fetch_assoc($mysql)){

$finalData[] = array( convertToWord($row['employeeTraveld']).'employeeTraveld' => $row['value'] );

}

print_r($finalData);

function convertToWord($number) {

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);

return $f->format($number);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.