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
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 4 years ago.
Improve this question
how can i using php divide every single value in an array on the next value to return a number like a have an array
i want a function that divide 1/2/3/4/5 and it should return this value (0.0083333333333333)
i have tried this
<?php
function vo(){
$newArray=array(1,2,3,4,5);
$resulte=1;
foreach($newArray as $value){
$resulte=$value/$resulte;
}}
vo();
?>
output for this code, resulte=1.875
i think im bad at math not sure tho
$your_array = [1,2,3,4,5];
$result = $your_array[0]; //result defaulted to first element in array;
for($i=1; $i<sizeof($your_array); $i++){ //loop starts from 2nd //element in array
$result /= $your_array[$i];
}
code explanation: set initial value of $result to first element of array, as this is the first thing that you said you want to divide;
start looping through your array from 2nd element, and set the value of $result to it's current value divided by the current element in the loop (this is why /= is used)
the final value of $result, becomes the accumulated result of the division of each element of the array divided by the element next to it.
if you output $result, you will get 0.0083333333333333
You got your division backwards. You want to divide by the next number in the list, therefore your $resulte = $value / $resulte should be $resulte = $resulte / $value. Also remember to add a print($resulte) after the loop.
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 7 years ago.
Improve this question
I have arraylist like : [1, 2, 3, 4, 5];
How can I get each of these values in php?
Is there any function ? I tried array() but it did not return anything.I really appreciate any help.
Let say your $array = [1,2,3,4,5];
To get the values:
foreach($array as $values):
echo $values.'<br/>';
endforeach;
Or if you want based on elements, you can get by this way:
//To get value 1
echo $array[0];
// 0 being here the index of your array
// so echo $array[0] will output 1 since it's the first key of your array
If you have an array like this
$numbers = array(1,2,3,4,5);
with five values (i.e indexed 0 - 4) you can print out all values in the array using the print_r() function like this:
print_r($numbers);
Or you can loop through every element in the array with any PHP looping statement like FOR STATEMENTS, FOREACH STATEMENTS, WHILE STATEMENTS..
e.g
foreach($numbers as $num){
echo $num;
echo '<br/>';
}
You can also get a single value from the array using the index for example my array has 5 values (indexed from 0 - 4) if i want to print out the number '3' i would write
echo $numbers[2];
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 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
I have string like this : hello world new foo.
I need to have array like this:
[
`helloworld new foo`,
`hello worldnew foo`,
`hello world newfoo`,
`helloworldnew foo`,
`hello worldnewfoo`,
]
order doesn't matter but i need to have all case that space will be remove in current string.
You can use recursion to enumerate all the posibilities:
<?php
function combine($words, $acc=array(), $res=array()){
if(count($words)===0){
$res[] = join("", $acc);
return $res;
}
if(count($words)===1){
$res[] = join("", $acc).$words[0];
return $res;
}
$w1 = array_shift($words);
$res = combine($words, array_merge($acc, array("$w1")), $res);
$res = combine($words, array_merge($acc, array("$w1 ")), $res);
return $res;
}
var_dump( combine( explode(' ', 'hello world new foo') ) );
Another possible solution is to represent the N spaces betweeen your words, as bits in a binary number that can be on or off, and then count from 0 to 2^N-1, but I think that in PHP that will be more complex.
NB: the above recursive solution, returns all possible combinations ... so if you have an input array with 4 words, one of the results will have all 4 words joined.
From what i understood you need all posible combinations with the words given. so:
posible combinations = amountofwords*amountofspaces.
start iteration for amout of posible cominations. -> for(i=0;i<=(words*spaces);i++)
have words in array and spaces found in String so $WordArray = $string.split(" ") and $spaces = substr_count(" ")
start iteration for posible word combinations. for(j=0;j<=words;j++)
start iteration for amount of spaces. for(k=0;k<=spaces;++)
combine all.
but keep in mind that PERMUTATIONS and COMBINATIONS for computer science is what you need to learn first, so the answer above has a lot of sence.
here is a link to get you started.
http://cpsc.ualr.edu/srini/DM/chapters/review3.4.html