I have string like below
["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza
Which i am trying to convert into array like below
$arr["Day1"]["Morning"] = "mutton";
$arr["Day1"]["Evening"] = "Juice";
$arr["Day2"]["morning"] = "burger";
$arr["Day2"]["evening"] = "pizza";
I tried something like below.
$str = '["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
$pieces = explode("&", $str);
foreach($pieces as $pie)
{
$arr.$pie;
}
var_dump($arr);
I know above code is really dumb :/ .Is there any proper solution for this ?
You could do like this...
<?php
$str='["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
$arr = explode('&',$str);
foreach($arr as $v)
{
$valarr=explode('=',$v);
preg_match_all('/"(.*?)"/', $valarr[0], $matches);
$narr[$matches[1][0]][$matches[1][1]]=$valarr[1];
}
print_r($narr);
OUTPUT :
Array
(
[Day1] => Array
(
[Morning] => mutton
[Evening] => Juice
)
[Day2] => Array
(
[Morning] => burger
[Evening] => pizza
)
)
You could access like echo $arr["Day1"]["Morning"] which prints mutton
Demo
It looks like it could be parsed with parse_str(), but not without some conversion:
$str = '["Day1"]["Morning"]=mutton&["Day1"]["Evening"]=Juice&["Day2"]["Morning"]=burger&["Day2"]["Evening"]=pizza';
parse_str(preg_replace('/(?<=^|&)/', 'x', str_replace('"', '', $str)), $a);
var_dump($a['x']);
It removes the double quotes, prefixes each entry with x and then applies parse_str(). To get an idea of what the preg_replace() does, the intermediate result is this:
x[Day1][Morning]=mutton&x[Day1][Evening]=Juice&x[Day2][Morning]=burger&x[Day2][Evening]=pizza
Parsing the above string yields an array with a single root element x.
Related
I have an array:
$a = array('foo' => 'fooMe');
and I do:
print_r($a);
which prints:
Array ( [foo] => printme )
Is there a function, so when doing:
needed_function(' Array ( [foo] => printme )');
I will get the array array('foo' => 'fooMe'); back?
I actually wrote a function that parses a "stringed array" into an actual array. Obviously, it's somewhat hacky and whatnot, but it works on my testcase. Here's a link to a functioning prototype at http://codepad.org/idlXdij3.
I'll post the code inline too, for those people that don't feel like clicking on the link:
<?php
/**
* #author ninetwozero
*/
?>
<?php
//The array we begin with
$start_array = array('foo' => 'bar', 'bar' => 'foo', 'foobar' => 'barfoo');
//Convert the array to a string
$array_string = print_r($start_array, true);
//Get the new array
$end_array = text_to_array($array_string);
//Output the array!
print_r($end_array);
function text_to_array($str) {
//Initialize arrays
$keys = array();
$values = array();
$output = array();
//Is it an array?
if( substr($str, 0, 5) == 'Array' ) {
//Let's parse it (hopefully it won't clash)
$array_contents = substr($str, 7, -2);
$array_contents = str_replace(array('[', ']', '=>'), array('#!#', '#?#', ''), $array_contents);
$array_fields = explode("#!#", $array_contents);
//For each array-field, we need to explode on the delimiters I've set and make it look funny.
for($i = 0; $i < count($array_fields); $i++ ) {
//First run is glitched, so let's pass on that one.
if( $i != 0 ) {
$bits = explode('#?#', $array_fields[$i]);
if( $bits[0] != '' ) $output[$bits[0]] = $bits[1];
}
}
//Return the output.
return $output;
} else {
//Duh, not an array.
echo 'The given parameter is not an array.';
return null;
}
}
?>
If you want to store an array as string, use serialize [docs] and unserialize [docs].
To answer your question: No, there is no built-in function to parse the output of print_r into an array again.
No. But you can use both serialize and json_* functions.
$a = array('foo' => 'fooMe');
echo serialize($a);
$a = unserialize($input);
Or:
echo json_encode($a);
$a = json_decode($input, true);
you cannot do this with print_r,
var_export should allow something similar, but not exactly what you asked for
http://php.net/manual/en/function.var-export.php
$val = var_export($a, true);
print_r($val);
eval('$func_val='.$val.';');
There is a nice Online-Tool which does exatly what its name is:
print_r to json online converter
From a JSON Object its not far to creating an array with the json_decode function:
To get an array from this, set the second paramter to true. If you don't, you will get an object instead.
json_decode($jsondata, true);
I think my function is cool too, works with nested arrays:
function print_r_reverse($input)
{
$output = str_replace(['[', ']'], ["'", "'"], $input);
$output = preg_replace('/=> (?!Array)(.*)$/m', "=> '$1',", $output);
$output = preg_replace('/^\s+\)$/m', "),\n", $output);
$output = rtrim($output, "\n,");
return eval("return $output;");
}
NB: better not use this with user input data
Here is a print_r output parser, producing the same expression in PHP syntax. It is written as an interactive Stack Snippet, so you can use it here:
function parse(s) {
const quote = s => '"' + s.replace(/["\\]/g, '\\$&') + '"';
const compress = indent => " ".repeat(((indent.length + 4) >> 3) * 4);
return "$data = " + (s.replace(/\r\n?/g, "\n") + "\n").replace(
/(Array|\w+ (Object)) *\n *\( *\n|^( *)(?:\[(?:(0|-?[1-9]\d*)|(.*?))\] => |(\) *\n+))|(-?\d+(?:\.\d+)?(?:E[-+]\d+)?)\n|(.*(?:\n(?! *\) *$| *\[.*?\] => ).*)*)\n/gm,
(_, array, object, indent, index, key, close, number, string) =>
object ? "(object) [\n"
: array ? "[\n"
: close ? compress(indent) + "],\n"
: indent ? compress(indent) + (index ?? quote(key)) + " => "
: (number ?? quote(string)) + ",\n"
).replace(/,\n$/, ";");
}
// I/O handling
const [input, output] = document.querySelectorAll("textarea");
(input.oninput = () => output.value = parse(input.value))();
textarea { width: 23em; height: 12em }
<table><tr><th>Input print_r format</th><th>Output PHP syntax</th></tr>
<tr><td><textarea>
Array
(
[0] => example
[1] => stdClass Object
(
[a[] => 1.43E+19
["] => quote
[] =>
)
)
</textarea></td><td><textarea readonly></textarea></td></tr></table>
Remarks
Don't remove any line breaks from the original print_r output. For instance, both the opening and closing parentheses after Array must appear on separate lines.
Don't change the spacing around => (one space before, one after).
As print_r does not distinguish between null, "" or false (it produces no output for these values), nor between true and 1 (both are output as 1), this converter will never produce null, false or true.
As print_r does not distinguish between numbers and strings (9 could represent a number or a string), this converter will assume that the data type is to be numeric when such ambiguity exists.
stdClass Object is supported and translates to (object) [...] notation
MyClass Object will be treated as if it was a stdClass object.
String literals in the output are double quoted and literal double quotes and backslashes are escaped.
Quick function (without checks if you're sending good data):
function textToArray($str)
{
$output = [];
foreach (explode("\n", $str) as $line) {
if (trim($line) == "Array" or trim($line) == "(" or trim($line) == ")") {
continue;
}
preg_match("/\[(.*)\]\ \=\>\ (.*)$/i", $line, $match);
$output[$match[1]] = $match[2];
}
return $output;
}
This is the expected input:
Array
(
[test] => 6
)
This is how I interpreted the question:
function parsePrintedArray($s){
$lines = explode("\n",$s);
$a = array();
foreach ($lines as $line){
if (strpos($line,"=>") === false)
continue;
$parts = explode('=>',$line);
$a[trim($parts[0],'[] ')] = trim($parts[1]);
}
return $a;
}
Works for both objects and arrays:
$foo = array (
'foo' => 'bar',
'cat' => 'dog'
);
$s = print_r($foo,1);
$a = parsePrintedArray($s);
print_r($a);
Output:
Array
(
[foo] => bar
[cat] => dog
)
doesnt work on nested arrays, but simple and fast.
use
var_export(array('Sample array', array('Apple', 'Orange')));
Output:
array (
0 => 'Sample array',
1 =>
array (
0 => 'Apple',
1 => 'Orange',
),
)
json_encode() and json_decode() function will do it.
$asso_arr = Array([779] => 79 => [780] => 80 [782] => 82 [783] => 83);
$to_string = json_encode($asso_arr);
It will be as a json format {"779":"79","780":"80","782":"82","783":"83"}
Then we will convert it into json_decode() then it gives associative array same as original:
print_r(json_decode($to_string));
Output will be Array([779] => 79 => [780] => 80 [782] => 82 [783] => 83) in associative array format.
I have string like this:
['key1':'value1', 2:'value2', 3:$var, 4:'with\' quotes', 5:'with, comma']
And I want to convert it to an array like this:
$parsed = [
'key1' => 'value1',
2 => 'value2',
3 => '$var',
4 => 'with\' quotes',
5 => 'with, comma',
];
How can I parse that?
Any tips or codes will be appreciated.
What can't be done?
Using standard json parsers
eval()
explode() by , and explode() by :
As you cannot use any pre-built function, like json_decode, you'll have to try and find the most possible scenarios of quoting, and replace them with known substrings.
Given that all of the values and/or keys in the input array are encapsulated in single quotes:
Please note: this code is untested
<?php
$input = "[ 'key1':'value1', 2:'value2', 3:$var, 4:'with\' quotes', 5: '$var', 'another_key': 'something not usual, like \'this\'' ]";
function extractKeysAndValuesFromNonStandardKeyValueString ( $string ) {
$input = str_replace ( Array ( "\\\'", "\'" ), Array ( "[DOUBLE_QUOTE]", "[QUOTE]" ), $string );
$input_clone = $input;
$return_array = Array ();
if ( preg_match_all ( '/\'?([^\':]+)\'?\s*\:\s*\'([^\']+)\'\s*,?\s*/', $input, $matches ) ) {
foreach ( $matches[0] as $i => $full_match ) {
$key = $matches[1][$i];
$value = $matches[2][$i];
if ( isset ( ${$value} ) $value = ${$value};
else $value = str_replace ( Array ( "[DOUBLE_QUOTE]", "[QUOTE]" ), Array ( "\\\'", "\'" ), $value );
$return_array[$key] = $value;
$input_clone = str_replace ( $full_match, '', $input_clone );
}
// process the rest of the string, if anything important is left inside of it
if ( preg_match_all ( '/\'?([^\':]+)\'?\s*\:\s*([^,]+)\s*,?\s*/', $input_clone, $matches ) ) {
foreach ( $matches[0] as $i => $full_match ) {
$key = $matches[1][$i];
$value = $matches[2][$i];
if ( isset ( ${$value} ) $value = ${$value};
$return_array[$key] = $value;
}
}
}
return $return_array;
}
The idea behind this function is to first replace all the possible combinations of quotes in the non-standard string with something you can easily replace, then perform a standard regexp against your input, then rebuild everything assuring you're resetting the previously replaced substrings
This is the initial string:-
NAME=Marco\nLOCATION=localhost\nSECRET=fjsdgfsjfdskffuv=\n
This is my solution although the "=" in the end of the string does not appear in the array
$env = file_get_contents(base_path() . '/.env');
// Split string on every " " and write into array
$env = preg_split('/\s+/', $env);
//create new array to push data in the foreach
$newArray = array();
foreach($env as $val){
// Split string on every "=" and write into array
$result = preg_split ('/=/', $val);
if($result[0] && $result[1])
{
$newArray[$result[0]] = $result[1];
}
}
print_r($newArray);
This is the result I get:
Array ( [Name] => Marco [LOCATION] => localhost [SECRET] => fjsdgfsjfdskffuv )
But I need :
Array ( [Name] => Marco [LOCATION] => localhost [SECRET] => fjsdgfsjfdskffuv= )
You can use the limit parameter of preg_split to make it only split the string once
http://php.net/manual/en/function.preg-split.php
you should change
$result = preg_split ('/=/', $val);
to
$result = preg_split ('/=/', $val, 2);
Hope this helps
$string = 'NAME=Marco\nLOCATION=localhost\nSECRET=fjsdgfsjfdskffuv=\n';
$strXlate = [ 'NAME=' => '"NAME":"' ,
'LOCATION=' => '","LOCATION":"',
'SECRET=' => '","SECRET":"' ,
'\n' => '' ];
$jsonified = '{'.strtr($string, $strXlate).'"}';
$array = json_decode($jsonified, true);
This is based on 1) translation using strtr(), preparing an array in json format and then using a json_decode which blows it up nicely into an array...
Same result, other approach...
You can also use parse_str to parse URL syntax-like strings to name-value pairs.
Based on your example:
$newArray = [];
$str = file_get_contents(base_path() . '/.env');
$env = explode("\n", $str);
array_walk(
$env,
function ($i) use (&$newArray) {
if (!$i) { return; }
$tmp = [];
parse_str($i, $tmp);
$newArray[] = $tmp;
}
);
var_dump($newArray);
Of course, you need to put some sanity check in the function since it can insert some strange stuff in the array like values with empty string keys, and whatnot.
I have an array with a list of all controllers in my application:
$controllerlist = glob("../controllers/*_controller.php");
How do I strip ../controllers/ at the start and _controller.php at the end of each array element with one PHP command?
As preg_replace can act on an array, you could do:
$array = array(
"../controllers/test_controller.php",
"../controllers/hello_controller.php",
"../controllers/user_controller.php"
);
$array = preg_replace('~../controllers/(.+?)_controller.php~', "$1", $array);
print_r($array);
output:
Array
(
[0] => test
[1] => hello
[2] => user
)
Mapping one array to another:
$files = array(
'../controllers/test_controller.php',
'../controllers/hello_controller.php'
);
$start = strlen('../controllers/');
$end = strlen('_controller.php') * -1;
$controllers = array_map(
function($value) use ($start, $end) {
return substr($value, $start, $end);
},
$files
);
var_dump($controllers);
I'm not sure how you defined "command", but I doubt there is a way to do that with one simple function call.
However, if you're simply wanting it to be compact, here's a simple way of doing it:
$controllerlist = explode('|||', str_replace(array('../controllers/', '_controller.php'), '', implode('|||', glob("../controllers/*_controller.php"))));
It's a bit dirty, but it gets the job done in a single line.
One command without searching and replacing? Yes you can!
If I'm not missing something grande, what about keeping it simple and chopping 15 characters from the start and the end using the substr function:
substr ( $x , 15 , -15 )
Since glob will always give you strings with that pattern.
Example:
// test array (thanks FruityP)
$array = array(
"../controllers/test_controller.php",
"../controllers/hello_controller.php",
"../controllers/user_controller.php" );
foreach($array as $x){
$y=substr($x,15,-15); // Chop 15 characters from the start and end
print("$y\n");
}
Output:
test
hello
user
No need for regex in this case unless there can be variations of what you mentioned.
$array = array(
"../controllers/test_controller.php",
"../controllers/hello_controller.php",
"../controllers/user_controller.php"
);
// Actual one liner..
$list = str_replace(array('../controllers/', '_controller.php'), "", $array);
var_dump($array);
This will output
array (size=3)
0 => string 'test' (length=4)
1 => string 'hello' (length=5)
2 => string 'user' (length=4)
Which is (I think) what you asked for.
If you have an array like this :
$array = array( "../controllers/*_controller.php",
"../controllers/*_controller.php");
Then array_map() help you to trim the unnecessary string.
function trimmer( $string ){
return str_replace( "../controllers/", "", $string );
}
$array = array( "../controllers/*_controller.php",
"../controllers/*_controller.php");
print_r( array_map( "trimmer", $array ) );
http://codepad.org/VO6kyVOa
to strip 15 chars at the start and 15 at the end of each arrayelement in one command:
$controllerlist = substr_replace(
substr_replace(
glob("../controllers/*_controller.php"),'',-15
),'',0,15
)
preg_replace accepts an array as argument too:
$before = '../controllers/';
$after = "_controller.php";
$preg_str = preg_quote($before,"/").'(.*)'.preg_quote($after,"/");
$controllerlist = preg_replace('/^'.$preg_str.'$/', '\1', glob("$before*$after"));
I have an string (like one.two.three or month.2) and I need to translate this string exploding point character into array keys.
So I have month.2 and I need the codeline to translate this string into $lang['month'][2]
I was looking for that solution but I am not able to find it, I am blocked.
I finally found this:
<?php
$lang['one']['two']['three'] = 'well done';
$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
$result .= '["'.$item.'"]';
}
var_dump(eval("return " . $result.';'));
?>
Start by exploding it:
$in = explode('.', $in);
Then rewrite it as a nested array:
$arg = array();
foreach (array_reverse($in) as $key) {
$arg = array($key => $arg);
}
And finally merge:
$out = array_merge_recursive($arg, $out);
Edit: in case you're seeking to read the array instead, then bazmegakapa's answer is what you're looking for.
I wrote a little recursive function to handle this. First you explode the string, and pass the array ($lang in your example, $a in mine) and this exploded array to GetVal(). It will do its best to return the needed value into $x.
$s="month.2";
$keys=explode('.', $s);
$a=array(
'month' => array('1' => 'fos', '2' => 'fos2'),
'retek' => 1
);
function GetVal($array, $keyarray) {
$key=array_shift($keyarray);
if (array_key_exists($key, $array)) {
if (count($keyarray)==0) {
return $array[$key];
} else {
return GetVal($array[$key], $keyarray);
}
} else {
return null;
}
}
$x=GetVal($a, $keys);
var_dump($x);
Its a clarification for the use of explode function:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
this code explodes as:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)
not as Array[hello][world][its][a].......