Php $value as an array learning
Get the solution ↓↓↓Solution:
Yes, you should be using an array.
$value = [1, 2, 5, 7];
If your dataset is a string, you can convert it to an array easily buy splitting on the comma and trimming any whitespace (for CSV style data):
$value = "1, 2, 5, 7";
$newValue = array_map('trim', explode(',', $value));
// $newValue is now [1, 2, 5, 7];
You can check if a value is contained inside the array with in_array():
if (in_array(5, $value)) {
echo "The number 5 is in the dataset!";
}
There are tons of functions you can do with arrays to sort, filter, and do pretty much whatever you want with your dataset: https://www.php.net/manual/en/ref.array.php
Answer
Solution:
Put your values in an array and check to see if they are in the array. And as you'll find out=
is for assignment not comparison which is==
.
$value = [1, 2, 5, 7];
If (in_array(1,$value)) print "One, ";
If (in_array(2,$value)) print "Two, ";
If (in_array(3,$value)) print "Three, ";
If (in_array(4,$value)) print "Four, ";
If (in_array(5,$value)) print "Five, ";
If (in_array(6,$value)) print "Six, ";
If (in_array(7,$value)) print "Seven, ";**
Answer
Solution:
It sounds as if this is a substitution(translation) problem:
<?php
$value = "1, 2, 5, 7";
$map = [
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven'
];
print strtr($value, $map);
Output:
One, Two, Five, Seven
Answer
Solution:
If your goal is to learn how you would structure this program and what tools can get you there, then here is some information on tools available.
First thing to do would be to convert this data to an array, using :
$valueArray = explode(',', $value);
Then you'd want to iterate over it with :
foreach ($valueArray as $data) {
}
Finally you could utilize the on the data items after wrangling their types:
$wrangledData = (int)trim($data);
$spelledOut = NumberFormatter::format($wrangledData, NumberFormatter::SPELLOUT);
And you could return it back to a string with :
$value = implode(', ', $valueArray);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 7.3.0".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.