I would like to put a number separately in an array
ex.
$num = 345;
should be in an array so i can call the numbers as
$num[1] (which should return 4)
I tried str_split($num,1) but without succes.
Thanks
EDIT -------
After some more research str_split($num,1) actually did the trick.
(thanks, Crayon Violent)
$num = 345;
$arr1 = str_split($num); print_r($arr1); //Array ( [0] => 3 1 => 4
[2] => 5 )
echo $arr11; //4
str-split
If you are just trying to get individual characters from the string, use substr.
$second_digit = substr( $num, 1, 1 );
while ($num >0) {
$arr[i] = $num %10;
$num = $num/10;
i++
}
//this leaves the array in reverse order i.e. 543
//to flip
array_reverse($arr);
Related
I have an array that uses two codes in a list and pushes them into a form on the next page. Currently I have:
$code= array();
$code['c1'] = substr($part, 0, 1);
$code['c2'] = substr($part, 2);
Now, If I select anything with a single digit c1 and single or double c2 then it adds to the form.
Examples:
1-9
1-15
9-12
9-9
But if I try to add anything with double digits in c1 it doesn't add, like:
10-1
10-2
10-11
If I try
$codes= array();
$codes['c1'] = substr($part, 0, 2);
$codes['c2'] = substr($part, 2);
Then no codes show up.
How can I account for both?
UPDATE:
Currently, the above code, if I select 10-58, will dump c1 as 1 and c2 as -5
You can easily use explode() and list() to split any combination of codes...
$part = "1-15";
$codes = array();
list($codes['c1'], $codes['c2']) = explode("-", $part);
print_r($codes);
gives...
Array
(
[c1] => 1
[c2] => 15
)
For
$part = "10-15";
it gives...
Array
(
[c1] => 10
[c2] => 15
)
If you are unsure if your data is always correct, you can check that the data has 2 components after using explode() and only convert it then, you can also do something to report and error or whatever you need...
$split = explode("-", $part);
if ( count($split) == 2 ){
$codes['c1'] = $split[0];
$codes['c2'] = $split[1];
}
else {
// Not of correct format.
}
print_r($codes);
I have code to get minimum and maximum value from comma separated value ranges by using below given code
<?php
$price=$_GET['price'];
$grade = str_replace('-', ',', $price);
$number = array($grade);
$max = max($number);
$min = min($number);
echo "min value is $min <br/>";
echo "max value is $max <br/>";
?>
for the input ?price=0-5,4-30,6-50 This should output minimum value 0 and maximum value 50 but my above code is giving output as
min value is 0,5,4,30,6,50
max value is 0,5,4,30,6,50
Kindly guide me where i am making mistake or any other working alternate.
You are incorrect with creating an array. Please use explode for this. Explode function will break the string into array.
First parameter is the character on which you want to split the string and the second one is input. In your case it would be nice to add the str_replace function right there, so you don't change the original input.
$input = '0-5,4-30,6-50';
$numbers = explode(',', str_replace('-', ',', $input));
And now you can use min and max functions and they will work properly.
After using str_replace method, you are converting a string into an array, your array looks like:
Array ( [0] => 0,5,4,30,6,50 )
With this array, you cant achieve or get the maximum and minimum value from an array.
You need to explode your string with comma as:
$yourArr = explode(",", $grade); // this will convert string into array.
Now your result should looks like:
Array ( [0] => 0 [1] => 5 [2] => 4 [3] => 30 [4] => 6 [5] => 50 )
Complete Example:
<?php
$price='0-5,4-30,6-50';
$grade = str_replace('-', ',', $price);
$yourArr = explode(",", $grade);
$max = max($yourArr);
$min = min($yourArr);
echo "min value is $min <br/>";
echo "max value is $max <br/>";
?>
Result:
min value is 0
max value is 50
<?php
$price = '0-5,4-30,6-50';
if(preg_match_all('/\d+/', $price, $matches)) {
$min = min($matches[0]);
$max = max($matches[0]);
var_dump($min, $max);
}
Output:
string(1) "0"
string(2) "50"
I have a variable in php that has the following value:
var $line="'PHYSICAL':3.8,'ORGANIZATIONAL':4,'TECHNICAL':2.9";
From that line variable, I want to extract each word and values in php, so basically I want something like below:
var $word1=PHYSICAL;
var $word1value=3.8;
var $word2=ORGANIZATIONAL;
var $word2value=4;
var $word3=TECHNICAL;
var $word3value=2.9;
I want to capture all the words and values separately in different variables, so that later I can process them. Can anyone please assist me on this. Thanks.
The simplest way to do this is with preg_split. You can use the PREG_SPLIT_DELIM_CAPTURE flag to enable capturing the word without the surrounding quotes, and PREG_SPLIT_NO_EMPTY to remove the empty values that arise from this particular split pattern.
$line="'PHYSICAL':3.8,'ORGANIZATIONAL':4,'TECHNICAL':2.9";
$array = preg_split("/'([^']+)':|,/", $line, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
Output:
Array
(
[0] => PHYSICAL
[1] => 3.8
[2] => ORGANIZATIONAL
[3] => 4
[4] => TECHNICAL
[5] => 2.9
)
You can then post-process the array (for example using array_filter and array_combine) to produce something which might be more useful:
$newarray = array_combine(array_filter($array, function ($i) { return !($i % 2); }, ARRAY_FILTER_USE_KEY),
array_filter($array, function ($i) { return $i % 2; }, ARRAY_FILTER_USE_KEY));
print_r($newarray);
Output:
Array
(
[PHYSICAL] => 3.8
[ORGANIZATIONAL] => 4
[TECHNICAL] => 2.9
)
If you really want the individual variables you can do this:
for ($i = 0; $i < count($array); $i += 2) {
${'word' . intdiv($i+2, 2)} = $array[$i];
${'word' . intdiv($i+2, 2) . 'value'} = $array[$i+1];
}
echo "word1 = $word1\n";
echo "word1value = $word1value\n";
echo "word2 = $word2\n";
echo "word2value = $word2value\n";
echo "word3 = $word3\n";
echo "word3value = $word3value\n";
Output:
word1 = PHYSICAL
word1value = 3.8
word2 = ORGANIZATIONAL
word2value = 4
word3 = TECHNICAL
word3value = 2.9
I'm trying to find out wich variable is bigger (those are all integers):
<?php
$ectoA=3;
$ectoB=5;
$mesoA=0;
$mesoB=4;
$endoA=11;
$endoB=11;
echo max($ectoA,$ectoB,$mesoA,$mesoB,$endoA,$endoB);
I tried with max but it gives the value and not the $varName.
I want to get the name of the variable and if there are two that are equal I need both.
Thanks for the help.
As suggested i tried this and worked but still got to know if I have two MAX values I need to do something else...
$confronto = [
'ectoA' => $ectoA,
'ectoB' => $ectoB,
'endoA' => $endoA,
'endoB' => $endoB,
'mesoA' => $mesoA,
'mesoB' => $mesoB,
];
$result= array_keys($confronto,max($confronto));
$neurotipo = $result[0];
echo $neurotipo;
I want endoA and endoB to be identified...
You can define an array instead, or compact your variables into an array:
//$array = array('ectoA'=>3,'ectoB'=>5,'mesoA'=>0,'mesoB'=>4,'endoA'=>11,'endoB'=>11);
$array = compact('ectoA','ectoB','mesoA','mesoB','endoA','endoB');
$result = array_keys($array, max($array));
Then compute the max() of that array and use array_keys() to search for the max number and return the keys.
print_r($result);
Yields:
Array
(
[0] => endoA
[1] => endoB
)
I would definitely recommend using an array. Then you can do something like this:
$my_array = array(3, 5, 0, 4, 11, 11);
$maxIndex = 0;
for($i = 1; $i < count($my_array); $i++) {
if($my_array[$i] > $my_array[$maxIndex])
$maxIndex = $i;
}
Another option with array keys would be:
$my_array = array("ectoA" => 3, "ectoB" => 5, "mesoA" => 0, "mesoB" => 4, "endoA" => 11, "endoB" => 11);
$maxIndex = "ectoA";
while($c = current($my_array)) {
$key = key($my_array);
if($my_array[$key] > $my_array[$maxIndex])
$maxIndex = $key;
next($my_array);
}
Note: Code not tested, but should be the gist of what needs to be done
use the array like this
<?php
$value= array (
"ectoA" =>3,
"ectoB"=>5,
"mesoA"=>0,
"mesoB"=>4,
"endoA"=>11,
"endoB"=>11);
$result= array_keys($value,max($values))
print_r($result);
?>
As per the documentation, Since the two values are equal, the order they are provided determines the result $endoA is the biggest here.
But I am not sure your intention is to find the biggest value or which variable is the highest
Your code seems to be a working one.
But it would print for you the value of the maximum number, not the name of the variable.
To have the name of the variable at the end you should add some additional code like:
$max = (max($ectoA,$ectoB,$mesoA,$mesoB,$endoA,$endoB);
if($max == $ectoA) echo "ectoA";
if($max == $ectoB) echo "ectoB";
// ... same goes for other variables
But working with an array would be the most proper solution.
I am using the following code to populate an array:
$number = count ($quantitys);
$count = "0";
while ($count < $number) {
$ref[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}
postcodeUnknown returns the first row from a mysql query, using a table prefix and an element from an array named postcodes. $postcodes contains strings that should return a different row each time though the loop.
Which I'd expect to create an array similar to:
Array ([0] =>
Array ([0] => some data [1] => more data)
[1] =>
Array ([0] => second row [1] => even more...)
)
But it's not. Instead it's creating a strange array containing the first results over and over until the condition is met, eg:
simplified result of print_r($ref);
Array (
[0] => Array ([0] => some data [1] => more data)
)
Array(
[0] => Array (
[0] => the first arrays content...
[1] => ...repeated over again until the loop ends
)
)
And I'm at a loss to understand why. Anyone know better than me.
Try
$number = count ($quantitys);
$count = "0";
while ($count < $number) {
$ref1[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$ref2[] = postcodeUnknown ($prefix, $postcodes[$count]);
$ref3[$count][] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}
echo "<pre>";
print_r($ref1);
print_r($ref2);
print_r($ref3);
echo "</pre>";
Try that to see if any of those get an output you are looking for.
Uh, add a print to postcodeUnknown and check if its actually passing different postcodes or the same thing all the time?
ie
function postcodeUnkown($a,$b){
echo var_dump($a).var_dump($b);
//the rest
}
Also, why two different variables? ($quantitys and $postcodes).
Also, you might want to replace the while loop with for:
for($i=0;$i<$number;$i++){
$ref[$i] = postcodeUnknown ($prefix, $postcodes[$i]);
}
your variable count should not be a string, try this
$number = count ($quantitys);
$count = 0;
while ($count < $number) {
$ref[$count] = postcodeUnknown ($prefix, $postcodes[$count]);
$count = $count +1;
}