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 have an array like
$remarks = array('Poor','Fair','Good');
I want to access the array index by function call in it.
echo $remarks[myindex($id)];
where myindex() is a function which returns some number value from database.
How can I access the array index of my array at run time
As per you say..
myindex is a function which returns a value between 0-2 then I want to
display array value of that index
<?php
$remarks = array('Poor','Fair','Good');
function myIndex()
{
return rand(0,2);
}
echo $remarks[myIndex()]; //"prints" either Poor , Fair or Good randomly..
The myIndex() function returns a random value between 0,1 or 2 , so that is passed as the index value to your array and it prints the values either Poor , Fair or Good.
it may be
function myindex($id){
return $id % 3;
}
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 2 years ago.
Improve this question
I have $GET function that by default send out a request to get 30 items back
$transactions = $MK->getTransactions($_GET)
It will go to the header as following
transactions?&page=1&per_page=30
And from the API provider module i found this line
if (!empty($params['per_page'])) {
$request_params['per_page'] = (int) $params['per_page'];
}
SO my question is how to add the param to the $_GET
Right now when it goes out just as $transactions = $MK->getTransactions($_GET)
the request itself will go out as /transactions?&page=1&per_page=30
But i need to change the per_page value to smth else.
So you have smth like:
$transactions = $MK->getTransactions($_GET)
Whiich leads you to the following path:
/transactions?&page=1&per_page=30
But you want to change the value of one parameter in $_GET?
First, from you example, if you'd print $_GET, you would see:
Array
(
[page] => 1
[per_page] => 30
)
So do smth like:
$parameters = $_GET;
$parameters["per_page"] = 123;
And than:
$transactions = $MK->getTransactions($parameters);
which will result in changed number per page.
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
Can somebody explain me this code? I don't understand why it outputs 21.
<?php
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
?>
It will echo 21. I have no idea how it got this result.
The function is recursive, it calls itself until it gets to 0, then adds all the previously returned values (6,5,4,3,2,1).
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
So on loop one it gets 6 then 6-1 = 5 so math gets called again with 5 this time and so on. Take a look at http://sandbox.onlinephpfunctions.com/code/e228f3b696c5058efee03fa978a09179c1f2ffbb.
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 6 years ago.
Improve this question
I'm trying to check if a filename of an image contains "cover". Somehow this stopped working for me (Pretty sure it worked already). I copied the part of my function not working.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
This does not work for me:
print_r(array_search($cov, $name)); //Returns empty String
print_r($name[array_search($cov, $name)]); //Returns first element of the name Array
Also I added a test Strings to make sure this is not result the searched string is the same as the search value.
print_r($name[3]===$cov[1]); //Returns true(1)
Can anyone help? Why does this simple script not work?
I also tried using in_array() but this is not working either.
The array_search() function search an array for a value and returns the key
array_search(key_value,array)
Loop your $cov array and get one key at a time and check with $name array
foreach($cov as $i => $cov_s){
if(in_array($cov_s, $name)){
return $name[array_search($cov_s, $name)];
}
}
return $name[0];
Try this code.
$name=array("_IMG8555.jpg", "_IMG7769.jpg", "_IMG8458.jpg", "Cover.jpg", "_IMG7184.jpg");
$cov=array("Cover.png","Cover.jpg","Cover.jpeg", "cover.png","cover.jpg","cover.jpeg");
foreach($cov as $c){
if(array_search($c,$name)){
//Do your success function
return true;
}
else
return false;
}
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 find how to get number of variables to be passed in function.
say suppose I have function
public function abc($id,$name,$email){
....
}
Now there is another function xyz in want to know number of variables that are need to be passed in this function.
Thanks in advance.
Now there is another function xyz in want to know number of variables that are need to be passed in this function.
Use this:
$rfunc = new ReflectionFunction('xyz');
echo $rfunc->getNumberOfRequiredParameters();
But I do not know for what this should be useful...
if you did not know how many number of variables that are need to be passed in calling function, then you can make use default value of parameter concept.
For ex:
// Function call-1
abc($a,$b,$c);
// Function call -2
abc($a,$b,$c,$d);
// Function defination goes here
function abc($a,$b,$c,$d=0)
{
/* if you did not pass $d value , $d gets assigned automatically to 0
as default value. So in this way, you should not be worried about how
many parameters you should pass */
}
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 9 years ago.
Improve this question
I have written some php code, trying what is to assign a string (tag_of_organization) to a variable and then latter to assign it to array newvar. As i am totally new to php and search a lot for it but cannot find anything, so i am asking it here how to do this. here it is
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
Try with:
$newvar[$organ] = $organ;
You are already correct. put print_r to test print your array
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )
Update
you want dynamic result
$organ='tag_of_organization';
$newvar = array();
$newvar[$organ] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )