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);
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
Hello to All i get a result from a server like this.
Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0
How Can i convert it into a arary in php.
Thanks for the Help.
$query = 'Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0';
parse_str($query, $output);
echo '<pre>';
print_r($output);
echo '</pre>';
/*
Array
(
[Id] => 6528537
[CCode] => 250
[Fild1] =>
[Fild2] =>
[Fild3] =>
[HeshASM] => 0
)
*/
You could try to construct the array yourself:
$string = "Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0";
$explode = explode("&", $string);
$array = [];
foreach ($array as $key => $value) {
$inner_explode = explode("=", $value);
$array[$inner_explode[0]] = $inner_explode[1];
}
You would explode the string using "&" as separator, that will give you an array with the different parameters like array('id=6528537','ccode=250'...)
Then you will iterate trough that array, and explode again, this time using "=" as separator, this will give you the different parts of the different parameters, like: array('Id','6528537').
Knowing that in the position 0 of that array you will have the key, and in the position 1 the value, you simply add to your array the values in each iteration with $array[$inner_explode[0]] = $inner_explode[1];
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
Right now i am working on a simple PHP script.
I have four variables:
$test_a = 25;
$test_b = 24;
$test_c = 22;
$test_d = 35;
I want to display not the numbers from the variables. I need to extract only the two variables with the highest number.
So i need result something like this:
<?PHP echo "The two highest variables are: $test_a and $test_d";?>
So how i can extract only the two highest variables?
Add the values to array, sort it in descending order, then take two first elements:
$a = array($test_a, $test_b, $test_c, $test_d);
arsort($a);
echo 'Two highest values:'.$a[0].' and '.$a[1];
Put your values in an array: $array = array(25, 24, 22, 35); and get the first highest value using php max function:
$highest[] = max($array); //store it in an array so you can compare using array_diff
Remove that value from your array w/ array_diff:
$array = array_diff($array, $highest); //remove highest from original array
And then repeat finding highest with max:
$second_highest = max($array);
echo "The two highest variables are: $highest[0] and $second_highest";
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What's the difference between $data["xxxx"] AND $xxxx ?
I have seen $data["xxxx"] in some page, then i want to know What is difference between $data["xxxx"] and $xxxx (that I usually use)?
It's very simple
$data["xxxx"] is an element of array and $xxxx is normal variable.
You can create array using
$data = array();
$data['xxxx'] = 'abc';
$data['yyyy'] = '222';
and then output it's element using
echo $data['xxxx'];
echo $data['yyyy'];
You should definitely look at Types in PHP
$xxxx is a simple variable
whereas
$data["xxxx"] is a array element where $data is name of array and xxxx is a key in that array
The answer: It depends.
If there was code linking these two variables together, they could potentially be the exact same thing.
$data['xxxx'] = &$xxxx;
$xxxx = &$data['xxxx'];
Now, there's no difference between these two, setting either variable would be the same thing.
<?php
$data = array();
$data['xxxx'] = &$xxxx;
$xxxx = &$data['xxxx'];
$xxxx = 2;
$data['xxxx'] = 4;
echo $xxxx;
echo $data['xxxx'];
Output: 44
In this case, there is no difference between $data['xxxx'] and $xxxx.
If the two variables were not linked, the difference would be, of course, they both have different addresses.
In conclusion, I can say however, that $data["xxxx"] is an element of $data, which is an array.
BUT, I cannot say anything about the $xxxx, or the type of values that either variable holds.
$xxxx is a variable like we have in any languate in which you can store value (string, integer etc):
For example
$xxxx = 100;
$xxxx = 'string';
But $data['xxxx'] is an associative array. In a simple array you get values on basis of there index position for example
$data[0] = "Jona";
$data[1] = "Hex";
echo $data[1]; //will out put 'Hex'
But in associative array like you mentioned in your question, values at positions are recognized by there name like 'xxxx'. For example:
$data['xxxx'] = "This string is saved at xxxx in array $data";
$data['yyyy'] = "This string is saved at yyyy in array $data";
//you can use it like:
echo $data['xxxx'];
Like "xxxx" you can give any name to array elements; for more details see arrays in PHP
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 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
My code:
<?php
$string = "hello world";
echo $string[1]; //output is 'e'
echo end($string); // I've got a warning error
?>
How can I exchange my string variable to an array format?
I think if I exchange it the problem will solve.
$string[n] is a specific notation to access the nth offset of a string. This does not mean the string is an array, it's just special syntactic sugar. If you want the last offset of the string, use substr($string, -1).
There is a predefined function that do this. It's str_split(). Here http://it.php.net/manual/en/function.str-split.php you find the doc
Just do this:
echo substr($string, -1);
Because a string is not natively an array, functions like end and array_sort don't work.
To physically cast the string, use this:
function CastStringToArray($string)
{
$ret = array();
$length = strlen($string);
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
return $ret;
}