Programmatically write an associative array of functions - php

I have a lot of functions stored in an associative array like this :
$arr['my-title'] = function(){process(146,'My Title');};
$arr['un-cafe-chaud'] = function(){process(857,'Un café chaud');};
$arr['vpn'] = function(){process(932,'VPN');};
$arr['another-example'] = function(){process(464,'Another example');};
Currently I have to encode manually each key.
As the key name is function of the Title, I'd like to automate it.
function assign_keys($title,$id){
$u=str_replace(array(' ','é'),array('-','e'),strtolower($title));
$arr[$u] = function(){process($id,$title);};
}
But it doesn't work, as process function can't get $id and $title value.
Any help on how I could handle this would be highly appreciated ! Thank you.

First of all, you should pass $arr as argument to the function in order to be able to mutate it. Second, you should use use to make those two variables available in the anonymous function, like this:
function assign_keys($title,$id, &$arr){
$u=str_replace(array(' ','é'),array('-','e'),strtolower($title));
$arr[$u] = function() use ($id, $title){process($id,$title);};
}
Then use it like this:
$arr = [];
assign_keys('Some title', 123, $arr);
var_dump($arr);
This should print:
array(1) {
["some-title"]=>
object(Closure)#1 (1) {
["static"]=>
array(2) {
["id"]=>
int(123)
["title"]=>
string(10) "Some title"
}
}
}

You probably want a reference & to get the array outside of the function and use to get the variables into the closure:
function assign_keys($title, $id, &$arr){
$u = str_replace(array(' ','é'), array('-','e'), strtolower($title));
$arr[$u] = function() use($title, $id) { process($id, $title); };
}
assign_keys('My Title', 146, $arr);

Related

json_encode doesn't works on array_splice return but works if called directly

My goal is very simple, I want to parse the $GLOBALS variable in JSON (to log it). According to this Stackoverflow post, https://stackoverflow.com/a/23176085/1369579, I have to remove the recursive variable.
The following code works:
<?php
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
$json = json_encode(array_splice($global_array, $index, $index-1));
var_dump($json);
?>
It returns string(59) "{"GLOBALS":{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}}" (in http://sandbox.onlinephpfunctions.com)
But I have to use an intermediate variable to store the array_splice result. When I do this, I doesn't works:
<?php
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
$splice_result = array_splice($global_array, $index, $index-1);
var_dump(json_encode($splice_result));
?>
The result is bool(false) and the json_last_error_msg() returns Recursion detected.
What the difference between the two versions? I really don't understand. For me foo(bar()) is the exactly the same code than $bar = bar(); foo($bar)…
I just understood my problem, the call of array_splice remove the $GLOBALS variable… but I still don't understand why.
I tried to put my code into a function, because I thought my problem was to put the code directly in the global scope:
<?php
function globalWithoutGlobals() {
$global_array = $GLOBALS;
$index = array_search('GLOBALS',array_keys($global_array));
array_splice($global_array, $index, 1);
return $global_array;
}
var_dump(json_encode(globalWithoutGlobals()));
var_dump(json_encode(globalWithoutGlobals()));
/* returns
# First call: success,
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
# Second call : wtf ?!!
<br />
<b>Notice</b>: Undefined variable: GLOBALS in <b>[...][...]</b> on line <b>4</b><br />
*/
It's still very weird for me, the $global_array should be changed, not the $GLOBALS. To test this behaviour, I did the same thing on others arrays (with recursion too):
<?php
// Looks like $GLOBALS (with recursive element)
$globals_array = ["foo" => "bar"];
$globals_array['GLOBALS'] = &$globals_array;
$copy = $globals_array;
$index = array_search('GLOBALS', array_keys($copy));
array_splice($copy, $index, 1);
var_dump($globals_array);
var_dump($copy);
/* return:
array(2) {
["foo"]=>
string(3) "bar"
["GLOBALS"]=>
&array(2) {
["foo"]=>
string(3) "bar"
["GLOBALS"]=>
*RECURSION*
}
}
array(1) {
["foo"]=>
string(3) "bar"
}
*/
It returns the expected output, so why the behaviour is not the same with $GLOBALS ? 😩
To resolve my issue, I changed my approach and I stopped used array_splice, instead I just do a naive implementation with a foreach on the $GLOBALS and it works like expected:
<?php
function globalWithoutGlobals() {
$result = [];
foreach ($GLOBALS as $key => $value) {
if ($key !== 'GLOBALS') {
$result[$key] = $value;
}
}
return $result;
}
var_dump(json_encode(globalWithoutGlobals()));
var_dump(json_encode(globalWithoutGlobals()));
/* it returns:
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
string(47) "{"_GET":[],"_POST":[],"_COOKIE":[],"_FILES":[]}"
*/
If someone know why the behaviour is different between my two first examples above. I'm curious to understand 😊

Combine two foreach loops for better inefficiency?

I have the following code but I am wondering how I can make it more efficient.
if ($genres){
$arr = array();
foreach ($genres as $i) {
$arr[] = $i->name;
}
$genres_arr = $arr;
}
if ($themes){
$arr = array();
foreach ($themes as $i) {
$arr[] = $i->name;
}
$themes_arr = $arr;
}
var_dump($genres_arr);
var_dump($themes_arr);
I've tried putting them into an if statement but because they both always exists only the first one runs. I want to check to see if both exist and always run them both through a foreach loop. If only one exists I want only the one to run.
These are the array structures.
["genres"]=>
array(1) {
[0]=>
object(stdClass)#1579 (2) {
["id"]=>
int(25)
["name"]=>
string(26) "Hack and slash/Beat 'em up"
}
}
["themes"]=>
array(3) {
[0]=>
object(stdClass)#1576 (2) {
["id"]=>
int(1)
["name"]=>
string(6) "Action"
}
}
I want to have them as flattered as at the moment they are inside objects. I am then going to implode them into a list for WordPress use.
This code works but its repetitive and some help would be great!
I think you can use array column because it can read values from "A multi-dimensional array or an array of objects from which to pull a column of values from" like this:
if ($genres) {
$genres_arr = array_column($genres, 'name');
}
if ($themes) {
$themes_arr = array_column($themes, 'name');
}
var_dump($genres_arr);
var_dump($themes_arr);
In this way the easiest simplification would be to introduce a new function, which builds the array.
function getNames($arr) {
if (!is_array($arra)) return false;
return array_map(function($item) {
return $item->name;
}, $arr);
}
$themes_arr = getNames($themes);
$genre_arr = getNames($genres);
This is how I would combine them
$sets = [];
if ($genres){
$sets['genres'] = $genres;
}
if ($themes){
$sets['themes'] = $themes;
}
$arr = array();
foreach( $sets as $type => $data ){
foreach ($genres as $i) {
$arr[$type][] = $i->name;
}
}
claudio's answer is perfect for this situation, but in the more general case you can also define a mapping function, which you then use with array_map for both sets of objects, e.g.
$mapper = function ($item) { return $item->name; };
$genres_arr = array_map($mapper, $genres);
$themes_arr = array_map($mapper, $themes);
This has the advantage of being able to run more complex logic (using a getter function instead of direct property access, etc), if you need it in future.
Ideally, both objects would implement a common interface, so that it was clear exactly what kinds of objects the mapping function was designed for.

