I have variable of type varchar having values $pointmoney=356+2311;
pointmoney is a field in database from where i am retreiving this value .
now i have separte two variable $point and $money
where i want to store $point=356 and $money=2311 from $pointmoney=356+2311.(i.e separate values from '+').
If any 1 knows any similar function then pls answer.
hope i ellaborated well to understand my query . if anything is unclear pls feel to comment .
Use explode():
$pointmoney = "356+2311";
list($point, $money) = explode('+', $pointmoney);
you explode function like
$pointmoney = "356+2311";
$arr_money = explode("+",$pointmoney);
echo $point = $arr_money[0];
echo $money= $arr_money[1].
hope this will sure help you,
You can use explode() reference PHP explode
$pointAndmoney = "356+2311";
list($point,$money) = explode('+',$pointAndmoney); // now you have different variable for each
$arr = explode("+","54+99");
$point = $arr[0];
$money = $arr[1];
Related
I have this autogenerated code:
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
How can I save to a var the first and the last part of this code like this?
$first = "k9sdhfkr9235kdh5";
$last = "fdh4hwshnchjgrj";
The code is always separated by this character (|) and the code consists random character number so sometimes it is 16 characters, sometimes 11 etc...
foreach($code as $v){
$pos = strpos($v, "|");
$first = substr($v,...?
You may use the explode
explode("|",$code );
It will return an array of values
You can use builtin functionality called explode(); given below the solution for your question
<?
$code = "k9sdhfkr9235kdh5|fdh4hnchjgrj";
$codeArr=explode("|",code);
$first = $codeArr[0];
$last = $codeArr[1];
?>
Please try this. More about the explode function is here
Here is an example of what I am trying to accomplish:
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
echo $array['aaa']$subarray; // these 2 echos should be the same
echo $array['aaa']['bbb']['ccc']; // these 2 echos should be the same
It should display the same as $array['aaa']['bbb']['ccc'] i.e., "value".
This doesnt work, of course. But is there some simple solution to this?
There could be some function and the $subarrayvalue may be used as a parametr and/or as an array itself like: $subarray = array('bbb','ccc'); I dont mind as long as it worsk.
You could try something like below.
$subarray = "['bbb']['ccc']";
$temp = parse_str("\$array['aaa']".$subarray);
echo $temp;
OR To ignore single quotes -
$subarray = "[\'bbb\'][\'ccc\']";
$temp = parse_str("\$array[\'aaa\']".$subarray);
echo $temp;
Also you may refer - http://php.net/manual/en/function.parse-str.php
Just try using array chunk function http://php.net/manual/en/function.array-chunk.php
here is what actually works!!
$array['aaa']['bbb']['ccc'] = "value";
$subarray = "['bbb']['ccc']";
$string = 'echo $array[\'aaa\']' . $subarray . ';';
eval($string);
I have this small code , why it does not work and how to make it correctly ?
$temp = $_SESSION['contactPersonInterest'][$i];
$temp += ',Medlemskort';
//$_SESSION['contactPersonInterest'][$i] = $temp;
I am testing it with
?><script>alert('<?php echo $_SESSION['contactPersonInterest'][$i] ?>'+'----------'+'<?php echo $temp ?>');</script> <?php
And what i get is :
blbla,blll----------0
Whats wrong ?
Thank you
String concatenation is done with . in PHP. Try:
$temp .= ',Medlemskort';
Otherwise you perform addition, and if both strings don't start with numbers, they will be converted to 0 and 0 + 0 = 0 :)
Have a look at Type Juggling.
That's because += is an operator for adding integers, not strings. You want to concatenate strings (which is "."). Also, there is no need to create a temporary variable, only to overwrite the existing one. This should work:
$_SESSION['contactPersonInterest'][$i] .= ',Medlemskort';
You wrongly assign more things to the variable via +. You should use . instead.
$temp .= ',Medlemskort';
If you want $i to have temp's value, no need for the +=:
$temp = ""; // good habit to initialize before usage
$temp = $_SESSION['contactPersonInterest'][$i];
$temp = ',Medlemskort';
$_SESSION['contactPersonInterest'][$i] = $temp;
// or even save a $temp
$_SESSION['contactPersonInterest'][$i] = ',Medlemskort';
Hope this makes sense, good-luck
How do I go about setting a string as a literal variable in PHP? Basically I have an array like
$data['setting'] = "thevalue";
and I want to convert that 'setting' to $setting so that $setting becomes "thevalue".
Thanks for any help!
See PHP variable variables.
Your question isn't completely clear but maybe you want something like this:
//Takes an associative array and creates variables named after
//its keys
foreach ($data as $key => $value) {
$$key = $value;
}
extract() will take the keys of an array and turn them into variables with the corresponding value in the array.
${'setting'} = "thevalue";
It may be evil, but there is always eval.
$str = "setting";
$val = "thevalue";
eval("$" . $str . " = '" . $val . "'");
Consider my variable $result to be 01212.... Now if i add 1 to my variable i get the answer 1213,but i want it to be 01213.... My php code is
echo sprintf('%02u', ($result+1));
Edit:
Answers to this question works.. But what happens if my variable is $result to be 0121212
in future...
You can use %05u instead on %02u
echo sprintf('%05u', ($result+1));
EDIT:
To generalize it:
<?php
$result = "0121211";
$len = strlen($result+1) + 1;
printf("%0${len}d", ($result+1)); // print 0121212
?>
you could try:
str_pad($result, 5, "0", STR_PAD_LEFT);
Maybe I'm missing something here but it could be as simple as
$result = '0'. ($result+1);
edit:
$test = array('01212', '0121212', '012121212121212', '-01212');
foreach( $test as $result ) {
$result = '0'.($result+1);
echo $result, "\n";
}
prints
01213
0121213
012121212121213
0-1211
( you see, there are limitations ;-) )
Read here: http://php.net/manual/en/function.sprintf.php
in sprintf, you also has to specify length you want - so in your case, if you want any number to be shown 5chars long, you haveto write
echo sprintf ('%05d', $d); // 5 places taking decimal
or even better
printf ('%05d', $d);
Seems like you want to have a '0' in front of the number, so here would be the simplest :P
echo '0'. $result;
Or if you insist on using sprintf:
$newresult = $result + 1;
echo sprintf('%0'.(strlen(strval($newresult))+1).'u', $newresult);