use strings to access (potentially large) multidimensional arrays - php

I am having trouble figuring out a way to simply parse a string input and find the correct location within a multidimensional array.
I am hoping for one or two lines to do this, as the solutions I have seen rely on long (10-20 line) loops.
Given the following code (note that the nesting could, in theory, be of any arbitrary depth):
function get($string)
{
$vars = array(
'one' => array(
'one-one' => "hello",
'one-two' => "goodbye"
),
'two' => array(
'two-one' => "foo",
'two-two' => "bar"
)
);
return $vars[$string]; //this syntax isn't required, just here to give an idea
}
get("two['two-two']"); //desired output: "bar". Actual output: null
Is there a simple use of built-in functions or something else easy that would recreate my desired output?

Considering $vars being your variables you would like to get one['one-one'] or two['two-two']['more'] from (Demo):
$vars = function($str) use ($vars)
{
$c = function($v, $w) {return $w ? $v[$w] : $v;};
return array_reduce(preg_split('~\[\'|\'\]~', $str), $c, $vars);
};
echo $vars("one['one-one']"); # hello
echo $vars("two['two-two']['more']"); # tea-time!
This is lexing the string into key tokens and then traverse the $vars array on the keyed values while the $vars array has been turned into a function.
Older Stuff:
Overload the array with a function that just eval's:
$vars = array(
'one' => array(
'one-one' => "hello",
'one-two' => "goodbye"
),
'two' => array(
'two-one' => "foo",
'two-two' => "bar"
)
);
$vars = function($str) use ($vars)
{
return eval('return $vars'.$str.';');
};
echo $vars("['one']['one-two']"); # goodbye
If you're not a fan of eval, change the implementation:
$vars = function($str) use ($vars)
{
$r = preg_match_all('~\[\'([a-z-]+)\']~', $str, $keys);
$var = $vars;
foreach($keys[1] as $key)
$var = $var[$key];
return $var;
};
echo $vars("['one']['one-two']"); # goodbye

How about
$vars = array(
'one' => array(
'one-one' => "hello",
'one-two' => "goodbye"
),
'two' => array(
'two-one' => "foo",
'two-two' => "bar"
)
);
function get( $string, $vars )
{
$keys = explode( '][', substr( $string, 1, -1 ) );
foreach( $keys as $key ) {
$vars = $vars[$key];
}
return $vars;
}
echo get( '[two][two-one]', $vars );

For one, you've not got a $var in your get() function. $var was defined outside the function, and PHP scoping rules do not make "higher" vars visible in lower scopes unless explictly made global in the lower scope:
function get($string) {
global $vars;
eval('$x = $vars' . $string);
return $x;
}
get("['two']['two-two']");
might work, but this isn't tested, and using eval is almost always a very bad idea.

Kohana has a nice Config class which alows something like this:
echo Config::get("two.two-two");
You can check it out here: http://kohanaframework.org/3.1/guide/api/Config

Related

why the variable value changed to array into function scope ? PHP

why the variable $ replaced changed to array in a function ?
foreach ($rowset as $row)
{
$replaced = str_replace('$uid',$this->bbcode_uid,$row['first_pass_replace']);
echo $replaced; // this is a test of the variable error and it's ok
$this->bbcodes[$row['bbcode_tag']] = array(
'bbcode_id' => (int) $row['bbcode_id'],
'regexp' => array($row['first_pass_match'] => function($replaced){
return $replaced; //this is not working
}
)
);
}
I honestly have no idea what you are trying to accomplish but to make it "work" you need:
...
'regexp' => array(
$row['first_pass_match'] => function() use($replaced) {
return $replaced;
}
)
...
You use use() to achieve closure and bring an external variable into the function without explicitly passing it in as a parameter.

Shortcut function to populate associative array

I'm extracting data and then creating an array like:
$foo = $_POST[ 'foo' ];
$bar = $_POST[ 'bar' ];
$val = array( 'foo' => $foo, 'bar' => $bar );
Is it possible to have a shortcut function?
Something like the following:
$val = array( someFunction( 'foo' ), someFunction( 'bar' ) );
Where:
function someFunction( $name ) {
return( "$name" => $_POST[ $name ] ); // Does not work!
}
What you're trying to do with the Associative Array definition is impossible with the current PHP syntax:
array( someFunction( 'foo' ), someFunction( 'bar' ) );
However, this is the closest thing I can think of:
$_POST = array( 'foo' => 'FOO', 'bar' => 'BAR' );
$var = array();
someFunction( $var, 'foo' );
someFunction( $var, 'bar' );
function someFunction( &$output, $name ) {
$output[$name] = $_POST[$name];
}
Not very sexy and might be confusing also, but will do the job.
Update
Is it what you're looking for -- it is even closer:
$var = ( list($foo, $bar) = $_POST );
// OR
$var = (array) ( list($foo, $bar) = $_POST );
Obviously it's highly depended on the array structure, so it might not be a good idea to use it for $_POST for example.
You're thinking in the right direction, but not nearly big enough!
You want to define an input schema like:
$input = $_POST;
$input_schema = array(
"foo" => "string",
"bar" => "string"
);
validate_input($input, $schema);
From then on you can safely use $input in accordance with your schema.

PHP - Losing the array references out of the function (local to global)