Can I set the keys of an array using array functions like array_map

I really like the functional programming style of using array map to create an array of objects from another array of objects.
$newObjects = array_map(
function($oldObject) {
return new NewObject($oldObject);
},
$oldObjects
);
Which all works fine but I would really like to be able to set the indices of the array so that they are the ids of the original objects for easier search and retrieval from the array but I cannot think how to do it other then which is not as elegant.
$newObjects = array();
foreach ($oldObjects as $oldObject) {
$newObjects[$oldObject->getId()] = new NewObject($oldObject);
}
Is there a way I can do this?
That is - array_reduce() is exactly what you need:
class Bar
{
protected $id;
public function __construct($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
}
class Foo
{
protected $bar;
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
}
$oldObjects = [new Bar('x'), new Bar('y'), new Bar('z')];
$newObjects = array_reduce($oldObjects, function($current, Bar $obj) {
$current[$obj->getId()] = new Foo($obj);
return $current;
}, []);
This will do all in-place without having to spend memory on additional arrays like for array_combine()
However, I would suggest to use such constructs when they're necessary. Using this just because it "looks better" might be not a good idea - as plain loops are in most cases just more readable.
What if you use array_walk and a temporary array with your new indices.
$array = ['A', 'B', 'C', 'D'];
$reIndexedTemp = [];
array_walk(
$array,
function ($item, $key) use (&$reIndexedTemp) {
// here you can have your logic to assemble your new index
$reIndexedTemp[$key + 100] = $item;
}
);
//$array = $reIndexedTemp;
var_dump($array, $reIndexedTemp);
output (without the commented line) :
array(4) {
[0] =>
string(1) "A"
[1] =>
string(1) "B"
[2] =>
string(1) "C"
[3] =>
string(1) "D"
}
array(4) {
[100] =>
string(1) "A"
[101] =>
string(1) "B"
[102] =>
string(1) "C"
[103] =>
string(1) "D"
}
I think a foreach is probably the most readable solution in this case, but you can use array_map() with array_combine() to achieve what you want. Something like:
// empty array to store the old object ids
$ids = [];
// map over old objects, inheriting $id
// from parent scope by reference
$objs = array_map(function($oldObject) use (&$ids) {
$ids[] = $oldObject->getId();
return new NewObject($oldObject);
}, $oldObjects);
// combine id and object arrays
$newObjects = array_combine($ids, $objs);
Hope this helps :)
Looking around - Looking for array_map equivalent to work on keys in associative arrays
Suggests it might work using array_combine
So I guess it would be
$newObjects = array_combine(
array_map(
function($oldObject) {
return $oldObject->getId();
},
$oldObjects
),
array_map(
function($oldObject) {
return new NewObject($oldObject);
},
$oldObjects
)
);
Hmm probably the best, just this side of overblown but definately a lot more complex than the foreach

