Related
Given a string that contains values separated by dots:
property.entry.item
What is the best way to convert that to a key for an associative array?
$result['imported_data']['property']['entry']['item']
The string may be of any length, with any number of dots and contain an value:
people.arizona.phoenix.smith
I've tried the following without success:
//found a dot, means we are expecting output from a previous function
if( preg_match('[.]',$value)) {
//check for previous function output
if(!is_null($result['import'])) {
$chained_result_array = explode('.',$value);
//make sure we have an array to work with
if(is_array($chained_result_array)) {
$array_key = '';
foreach($chained_result_array as $key) {
$array_key .= '[\''.$key.'\']';
}
}
die(print_r(${result.'[\'import\']'.$array_key}));
}
}
I was thinking I could convert the string to a variable variable, but I get an array to string conversion error.
You can explode the string into an array and loop through the array. (DEMO)
/**
* This is a test array
*/
$testArray['property']['entry']['item'] = 'Hello World';
/**
* This is the path
*/
$string = 'property.entry.item';
/**
* This is the function
*/
$array = explode('.', $string);
foreach($array as $i){
if(!isset($tmp)){
$tmp = &$testArray[$i];
} else {
$tmp = $tmp[$i];
}
}
var_dump( $tmp ); // output = Hello World
Split the string into parts, and itterate the array, accessing each element in turn:
function arrayDotNotation($array, $dotString){
foreach(explode('.', $dotString) as $section){
$array = $array[$section];
}
return $array;
}
$array = ['one'=>['two'=>['three'=>'hello']]];
$string = 'one.two.three';
echo arrayDotNotation($array, $string); //outputs hello
Live example: http://codepad.viper-7.com/Vu8Hhy
You should really check to see if keys exist before you reference them. Otherwise, you're going to spew a lot of warnings.
function getProp($array, $propname) {
foreach(explode('.', $propname) as $node) {
if(isset($array[$node]))
$array = &$array[$node];
else
return null;
}
return $array;
}
Now you can do things like:
$x = array(
'name' => array(
'first' => 'Joe',
'last' => 'Bloe',
),
'age' => 27,
'employer' => array(
'current' => array(
'name' => 'Some Company',
)
)
);
assert(getProp($x, 'age') == 27);
assert(getProp($x, 'name.first') == 'Joe');
assert(getProp($x, 'employer.current.name') == 'Some Company');
assert(getProp($x, 'badthing') === NULL);
assert(getProp($x, 'address.zip') === NULL);
Or, if you are only interested in the import section of the tree:
getProp($x['import'], 'some.path');
I have this string:
"[\"form\",\"form-elements\"], [\"company\",\"asdasd\"], [\"cod_postal\",\"051768\"], [\"recaptcha_challenge_field\",\"03AHJ_VuvJZjfMzRQolI_QhGijJqAEcRPswuz9l68I6VStJzTbuK4ilos06TIQKVsIy2vpe1PEq-Q5KBVlZ5xt4HM5VoJSWUgFTGVbtbmARtKiMvO4WZh57X0-QVDyQ5Lq-ZM8rMqB5O2-rCbnyw_UAGbbV1ZElsI4kuLk4ei6mzLtqgcU2VAR64tySiKPARDtahiTBKWePH2rjKO6KUBQTRE49TMjIGb5hg8sbYguKBSUrRF6G86b89M\"], [\"recaptcha_response_field\",\"168\"]"
This string was formated form an array using this code:
function arrayDisplay($input)
{
return implode(
', ',
array_map(
function ($v, $k) {
return sprintf('["%s","%s"]', $k, $v);
},
$input,
array_keys($input)
)
);
}
And encoded using :
$xml_array = arrayDisplay($_POST);
$xml_encode = json_encode($xml_array);
And this is the array in $_POST variable:
array (size=5)
'form' => string 'form-elements' (length=13)
'company' => string 'asdasd' (length=6)
'cod_postal' => string '051768' (length=6)
'recaptcha_challenge_field' => string '03AHJ_VuvJZjfMzRQolI_QhGijJqAEcRPswuz9l68I6VStJzTbuK4ilos06TIQKVsIy2vpe1PEq-Q5KBVlZ5xt4HM5VoJSWUgFTGVbtbmARtKiMvO4WZh57X0-QVDyQ5Lq-ZM8rMqB5O2-rCbnyw_UAGbbV1ZElsI4kuLk4ei6mzLtqgcU2VAR64tySiKPARDtahiTBKWePH2rjKO6KUBQTRE49TMjIGb5hg8sbYguKBSUrRF6G86b89M' (length=249)
'recaptcha_response_field' => string '168' (length=3)
First I want to wrap the entire string replacing "" with [] :
[[\"form\",\"form-elements\"], [\"company\",\"asdasd\"], [\"cod_postal\",\"051768\"], [\"recaptcha_challenge_field\",\"03AHJ_VuvJZjfMzRQolI_QhGijJqAEcRPswuz9l68I6VStJzTbuK4ilos06TIQKVsIy2vpe1PEq-Q5KBVlZ5xt4HM5VoJSWUgFTGVbtbmARtKiMvO4WZh57X0-QVDyQ5Lq-ZM8rMqB5O2-rCbnyw_UAGbbV1ZElsI4kuLk4ei6mzLtqgcU2VAR64tySiKPARDtahiTBKWePH2rjKO6KUBQTRE49TMjIGb5hg8sbYguKBSUrRF6G86b89M\"], [\"recaptcha_response_field\",\"168\"]]
And exclude [\"form\",\"form-elements\"] , [\"recaptcha_challenge_field\",\"03AHJ_VuvJZjfMzRQolI_QhGijJqAEcRPswuz9l68I6VStJzTbuK4ilos06TIQKVsIy2vpe1PEq-Q5KBVlZ5xt4HM5VoJSWUgFTGVbtbmARtKiMvO4WZh57X0-QVDyQ5Lq-ZM8rMqB5O2-rCbnyw_UAGbbV1ZElsI4kuLk4ei6mzLtqgcU2VAR64tySiKPARDtahiTBKWePH2rjKO6KUBQTRE49TMjIGb5hg8sbYguKBSUrRF6G86b89M\"] and [\"recaptcha_response_field\",\"168\"]
What I can use to do that and how ?
You must filter the input. After filtering it you just return the json encoded string:
function arrayDisplay($input)
{
$input = array_filter($input, function ($key) {
return !in_array($key, array('form', 'recaptcha_challenge_field', 'recaptcha_response_field'));
}, ARRAY_FILTER_USE_KEY);
return json_encode(array($input));
}
If you still want to return the key-value pairs separated by comma then keep the original implode function and concatenate the result with the squared brackets:
function arrayDisplay($input)
{
$input = array_filter($input, function ($key) {
return !in_array($key, array('form', 'recaptcha_challenge_field', 'recaptcha_response_field'));
}, ARRAY_FILTER_USE_KEY);
return '[' . implode(
', ',
array_map(
function ($v, $k) {
return sprintf('["%s","%s"]', $k, $v);
},
$input,
array_keys($input)
)
) . ']';
}
Please note that $xml_encode = json_encode($xml_array); makes no sense as the returned string is "almost" a valid json encoded string (in the original function). If this is your intention then you must use the first function I wrote and remove that line of code that is encoding again the returned value.
You should start from your $_POST array, and take the keys from it you really need:
$input = $_POST;
unset($input["form"]);
unset($input["recaptcha_challenge_field"]);
unset($input["recaptcha_response_field"]);
Now that $input array looks like this:
array(
"company" => "asdasd",
"cod_postal" => "051768"
);
Then, I would change the definition of your arrayDisplay function without altering its functionality, by splitting it into 2:
function arrayPairs($input) {
return array_map(
function ($v, $k) {
return [$k,$v];
},
$input,
array_keys($input)
);
}
function arrayDisplay($input) {
return implode (", ", array_map('json_encode', arrayPairs($input)));
}
This arrayDisplay function still returns the same values as before, but you can now also call arrayPairs without conversion to string. Instead you would do that conversion with json_encode which gives exactly the output you want:
echo json_encode(arrayPairs($input));
This outputs:
[["company","asdasd"],["cod_postal","051768"]]
NB: If you don't use the arrayDisplay function for any other purpose, then you can do away with it. This solution only uses the function arrayPairs.
I made some changes and ended up with this:
function arrayDisplay($input)
{
$goodKeys = array_diff(array_keys($input), array('form', 'recaptcha_challenge_field', 'recaptcha_response_field'));
var_dump($goodKeys);
$data = [];
foreach ($goodKeys as $goodKey) {
$data[$goodKey] = $input[$goodKey];
}
return '[' . implode(
', ',
array_map(
function ($v, $k) {
return sprintf('["%s","%s"]', $k, $v);
},
$data,
array_keys($data)
)
) . ']';
}
but now there is one more problem, I need to get rid of "" this 2 from the start/end of the string:
from this:
"[[\"company\",\"123\"], [\"cod_postal\",\"051768\"]]"
to this:
[[\"company\",\"123\"], [\"cod_postal\",\"051768\"]]
Given a string that contains values separated by dots:
property.entry.item
What is the best way to convert that to a key for an associative array?
$result['imported_data']['property']['entry']['item']
The string may be of any length, with any number of dots and contain an value:
people.arizona.phoenix.smith
I've tried the following without success:
//found a dot, means we are expecting output from a previous function
if( preg_match('[.]',$value)) {
//check for previous function output
if(!is_null($result['import'])) {
$chained_result_array = explode('.',$value);
//make sure we have an array to work with
if(is_array($chained_result_array)) {
$array_key = '';
foreach($chained_result_array as $key) {
$array_key .= '[\''.$key.'\']';
}
}
die(print_r(${result.'[\'import\']'.$array_key}));
}
}
I was thinking I could convert the string to a variable variable, but I get an array to string conversion error.
You can explode the string into an array and loop through the array. (DEMO)
/**
* This is a test array
*/
$testArray['property']['entry']['item'] = 'Hello World';
/**
* This is the path
*/
$string = 'property.entry.item';
/**
* This is the function
*/
$array = explode('.', $string);
foreach($array as $i){
if(!isset($tmp)){
$tmp = &$testArray[$i];
} else {
$tmp = $tmp[$i];
}
}
var_dump( $tmp ); // output = Hello World
Split the string into parts, and itterate the array, accessing each element in turn:
function arrayDotNotation($array, $dotString){
foreach(explode('.', $dotString) as $section){
$array = $array[$section];
}
return $array;
}
$array = ['one'=>['two'=>['three'=>'hello']]];
$string = 'one.two.three';
echo arrayDotNotation($array, $string); //outputs hello
Live example: http://codepad.viper-7.com/Vu8Hhy
You should really check to see if keys exist before you reference them. Otherwise, you're going to spew a lot of warnings.
function getProp($array, $propname) {
foreach(explode('.', $propname) as $node) {
if(isset($array[$node]))
$array = &$array[$node];
else
return null;
}
return $array;
}
Now you can do things like:
$x = array(
'name' => array(
'first' => 'Joe',
'last' => 'Bloe',
),
'age' => 27,
'employer' => array(
'current' => array(
'name' => 'Some Company',
)
)
);
assert(getProp($x, 'age') == 27);
assert(getProp($x, 'name.first') == 'Joe');
assert(getProp($x, 'employer.current.name') == 'Some Company');
assert(getProp($x, 'badthing') === NULL);
assert(getProp($x, 'address.zip') === NULL);
Or, if you are only interested in the import section of the tree:
getProp($x['import'], 'some.path');
I'm searching for a way to use named arguments for sprintf or printf.
Example:
sprintf(
'Last time logged in was %hours hours,
%minutes minutes, %seconds seconds ago'
,$hours,$minutes, $seconds
);
or via vsprintf and an associative array.
I have found some coding examples here
function sprintfn ($format, array $args = array())
http://php.net/manual/de/function.sprintf.php
and here
function vnsprintf( $format, array $data)
http://php.net/manual/de/function.vsprintf.php
where people wrote their own solutions.
But my question is, is there maybe an standard PHP solution out there to achieve this or is there another way, maybe with a simple PHP templating provided by PEAR, that I can achieve this by sticking to standard PHP?
Thanks for any help.
Late to the party, but you can simply use strtr to "translate characters or replace substrings"
<?php
$hours = 2;
$minutes = 24;
$seconds = 35;
// Option 1: Replacing %variable
echo strtr(
'Last time logged in was %hours hours, %minutes minutes, %seconds seconds ago',
[
'%hours' => $hours,
'%minutes' => $minutes,
'%seconds' => $seconds
]
);
// Option 2: Alternative replacing {variable}
echo strtr(
'Last time logged in was {hours} hours, {minutes} minutes, {seconds} seconds ago',
[
'{hours}' => $hours,
'{minutes}' => $minutes,
'{seconds}' => $seconds
]
);
// Option 3: Using an array with variables:
$data = [
'{hours}' => 2,
'{minutes}' => 24,
'{seconds}' => 35,
];
echo strtr('Last time logged in was {hours} hours, {minutes} minutes, {seconds} seconds ago', $data);
// More options: Of course you can replace any string....
outputs the following:
Last time logged in was 2 hours, 24 minutes, 35 seconds ago
I've written a small component exactly for this need. It's called StringTemplate.
With it you can get what you want with a code like this:
$engine = new StringTemplate\Engine;
$engine->render(
'Last time logged in was {hours} hours, {minutes} minutes, {seconds} seconds ago',
[
'hours' => '08',
'minutes' => 23,
'seconds' => 12,
]
);
//Prints "Last time logged in was 08 hours, 23 minutes, 12 seconds ago"
Hope that can help.
As far as I know printf/sprintf does not accept assoc arrays.
However it is possible to do printf('%1$d %1$d', 1);
Better than nothing ;)
This is from php.net
function vnsprintf( $format, array $data)
{
preg_match_all( '/ (?<!%) % ( (?: [[:alpha:]_-][[:alnum:]_-]* | ([-+])? [0-9]+ (?(2) (?:\.[0-9]+)? | \.[0-9]+ ) ) ) \$ [-+]? \'? .? -? [0-9]* (\.[0-9]+)? \w/x', $format, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
$offset = 0;
$keys = array_keys($data);
foreach( $match as &$value )
{
if ( ( $key = array_search( $value[1][0], $keys, TRUE) ) !== FALSE || ( is_numeric( $value[1][0] ) && ( $key = array_search( (int)$value[1][0], $keys, TRUE) ) !== FALSE) )
{
$len = strlen( $value[1][0]);
$format = substr_replace( $format, 1 + $key, $offset + $value[1][1], $len);
$offset -= $len - strlen( 1 + $key);
}
}
return vsprintf( $format, $data);
}
Example:
$example = array(
0 => 'first',
'second' => 'second',
'third',
4.2 => 'fourth',
'fifth',
-6.7 => 'sixth',
'seventh',
'eighth',
'9' => 'ninth',
'tenth' => 'tenth',
'-11.3' => 'eleventh',
'twelfth'
);
echo vnsprintf( '%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s %10$s %11$s %12$s<br />', $example); // acts like vsprintf
echo vnsprintf( '%+0$s %second$s %+1$s %+4$s %+5$s %-6.5$s %+6$s %+7$s %+9$s %tenth$s %-11.3$s %+10$s<br />', $example);
Example 2:
$examples = array(
2.8=>'positiveFloat', // key = 2 , 1st value
-3=>'negativeInteger', // key = -3 , 2nd value
'my_name'=>'someString' // key = my_name , 3rd value
);
echo vsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples); // output : "someString"
echo vsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples); // output : "positiveFloat"
echo vsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples); // output : "positiveFloat"
echo vsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples); // output : "negativeInteger"
echo vsprintf( "%%2\$s = '%2\$s'\n", $examples); // output : "negativeInteger"
echo vnsprintf( "%%2\$s = '%2\$s'\n", $examples); // output : [= vsprintf]
echo vsprintf( "%%+2\$s = '%+2\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%+2\$s = '%+2\$s'\n", $examples); // output : "positiveFloat"
echo vsprintf( "%%-3\$s = '%-3\$s'\n", $examples); // [unsupported]
echo vnsprintf( "%%-3\$s = '%-3\$s'\n", $examples); // output : "negativeInteger"
I know this has been resolved for too long now, but maybe my solution is simple enough, yet useful for somebody else.
With this little function you can mimic a simple templating system:
function parse_html($html, $args) {
foreach($args as $key => $val) $html = str_replace("#[$key]", $val, $html);
return $html;
}
Use it like this:
$html = '<h1>Hello, #[name]</h1>';
$args = array('name' => 'John Appleseed';
echo parse_html($html,$args);
This would output:
<h1>Hello, John Appleseed</h1>
Maybe not for everyone and every case, but it saved me.
See drupal's implementation
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/format_string/7
It's simple and doesn't use regexp
function format_string($string, array $args = array()) {
// Transform arguments before inserting them.
foreach ($args as $key => $value) {
switch ($key[0]) {
case '#':
// Escaped only.
$args[$key] = check_plain($value);
break;
case '%':
default:
// Escaped and placeholder.
$args[$key] = drupal_placeholder($value);
break;
case '!':
// Pass-through.
}
}
return strtr($string, $args);
}
function drupal_placeholder($text) {
return '<em class="placeholder">' . check_plain($text) . '</em>';
}
Example:
$unformatted = 'Hello, #name';
$formatted = format_string($unformatted, array('#name' => 'John'));
This is what I'm using:
$arr = ['a' => 'happy','b' => 'funny'];
$templ = "I m a [a] and [b] person";
$r = array_walk($arr,function($i,$k) use(&$templ){
$templ = str_replace("[$k]",$i,$templ);
} );
var_dump($templ);
Since 5.3 because of the use keyword:
This function supports formatting {{var}} or {{dict.key}}, you can change the {{}} to {} etc to match you favor.
function formatString($str, $data) {
return preg_replace_callback('#{{(\w+?)(\.(\w+?))?}}#', function($m) use ($data){
return count($m) === 2 ? $data[$m[1]] : $data[$m[1]][$m[3]];
}, $str);
}
Example:
$str = "This is {{name}}, I am {{age}} years old, I have a cat called {{pets.cat}}.";
$dict = [
'name' => 'Jim',
'age' => 20,
'pets' => ['cat' => 'huang', 'dog' => 'bai']
];
echo formatString($str, $dict);
Output:
This is Jim, I am 20 years old, I have a cat called huang.
This is really the best way to go imho. No cryptic characters, just use the key names!
As taken from the php site:
http://www.php.net/manual/en/function.vsprintf.php
function dsprintf() {
$data = func_get_args(); // get all the arguments
$string = array_shift($data); // the string is the first one
if (is_array(func_get_arg(1))) { // if the second one is an array, use that
$data = func_get_arg(1);
}
$used_keys = array();
// get the matches, and feed them to our function
$string = preg_replace('/\%\((.*?)\)(.)/e',
'dsprintfMatch(\'$1\',\'$2\',\$data,$used_keys)',$string);
$data = array_diff_key($data,$used_keys); // diff the data with the used_keys
return vsprintf($string,$data); // yeah!
}
function dsprintfMatch($m1,$m2,&$data,&$used_keys) {
if (isset($data[$m1])) { // if the key is there
$str = $data[$m1];
$used_keys[$m1] = $m1; // dont unset it, it can be used multiple times
return sprintf("%".$m2,$str); // sprintf the string, so %s, or %d works like it should
} else {
return "%".$m2; // else, return a regular %s, or %d or whatever is used
}
}
$str = <<<HITHERE
Hello, %(firstName)s, I know your favorite PDA is the %(pda)s. You must have bought %(amount)s
HITHERE;
$dataArray = array(
'pda' => 'Newton 2100',
'firstName' => 'Steve',
'amount' => '200'
);
echo dsprintf($str, $dataArray);
// Hello, Steve, I know your favorite PDA is the Newton 2100. You must have bought 200
You'll want to avoid using % in you custom functions as it can interfere with other implementations, for example, date formatting in SQL, so...
function replace(string $string, iterable $replacements): string
{
return str_replace(
array_map(
function($k) {
return sprintf("{%s}", $k);
},
array_keys($replacements)
),
array_values($replacements),
$string
);
}
$string1 = 'Mary had a little {0}. Its {1} was white as {2}.';
echo replace($string1, ['lamb', 'fleece', 'snow']);
$string2 = 'Mary had a little {animal}. Its {coat} was white as {color}.';
echo replace($string2, ['animal' => 'lamb', 'coat' => 'fleece', 'color' => 'snow']);
$string1: Mary had a little lamb. Its fleece was white as snow.
$string2: Mary had a little lamb. Its fleece was white as snow.
In PHP Consider this function:
function test($name, $age) {}
I need to somehow extract the parameter names (for generating custom documentations automatically) so that I could do something like:
get_func_argNames('test');
and it would return:
Array['name','age']
Is this even possible in PHP?
You can use Reflection :
function get_func_argNames($funcName) {
$f = new ReflectionFunction($funcName);
$result = array();
foreach ($f->getParameters() as $param) {
$result[] = $param->name;
}
return $result;
}
print_r(get_func_argNames('get_func_argNames'));
//output
Array
(
[0] => funcName
)
It's 2019 and no one said this?
Just use get_defined_vars():
class Foo {
public function bar($a, $b) {
var_dump(get_defined_vars());
}
}
(new Foo)->bar('first', 'second');
Result:
array(2) {
["a"]=>
string(5) "first"
["b"]=>
string(6) "second"
}
This is an old question I happened on looking for something other then Reflection, but I will toss out my current implementation so that it might help someone else. Using array_map
For a method
$ReflectionMethod = new \ReflectionMethod($class, $method);
$params = $ReflectionMethod->getParameters();
$paramNames = array_map(function( $item ){
return $item->getName();
}, $params);
For a function
$ReflectionFunction = new \ReflectionFunction('preg_replace');
$params = $ReflectionFunction->getParameters();
$paramNames = array_map(function( $item ){
return $item->getName();
}, $params);
echo '<pre>';
var_export( $paramNames );
Outputs
array(
'0' => 'regex',
'1' => 'replace',
'2' => 'subject',
'3' => 'limit',
'4' => 'count'
)
Cheers,
In addition to Tom Haigh's answer if you need to get the default value of optional attributes:
function function_get_names($funcName){
$attribute_names = [];
if(function_exists($funcName)){
$fx = new ReflectionFunction($funcName);
foreach ($fx->getParameters() as $param){
$attribute_names[$param->name] = NULL;
if ($param->isOptional()){
$attribute_names[$param->name] = $param->getDefaultValue();
}
}
}
return $attribute_names;
}
Useful for variable type validation.
#Tom Haigh, or do it more classy:
function getArguments( $funcName ) {
return array_map( function( $parameter ) { return $parameter->name; },
(new ReflectionFunction($funcName))->getParameters() );
}
// var_dump( getArguments('getArguments') );
func_get_args
function my($aaa, $bbb){
var_dump( func_get_args() );
}
my("something","something"); //result: array('aaa' => 'something', 'bbb' => 'something');
also, there exists another global functions: get_defined_vars(), that returns not only function, but all variables.