I made a small function to parse and get elements from a multidimensional array by a string written in a Unix-like path syntax.
function array_get($path, &$array) {
$keys = preg_split('/[\/\\\]+/', $path, null, PREG_SPLIT_NO_EMPTY);
$current = trim(array_shift($keys));
if (is_array($array) && array_key_exists($current, $array)) {
$path = implode("/", $keys);
if (empty($path)) {
// (Place the code here, see below)
return $array[$current];
}
return array_get($path, $array[$current]);
}
return false;
}
So if I got a simple array like this
$arr = array(
"A" => array(
"X" => array(),
"Y" => array(),
"Z" => array()
),
"B" => array(
"X" => array(),
"Y" => array(),
"Z" => array()
),
"C" => array(
"X" => array(),
"Y" => array(),
"Z" => array()
)
);
and I wish to fill it within some entries like these
$arr['A']['Z'][] = "foo";
$arr['A']['Z'][] = "bar";
I would do the same job using the following statements:
$var = array_get("A/Z", $arr);
$var[] = "foo";
$var[] = "bar";
But something went wrong.
If you try to run the code you will notice that going out of the local scope the references to the passed array will be lost.
If you wish to run a test, you can replace the placeholder comment line inside the function with these two code lines:
$array[$current][] = "foo";
$array[$current][] = "bar";
then you will see that the function would perform actually its own job.
Is there a way to maintain the references in output?
From the documentation, you can specify you want to return a reference by using the & character before the function name AND the function call.
<?php
function &foo(&$arr) {
return $arr[0];
}
$a = [[]];
$b = &foo($a);
$b[0] = 'bar';
print_r($a); /* outputs [ [ 'bar' ] ] */
You can return references.
But I find your approach really cumbersome, and it will lead to misbehaviours / maintainability / readability issues very soon.

Check for key-value pair in multidimensional array

I have the follow array:
Array
(
[0] => Array
(
[type] => foo
)
[1] => Array
(
[type] => bar
)
[2] => Array
(
[type] => bar
)
)
and need to know if exists one or more type which value is bar, without do this:
foreach ($arrayOfTypes as $type) {
if ($type['type'] == 'bar')
{
// Stuff here
}
}
(Only for learning purposes)
I'd go with array_filter();
$filteredArray = array_filter($stuff, function($item){
return $item['type'] == 'bar';
});
if( count($filteredArray) > 0 ){
echo 'There was a bar item in array.';
}else{
echo 'No luck sorry!';
}
Use in_array: http://se.php.net/manual/en/function.in-array.php
Combine it with array_map to flatten what you have. Like so:
$new_array = array_map( function( $arr ) {
return $arr['type'];
}, $array );
in_array( 'bar', $new_array );
Honestly a foreach loop or moonwave99's array-filter answer is probably going to be your best bet, but if what you're looking is the shortest code possible and creativity that will make most programmers gag, you could try serialize-ing the array and using string-searching functions:
serialize(array(
0=>array(
'type' => 'foo'
),
1=>array(
'type' => 'bar'
),
2=>array(
'type' => 'bar'
)
))
becomes
a:3:{i:0;a:1:{s:4:"type";s:3:"foo";}i:1;a:1:{s:4:"type";s:3:"bar";}i:2;a:1:{s:4:"type";s:3:"bar";}}
So you can now run a strpos() or preg_match() function to find them. So your whole function would look like:
$exists = strpos('{s:4:"type";s:3:"bar";}',serialize($arrayOfTypes)); //returns number or false
It's short, it's snappy, and get's the job done for simple string keypairs.
This is basically the same as moonwave99's solution but slightly more useful as it's in a function that you can feed a key/value pair to as well, so it can be used to search for any key/value combo:
function isKVInArray($k, $v, $array) {
$filtered = array_filter($array, function($item) use($k,$v) {
return $item[$k] == $v;
});
if(count($filtered)>=1) return true;
else return false;
}
$k = 'live';
$v = 'y';
$my_array = array(
0=>array(
'live'=>'n',
'something_else'=>'a'
),
1=>array(
'live'=>'y',
'something_else'=>'b'
),
2=>array(
'live'=>'n',
'something_else'=>'c'
)
);
if(isKVInArray($k, $v, $my_array)) echo "$k=>$v was found in the array.";
else echo 'No luck sorry!';
there's another simple way which is useful when you need to count all the different values
foreach ($arrayOfTypes as $type) $cnt[$type['type']]++;
To get the number of 'bar' (or to get the count of another value):
echo($cnt['bar']);

How to combine the keys and values of an array in PHP

Say I have an array of key/value pairs in PHP:
array( 'foo' => 'bar', 'baz' => 'qux' );
What's the simplest way to transform this to an array that looks like the following?
array( 'foo=bar', 'baz=qux' );
i.e.
array( 0 => 'foo=bar', 1 => 'baz=qux');
In perl, I'd do something like
map { "$_=$hash{$_}" } keys %hash
Is there something like this in the panoply of array functions in PHP? Nothing I looked at seemed like a convenient solution.
Another option for this problem: On PHP 5.3+ you can use array_map() with a closure (you can do this with PHP prior 5.2, but the code will get quite messy!).
"Oh, but on array_map()you only get the value!".
Yeah, that's right, but we can map more than one array! :)
$arr = array( 'foo' => 'bar', 'baz' => 'qux' );
$result = array_map(function($k, $v){
return "$k=$v";
}, array_keys($arr), array_values($arr));
function parameterize_array($array) {
$out = array();
foreach($array as $key => $value)
$out[] = "$key=$value";
return $out;
}
A "curious" way to do it =P
// using '::' as a temporary separator, could be anything provided
// it doesn't exist elsewhere in the array
$test = split( '::', urldecode( http_build_query( $test, '', '::' ) ) );
chaos' answer is nice and straightfoward. For a more general sense though, you might have missed the array_map() function which is what you alluded to with your map { "$_=$hash{$_}" } keys %hash example.

Categories