Converting a Date Number String to Word in PHP [duplicate] - php

This question already has answers here:
Is there an easy way to convert a number to a word in PHP?
(14 answers)
Closed last month.
I have a PHP script outputting dates like this:
03/01/2023
Now I wish to make them output the above value as
Zero Three/Zero One/Two Zero Two Three
I have tried to create functions that return string data of that date number but did not get the exact result.

Here you can pass your single digit date value to the function and in return it will give you the word.
You will need to modify this to some bit according to your need.
<?php
echo $key = getWord(0);
function getWord($a){
$array = array(
'0'=>'Zero',
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
'4' => 'Four',
'5' => 'Five',
'6' => 'Six',
'7' => 'Seven',
'8' => 'Eight',
'9' => 'Nine',
);
return $array[$a];
}
?>

Related

Use subarray keys to form new first level keys to group values [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have an array $a in the below format. I have to separate the array and value with same key. For example first array have 9 and 10 key, last array 9 and 10. so both the array have to merge.
$a = [
['9' => 0, '10' => 5000],
['1' => -5000, '2' => 0],
['1' => -1600, '2' => 0],
['9' => 0, '10' => 5200],
];
need to convert the array like the below format
[
'9' => [0, 0],
'10' => [5000, 5200],
'1' => [-5000, -1600],
'2' => [0, 0]
]
You were in the right direction, you just needed the [] in front of $key to achieve your desired result, as shown below:
$samearray = array();
foreach ($a as $det) {
foreach ($det as $key => $det1) {
$samearray[$key][] = $det1;
}
}
print_r($samearray);

Change languages in PHP echo or print

Please help me to solve this issue. I'm not sure is it possible or not! I need a small tricky solution.
<?php include_once"system/hijri_calendar.php";
echo (new hijri\datetime()) -> format ('_j');
?>
Above code gives me an output of integer (1...30) in English.
Now, After echo, I want to change this English language to other languages.
Example:
Say Above code gives me an output 1
I want to change this output (1) to other languages (১)
If I get you right you are trying to get the value from one array based on the value and key from another array. You can use array_search() to find the key from array based on value
<?php
$en = array(1,2,3,4,5,6,7,8,9,0);
$bn = array('১','২','৩','৪','৫','৬','৭','৮','৯','০');
var_dump(array_search(5, $en)); // gives the key 4 from $en where value is 5
// array keys strart with 0
// so you can do
var_dump($bn[array_search(5, $en)]); // gives ৬
PHPFiddle to play with
Quick and dirty:
function __($number, $lang)
{
if ($lang == 'en') {
return $number;
}
$translate = array();
$translate['bn'] = array(
'1' => '১',
'2' => '২',
'3' => '৩',
'4' => '৪',
'5' => '৫',
'6' => '৬',
'7' => '৭',
'8' => '৮',
'9' => '৯',
'0' => '০'
);
$translate['th'] = array(
'1' => '๑',
'2' => '๒',
'3' => '๓',
'4' => '๔',
'5' => '๕',
'6' => '๖',
'7' => '๗',
'8' => '๘',
'9' => '๙',
'0' => '๐'
);
$digits = str_split($number,1);
$return_this = '';
foreach($digits as $digit){
$return_this .= $translate[$lang][$digit];
}
return $return_this;
}
echo __('905','bn');
Break down, if the lang is en you get what you give, if bn or th, it'll break the number apart and rebuild it using the requested array.
This is basically how I do I18n except the arrays for each language are in their own files.
I've changed the arrays slightly to simplify things. If the array is in the order of the digits (so I've moved the 0 element to position 0 in the array) you can use...
$bn = array('০','১','২','৩','৪','৫','৬','৭','৮','৯');
$in = "18";
$out = "";
foreach ( str_split($in) as $ch ) {
$out .= $bn[$ch];
}
echo $out;

Count unique values in a column of an array

