how to expand an array elements as separate parameters to a function - php

I have an array of colors having dynamic values which depends on database. now these values are required in a function which takes values only like this function('para1','para2','para3','para4')
where param1 to param4 are color values in an array.
Problem is how can i parse these values to that function in the above stated format.Only a programminng logic required.Language is php.
Suppose dynamic array is color[]=('red','maroon','blue','green');
and these value should be passed to this function like :setLineColor('red','maroon','blue','green');
I m using this function for creating graphs.(Lib using PHP_graphlib: link: http://www.ebrueggeman.com/phpgraphlib/documentation.php)
Any other suggested library is welcomed.Plz provide a simple example with it.

Since PHP 5.6 you can use argument unpacking with the triple-dot-operator:
setLineColor(...$colors);

You can use the function call_user_func_array.
<?php
$colors = array('red','maroon','blue','green');
call_user_func_array('setLineColor', $colors);
?>
If you want to call the method of an object, you can use this instead:
<?php
$graph = new ...
$colors = array('red','maroon','blue','green');
call_user_func_array(array($graph, 'setLineColor'), $colors);
?>

function($color[0], $color[1], $color[2], $color[3])

Related

How to format sql's return value in php?

In this case, I can get return value from sql.
$estateGroup =
Estate_base::select('id')->whereIn('name',$request->estateName)->get();
but $estateGroup will be like this: [{id:1},{id:2}]
I want to change [{id:1},{id:2}] to [1,2]
so I doing like following:
$estateGroup =
Estate_base::select('id')->whereIn('name',$request->estateName)->get();
$idGroup=[];
foreach ($estateGroup as $estate) {
array_push($idGroup, $estate->id);
}
Is there any way that I can format value more easier?
You can use array_map. By the way it will do same thing as you are doing but in less code.
$idGroup = array_map(function($estate) { return $estate->id;}, $estateGroup);
In PHP 7 you can use array_column() function with list of objects:
$idGroup = array_column($estateGroup, 'id');
It's shortest (and probably fastest) way to extract values of same property from multiple objects. The note from documentation:
7.0.0: Added the ability for the input parameter to be an array of objects.

codeigniter two dimensional superglobal _post

As you know We can use something like $_POST['name'][1]
How can we use it in $this->input->post('name') ?
Codeigniter post function isn't two dimensional. I think we can not pass second argument to post function.
Thank you.
It is not supported in Php(<v5.4) just to $this->input->post('name')[0], so there are several ways to get the values:
-use list function:
list($day) = $this->input->post("name");
-use loop, for exampleforeach:
$foreach($this->input->post("name") as $nameData){
echo $nameData;
}
- just to set array into variable and take value:
$name = $this->input->post("name");
echo $name[0];

pass arguments to a function php

Is it possible to convert an array to a list of elements without using list?
this works well
list($arg_a,$arg_b) = array($foo,$bar);
myfunction($arg_a,$arg_b);
but I'm looking for something similar to that:
$array = array($foo,$bar);
myfunction(php_builtin_function(array($foo,$bar)));
obviusly I can't edit that function!
function myfunction($param_a,$param_b){
...
}
As mentioned in comments, here's what you need:
call_user_func_array('myfunction', php_builtin_function(array($foo,bar)));
Docs
That said, it would be more readable to use list:
$result = php_builtin_function(array($foo,$bar));
list($arg_a, $arg_b) = $result;
myfunction($arg_a, $arg_b);

PHP populate single array with no keys

I have an indexed array like this:
$indexed = array(0=>2,1=>7,3=>9)
But i need a single array, without indexes, like this:
$notIndexed = array(2,7,9)
Zend_Form does not accept $indexed as parameter for the function populate() for multi checkboxes but works fine with $notIndexed
How can i dynamically transform $indexed to $notIndexed
Thanx for answers
$notIndexed = array_values($indexed);
Are you serious? Use, array_values($indexed).
http://php.net/manual/en/function.array-values.php
$notindex = array_values($indexed)

Combined 2 array data

I want to do something like bellow using php:
$turl=array(trim($params->get('c2')));
$tname=array(trim($params->get('cn2')));
and want to display each $turl with each $tname.
I tried like this:
$result=array_combine($turl,$tname);
print_r($result);
but given result as:
Array ( [http://184.107.144.218:8282/,http://184.107.144.218:8082/] => ABC Radio,AHH Radio )
But I want like this:
Array ( [http://184.107.144.218:8282/=> ABC Radio,
http://184.107.144.218:8082/=> AHH Radio )
Thanks in advance
maybe you want to use a code similar to this:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_combine($turl,$tname);
print_r($result);
The error seems to be into the $turl and $tname arrays creation as array combine should work as intended
Obviously I would add several checks, as, for example, Have the two arrays the same size?
Addendum
In your comment you give me a specimen of what $params->get returns if called with parameters 'c2' and 'cn2'.
$params->get('c2')="hoicoimasti.com,google.com"
$params->get('cn2')="hoicoi,google".
The example code I gave you do what required, or at leas what I was thinking you are trying to obtain. A different code with a different result is:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_map(function($x,$y){return $x.','.$y;},$turl,$tname);
print_r($result);
or, if you want to obtains a single string:
$result=join(',',array_map(function($x,$y){return $x.'=>'.$y;},$turl,$tname));
print_r($result);
You can obtain any desired result modifying one of the previous example.
To encase the results in an option you need a slight variation of the array_map user function:
$result=join("\n",array_map(
function($x,$y){
return "<option>$x=>$y</option>";
},
$turl,
$tname
));
print_r($result);
PHP < 5.3 version
Put this function declaration somewhere in the global scope
function formatOption($x,$y){
return "<option>$x=>$y</option>";
};
and then your code will become:
$result=join("\n",array_map( 'formatOption', $turl, $tname));
print_r($result);
If possible I won't clutter the global namespace with function like formatOption,
but when anonymous function are not available
Reference:
array_map
join

Categories