Passing an Array as Arguments, not an Array, in PHP
http://www.php.net/manual/en/function.call-user-func-array.php
call_user_func_array('func',$myArgs);
Passing an Array as Arguments in php
Try something like:
call_user_func_array(array($stmt,'bind_param'),$request['params']);
Passing an Array as an Argument
The first answer for the question you link shows call_user_func_array()
, so with an object method do:
$res = call_user_func_array(array($geo, $method), $params);
Can we pass an array as parameter in any function in PHP?
You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop()
(and similar) all you like on it and won't affect anything outside.
function sendemail($id, $userid){
// ...
}
sendemail(array('a', 'b', 'c'), 10);
You can in fact only accept an array there by placing its type in the function's argument signature...
function sendemail(array $id, $userid){
// ...
}
You can also call the function with its arguments as an array...
call_user_func_array('sendemail', array('argument1', 'argument2'));
Passing an array as the parameter of a function in PHP
Try this. It will create a function that has a reference to your array. When you change the array you can call the product of the function, and it will recalculate the sum.
$array = ['English' => '12', 'Swedish' => '12'];
function arraySumCb(&$subject) {
return function () use (&$subject) {
return array_sum($subject);
};
}
$sum = arraySumCb($array);
echo $sum(); // 24
$array['Swedish'] = '15';
echo $sum(); // 27
$array['Swedish'] = '10';
echo $sum(); // 22
Edit: This is how I would do it.
$array = ['English' => '12', 'Swedish' => '12'];
class SumMarks {
private $_subject;
public function __construct(array &$subject = []) {
$this->_subject = &$subject;
}
public function __toString() {
return "" . array_sum($this->_subject);
}
}
$sum = new SumMarks($array);
echo $sum; // 24
$array['Swedish'] = '10';
echo $sum; // 22
Edit: Proper use of PHP anonymous functions
Pass array as argument list to function
You can use call_user_func_array
.
Use like:
$array = array(1, 2, 3);
call_user_func_array("myfunc", $array);
function myfunc($a, $b, $c) {
var_dump($a, $b, $c); // 1 2 3
}
$array = array(2, 3);
$first = 1;
call_user_func_array("myfunc", array_merge(arary($first), $array));
function myfunc($a, $b, $c) {
var_dump($a, $b, $c); // 1 2 3
}
In case of an object instance your callback should look like:
array($obj, "someFunc")
so:
call_user_func_array(array($obj, "someFunc"), $array);
Passing and using an array by reference in PHP
I just ran your code and got the expected output, so I'm not sure what's happening. If arr
wasn't an array, it wouldn't pass the typehint, so it's definitely an array.
It's possible that you're running PHP < 5.4
<?php
function foo(array &$arr) {
$arr[] = "error on this line";
$arr[] = "more";
$arr[] = "stuff";
}
$a = ["initial"];
foo($a);
var_dump($a);
/*
array(4) {
[0] =>
string(7) "initial"
[1] =>
string(18) "error on this line"
[2] =>
string(4) "more"
[3] =>
string(5) "stuff"
}
*/
For PHP < 5.4:
<?php
function foo(array &$arr) {
array_push($arr, "error on this line");
array_push($arr, "more");
array_push($arr, "stuff");
}
$a = array("initial");
foo($a);
var_dump($a);
/*
array(4) {
[0] =>
string(7) "initial"
[1] =>
string(18) "error on this line"
[2] =>
string(4) "more"
[3] =>
string(5) "stuff"
}
*/
Function is expecting arrays but I want to pass a variable
You can use call_user_func_array to accomplish that.
$arrs = array(
array('blue', 'green', 'red'),
array('apples', 'oranges', 'bananas')
);
call_user_func_array('cartesianProduct', $arrs);
The first argument is the function name you want to call and the second is an indexed array of parameters that will be passed to it.
How to pass an array as a parameter in a class constructor in PHP
Add:
$this->tableFields = $tableFields;
And:
$myObject = new GenerateCrud('users_test', 'id', ['field1', 'field2', 'field3', 'field4']);
Related Topics
Convert Timestamp to Readable Date/Time PHP
PHP: Sort an Array by the Length of Its Values
Resetting Array Pointer in Pdo Results
PHP Curl, Extract an Xml Response
Decode Gzipped Web Page Retrieved Via Curl in PHP
PHP Setcookie "Samesite=Strict"
Sort PHP Multidimensional Array by Sub-Value
How to Find the Dates Between Two Specified Date
Netbeans Shows "Waiting For Connection (Netbeans-Xdebug)"
How to Select First 10 Words of a Sentence
How to Replace Text Urls and Exclude Urls in HTML Tags
How to Best Store User Information and User Login and Password
Array_Splice() For Associative Arrays
Get Absolute Path of Initially Run Script