How to add (any type )value to an array(by specified index) in php language

As title, I did it like below:
$array=array(0,1,2,3);
$result=array();
function makeArray($array,$result,$value){
$str='$result';
for ($i=0;$i<count($array);$i++){
$str.='['.$i.']';
}
$str.='="'.$value.'";';
eval($str);
return $result;
}
It can realize result when param $result is an empty array,but It report an error when $result is an array.
Error like :
Cannot use a scalar value as an array.
Anyways can realize it?
Thanks first!
Use pass by reference, not eval:
function makeArray($indexes, &$result, $value) {
$here =& $result;
foreach ($indexes as $i) {
if (!(isset($here[$i]) && is_array($here[$i]))) {
$here[$i] = array();
}
$here =& $here[$i];
}
$here = $value;
}
$array=array(0,1,2,3);
$result=array();
makeArray($array, $result, 3);
var_dump($result);
Output:
array(1) {
[0]=>
array(1) {
[1]=>
array(1) {
[2]=>
array(1) {
[3]=>
int(3)
}
}
}
}
Putting & before a function parameter means it will be passed by reference, so modifications to the variable inside the function will affect the original variable that was passed. And using =& in an assignment assigns a reference, so the target variable is an alias for the source.

How to get function's parameters names in PHP?

I'm looking for a sort of reversed func_get_args(). I would like to find out how the parameters were named when function was defined. The reason for this is I don't want to repeat myself when using setting variables passed as arguments through a method:
public function myFunction($paramJohn, $paramJoe, MyObject $paramMyObject)
{
$this->paramJohn = $paramJohn;
$this->paramJoe = $paramJoe;
$this->paramMyObject = $paramMyObject;
}
Ideally I could do something like:
foreach (func_get_params() as $param)
$this->${$param} = ${$param};
}
Is this an overkill, is it a plain stupid idea, or is there a much better way to make this happen?
You could use Reflection:
$ref = new ReflectionFunction('myFunction');
foreach( $ref->getParameters() as $param) {
echo $param->name;
}
Since you're using this in a class, you can use ReflectionMethod instead of ReflectionFunction:
$ref = new ReflectionMethod('ClassName', 'myFunction');
Here is a working example:
class ClassName {
public function myFunction($paramJohn, $paramJoe, $paramMyObject)
{
$ref = new ReflectionMethod($this, 'myFunction');
foreach( $ref->getParameters() as $param) {
$name = $param->name;
$this->$name = $$name;
}
}
}
$o = new ClassName;
$o->myFunction('John', 'Joe', new stdClass);
var_dump( $o);
Where the above var_dump() prints:
object(ClassName)#1 (3) {
["paramJohn"]=>
string(4) "John"
["paramJoe"]=>
string(3) "Joe"
["paramMyObject"]=>
object(stdClass)#2 (0) {
}
}
Code snippet that creates an array containing parameter names as keys and parameter values as corresponding values:
$ref = new ReflectionFunction(__FUNCTION__);
$functionParameters = [];
foreach($ref->getParameters() as $key => $currentParameter) {
$functionParameters[$currentParameter->getName()] = func_get_arg($key);
}
While it's not impossible to do it, it's usually better to use another method. Here is a link to a similar question on SO :
How to get a variable name as a string in PHP?
What you could do is pass all your parameters inside of an object, instead of passing them one by one. I'm assuming you are doing this in relation to databases, you might want to read about ORMs.
get_defined_vars will give you the parameter names and their values, so you can do
$params = get_defined_vars();
foreach ($params as $var=>$val) {
$this->${var} = $val;
}

Categories