I have an array like this:
$arr = [
1 => ['A' => '1', 'C' => 'TEMU3076746'],
2 => ['A' => '2', 'C' => 'FCIU5412720'],
3 => ['A' => '3', 'C' => 'TEMU3076746'],
4 => ['A' => '4', 'C' => 'TEMU3076746'],
5 => ['A' => '5', 'C' => 'FCIU5412720']
];
My goal is to count the distinct values in the C column of the 2-dimensional array.
The total rows in the array is found like this: count($arr) (which is 5).
How can I count the number of rows which contain a unique value in the 'C' column?
If I removed the duplicate values in the C column, there would only be: TEMU3076746 and FCIU5412720
My desired output is therefore 2.
Hope this simplest one will be helpful. Here we are using array_column, array_unique and count.
Try this code snippet here
echo count(
array_unique(
array_column($data,"C")));
Result: 2
combine array_map, array_unique, count
$array = [ /* your array */ ];
$count = count(
array_unique(
array_map(function($element) {
return $element['C'];
}, $array))))
or use array_column as suggested by sahil gulati, array_map can do more stuff which probably isn't needed here.
I had a very similar need and I used a slightly different method.
I have several events where teams are participating and I need to know how many teams there are in each event. In other words, I don't need to only know how many distinct item "C" there are, but how many items TEMU3076746 and FCIU5412720 there are.
The code will then be as is
$nbCs = array_count_values ( array_column ( $array, 'C' ) );
$nbCs will issue an array of values = Array([TEMU3076746] => 3 [FCIU5412720] => 2)
See example in sandbox Sandbox code
$data=array();
$data=[
1 => [
'A' => '1'
'C' => 'TEMU3076746'
]
2 => [
'A' => '2'
'C' => 'FCIU5412720'
]
3 => [
'A' => '3'
'C' => 'TEMU3076746'
]
4 => [
'A' => '4'
'C' => 'TEMU3076746'
]
5 => [
'A' => '5'
'C' => 'FCIU5412720'
]
];
$total_value=count(
array_unique(
array_column($data,"C")));
echo $total_value;
Most concisely, use array_column()'s special ability to assign new first level keys using the targeted column's values. This provides the desired effect of uniqueness because arrays cannot contain duplicate keys on the same level.
Code: (Demo)
echo count(array_column($arr, 'C', 'C')); // 2
To be perfectly clear, array_column($arr, 'C', 'C') produces:
array (
'TEMU3076746' => 'TEMU3076746',
'FCIU5412720' => 'FCIU5412720',
)
This would also work with array_column($arr, null, 'C'), but that create a larger temporary array.
p.s. There is a fringe case that may concern researchers who are seeking unique float values. Assigning new associative keys using float values is inappropriate/error-prone because the keys will lose precision (become truncated to integers).
In that fringe case with floats, fallback to the less performant technique: count(array_unique(array_column($arr, 'B))) Demo

letters to touch tone number output

I tried Googling this but not sure what's the best thing to look for. What I am trying to do is to translate a text input to output the letters of a touch tone phone. For example Hello World would output 43550 96153the idea is I'm trying to use the tropo voice api system and want the user to be able to enter their name as touch tone values and match that to their name as numbers in my database.
I'm assuming this can be done with a function along the lines of
$input= $touchtone_value;
$number_two_array (a,b,c);
if( $input==in_array($number_two_array)){
$output = '2';
}
I'm sure this will work. However, if there is a class out there or a simpler function than to break each letter into number arrays I think that would be a better way to do it. At this point this is a fairly open ended question as I have NO IDEA where to start as the best way to accomplish this.
EDIT: I found a solution, not sure it's the best one.
$input = strtolower('HELLO WORLD');
echo 'input: '. $input. "\n";
echo $output = 'output: '. strtr($input,'abcdefghijklmnopqrstuvwxyz', '22233344455566677778889999');
input:hello world
output: 43556 96753
Now I just need to find a way to remove white space :)
http://codepad.org/Ieug0Zuw
Source: code a number into letters
PHP provides a function called strtr which does string translations. The first argument is what you want to translate, the second is the original characters, the third is the replacement characters. Below is a function that should do what you want. Edit: Updated my sample to strip out any characters that aren't supported (anything other than a-z or a space)
<?php
function get_tones($inp) {
$from = 'abcdefghijklmnopqrstuvwxyz ';
$to = '222333444555666777788899990';
// convert the input to lower case
$inp = strtolower($inp);
// remove anything that isn't a letter or a space
$inp = preg_replace('/[^a-z ]/', '', $inp);
return strtr($inp, $from, $to);
}
assert(get_tones('Hello world') == '43556096753');
assert(get_tones('Hell234"*&o world') == '43556096753');
assert(get_tones('ALEX') == '2539');
assert(get_tones(' ') == '0000');
How about a structure like this... NOTE: This will ignore invalid 'letters' like spaces, punctuation, etc..
LIVE DEMO http://codepad.org/pQHGhm7Y
<?php
echo getNumbersFromText('Hello There').'<br />';
echo getNumbersFromText('This is a really long text string').'<br />';
function getNumbersFromText($inp){
$result=array();
$inp = strtolower($inp);
$keypad = array('a' => '2', 'b' => '2', 'c' => '2', 'd' => '3',
'e' => '3', 'f' => '3', 'g' => '4', 'h' => '4',
'i' => '4', 'j' => '5', 'k' => '5', 'l' => '5',
'm' => '6', 'n' => '6', 'o' => '6', 'p' => '7',
'q' => '7', 'r' => '7', 's' => '7', 't' => '8',
'u' => '8', 'v' => '8', 'w' => '9', 'x' => '9',
'y' => '9', 'z' => '9');
for ($x=0; $x<strlen($inp); $x++){
$letter = $inp[$x];
if ($keypad[$letter]) $result[]= $keypad[$letter];
}
return implode('',$result);
}
?>
I'm not sure I understand exactly what you are asking:
Your input is words, and you want to output the corresponding numbers?
Your input is numbers, and you want to output the corresponding words?
In case (1), it's simple, just use an array that maps each letter of the alphabet to the corresponding numbers (i.e. key is letter, value is number). You can then just iterate over the characters of the input, and output using the corresponding element in the array.
Case (2) is a bit trickier. You can build a Trie from your list of names, and use it to do the lookup.

Is there any php function to convert textual numbers to numbers? For example: convert("nine") outputs 9 [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Converting words to numbers in PHP
I need a function to convert textual numbers to numbers.
For example: convert("nine") outputs 9
I know I could write a function like for example if ($number == "one") { $number = 1; } etc...
but that would be a lot of work.
Use the switch statement.
Use a lookup table like this:
$numbers = array(
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9
);
$digit = strtolower($digit);
if (isset($numbers[$digit])) {
$digit = $numbers[$digit];
}
Note I use strtolower just in case. Of course, this solution would be impractical for anything over a couple dozen. Beyond that, you'll need some sort of parser.

Categories