How to get unlimited argument without parameter limit [closed] - 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 6 years ago.
Improve this question
function test(){
}
test(58,1,"world","hello");
How can Use this argument test() function parameter.

Use func_get_args()
<?php
function test(){
print_r(func_get_args());
}
test(58,1,"world","hello");
Output:
Array
(
[0] => 58
[1] => 1
[2] => world
[3] => hello
)

Related

Php function with return statement [closed]

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.

extract data from string and between characters in php [closed]

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 5 years ago.
Improve this question
I want extract data from string
Example, i have this :
n97(nok)_100(ok)_n41(145)_23(ok)
I want get the 2 string beginning by "n" :
n97(nok) and n41(145)
And after for this 2 string i need get in variable
97 => nok and 41 => 145
can you help me ?
Thanks you
Use regex /n(\d+)\((.+?)\)/ to find patern and array_combine to get result
$str = 'n97(nok)_100(ok)_n41(145)_23(ok)';
if(preg_match_all('/n(\d+)\((.+?)\)/', $str, $m)) {
$res = array_combine($m[1], $m[2]);
}
print_r($res);
demo

Defining a constant array in Laravel or PHP? [closed]

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
I want to define a constant of array. How can I do that?
define("CREDIT_REPORT_FAIL", "FAIL");
In Laravel you can define an array in a config file:
'some_array' => [
'var' => 'value',
'another_var' => 'value'
],
And then you config() global helper to get the array:
config('config_file.some_array')
Please Try This Code
Define Code
define('TEST', serialize(array(1, 5, 10, 15)));
Use
$data = unserialize(TEST);

Convert such this array into variables [closed]

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 such this array in PHP, How can i convert the one and two and three into variables for each one (i have just pasted first array of the items)?
Array
(
[encoding] => utf-8
[title] => arrays
[link] => arrays.com
[items] => Array
(
[0] => Array
(
[one] => this is one
[two] => arrays.com
[three] => This IS my array here
)
)
[items_count] => 100
[cached] => 0
)
You can use PHP's extract() function:
extract($array['items'][0]);
a realy simple way is the php function extract. It provides you the keys of an array as variables in your current scope.
In your case just write extract($array['items'][0]);
Now you can access the variables $one, $two and $three.
you can do this
list($one,$two,$tree) = $Your_Array['items'][0];

PHP string assign to array variable [closed]

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 )

Categories