Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I Want to explode custom numbers of variables.
My php code is here
$date="23-05-2015";
$var = "$var1, $var2, $var3";
// I want get $var value in list($var) = what I want{ list($var1, $var2, $var3)}
list($var) = explode('-', $date, 3);
The $var variable is weird, it keeps a list of comma-separated names of other variables....
Why would you do that ?
You have 2 options:
Either you use the list() on the variable names:
$date="23-05-2015";
list($var1, $var2, $var3) = explode('-', $date, 3);
Or you do the extract() hack, and keep the $var variable as is:
$date="23-05-2015";
$var = "$var1,$var2,$var3";
extract(array_combine(explode(',',$var,3),explode('-', $date, 3)))
Obviously, I think option #1 is the way to go
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example i have the PHP code like this
if ($rating_count == $rating_notnull) {
$returnValue = 1;
}
if($project_count == $project_notnull) {
$returnValue = 2;
}
Now,I want to store two variable values into one single variable?
Arrays can be used to hold multiple values. They can have a key or not.
<?php
$x = array();
$x[] = 'Some Value';
$x[] = 'Another value';
var_dump($x);
$x = array();
$x['name'] = 'Anil';
$x['other name'] = 'Del Boy';
var_dump($x);
$x = array(
'Name' => 'Batman',
'Status' => 'Busy',
'etc' => 'etc',
);
var_dump($x);
Have a play with it here: https://3v4l.org/LCjtY
As explained in other comments the most efficient way would to be to use an array to store these values.
However, if you do wish to store this in a single variable you could use explode.
This will output it in an array when it is exploding however allows you to store it into one variable
$str = "Hello, this, is, a, variable";
explode(",", $str);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to calculate two variables type "String" in php.
and I try all alternative but steady amount missing values.
example:
$a = "250,000.50";
$b = "30,000.00";
echo $a - $b;
I want to return value with same format ###,###,###.## -String -Decimal -only return same format.
Note: I try all the link format currency here...use regex, sprintf, replace, etc...
but always lose a digit because the example use in format en_US or EUR.
Thanks!.
You can use this function for easily handle:
$a = "250,000.50";
$b = "30,000.00";
function currency($c=0){
return preg_replace("/[^\d\.]+/iu","",$c);
}
echo number_format(currency($a) - currency($b) ,2);
The subtraction on strings will auto-cast the strings into numbers for you, but you have to get rid of the commas first:
$a = "250,000.50";
$b = "30,000.00";
echo str_replace(',', '', $a) - str_replace(',', '', $b);
Oh, and wrap the whole thing in number_format to get it back to the comma format:
echo number_format( str_replace(',', '', $a) - str_replace(',', '', $b), 2);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let´s say I´ve got an array like [3,2,8,4] (just an example, it can have more or less values).
I want the numbers to be in the same order but instead use numbers 1-4 (if there are 4 values as in this example), ie. [2,1,4,3].
How can I accomplish this?
$data = [3,2,8,4];
$keys = array_keys($data);
array_multisort($data, SORT_ASC, $keys);
array_walk($keys, function(&$value) { ++$value; });
var_dump($keys);
,You could go with ArrayReplace.
<?php
$base = array(3 2, 8, 4);
$replacements = array(0 => 2, 1 => 1, 2 => 4, 3 => 3);
$store = array_replace($base, $replacements);
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my array I have these values:
[10d, 10e] => 4
The values are retrieved from another website and the important values will be put in this array.
Now I want to have two single values from these like:
[10d] => 2 [10e] => 2
How can I do this?
Now I'm guessing a little because of the description that is not that clear.
$arr = array('10d, 10e' => 4);
$newArr = array();
foreach($arr as $key => $value) {
$newKeys = explode(',', $key);
foreach($newKeys as $item) {
$newArr[trim($item)] = $value / count($newKeys);
}
}
print_r($newArr);
Result
[10d] => 2 [10e] => 2
Some functions you can use:
foreach() to loop the input array
explode() to split the key
trim() to remove whitespace
count() to, well, count array items
And, of course, / to divide ;-)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I add new item to array? For example into middle of an array ?Should we use array splice or an array merge ?
Could you explain me the difference between both function ?
Say I have
$a1=array("a"=>"Horse","b"=>"Dog","c"=>"Cow",);
$a2=array("d"=>"Cat");
Now I need to add $a2 in 2 position .
Which one should I use ?
You can use array_splice, except that won't keep your keys.
$a1 = array("a"=>"Horse", "b"=>"Dog", "c"=>"Cow");
$a2 = array("d"=>"Cat");
array_splice($a1, 2, 0, $a2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", 0=>"Cat", "c"=>"Cow");
If you want Cat to have a key of d, you can use a mix of array_slice and the array union operator (+):
$a1 = array_slice($a1, 0, 2) + $a2 + array_slice($a1, 2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", "d"=>"Cat", "c"=>"Cow");
you can use array_push to add array at any position. ..array_splice can also be used. .
example: array_splice