Line and colon separated list to array PHP - php

I'm trying to work out a simple way to take a list, like this
foo: Alpha
bar: Bravo
fooBar: Charlie
And turn this into an associative array so that values would be
$array['foo'] //would contain Alpha
$array['bar'] //would contain Bravo
etc.
What is the cleanest way to achieve this ?

You can do it manually if your first list is already in an array:
$array = [];
$list = ['foo: Alpha', 'bar: Bravo'];
foreach ($list as $element) {
$parts = explode(': ', $element);
$array[$parts[0]] = $parts[1];
}
Otherwise, simply use parse_ini_string to parse a string that contains your data into an associative array (note that this function requires PHP 5.3 or greater).
If your data is in a string, and you don't have PHP 5.3, you can split on new lines to get an array: $list = explode("\n", $string);.

Something like this?:
$string = "foo: Alpha
bar: Bravo
fooBar: Charlie";
$array = array();
$lines = explode("\n", $string);
foreach ($lines as $line) {
list($key, $value) = explode(": ", $line);
$array[$key] = $value;
}
var_dump($array);
Result:
array(3) {
["foo"]=>
string(6) "Alpha
"
["bar"]=>
string(6) "Bravo
"
["fooBar"]=>
string(7) "Charlie"
}

Maybe this is overkill but if your file format is likely to expand, it's worth looking into YAML, your example happens to be valid YAML markup. So you could for example use the Symfony YAML component
use Symfony\Component\Yaml\Yaml;
$array = Yaml::parse('/path/to/file.yml');
It works with your current format and if you decide to add nested arrays or other non-trivial data, just use the YAML syntax, which is quite intuitive. Here is an introduction to the format:
http://symfony.com/doc/2.0/components/yaml/yaml_format.html

Try this :
$array = array();
$str = 'foo: Alpha';
$res = explode(":",$str);
$array[$res[0]] = $res[0];

Related

php regex to extract single parameter value from string

I'm working with a string containing parameters, separated by some special characters in PHP with preg_match
An example could be like this one, which has four parameters.
1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?
Each parameter name is followed by ?#?, and its value is right next to it, ending with ?#? (note: values can be strings or numbers, and even special characters)
I've probably overcomplicated my regex, which works in SOME cases, but not if I search for the last parameter in the string..
This example returns 2222 as the correct value (in group 1) for 2ndParm
(?:.*)2ndParm\?#\?(.*?)\?#\?(?=.)(.*)
but it fails if 2ndParm is the last one in the string as in the following example:
1stparm?#?1111?#?2ndParm?#?2222?#?
I'd also appreciate help in just returning one group with my result.. i havent been able to do so, but since I always get the one I'm interested in group 1, I can get it easily anyway.
Without regex:
$str ='1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?';
$keyval = explode('?#?', trim($str, '?#'));
$result = [];
foreach($keyval as $item) {
[$key, $result[$key]] = explode('?#?', $item);
}
print_r($result);
demo
You don't need to use a regex for everything, and you should have a serious talk with whoever invented this horrid format about the fact that JSON, YAML, TOML, XML, etc exist.
function bizarre_unserialize($in) {
$tmp = explode('?#?', $in);
$tmp = array_filter($tmp); // remove empty
$tmp = array_map(
function($a) { return explode('?#?', $a); },
$tmp
);
// rearrange to key-value
return array_combine(array_column($tmp, 0), array_column($tmp, 1));
}
$input = '1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?';
var_dump(
bizarre_unserialize($input)
);
Output:
array(4) {
["1stparm"]=>
string(4) "1111"
["2ndParm"]=>
string(4) "2222"
["3rdParm"]=>
string(4) "3333"
["4thparm"]=>
string(3) "444"
}
You can use
(?P<key>.+?)
\Q?#?\E
(?P<value>.+?)
\Q?#?\E
in verbose mode, see a demo on regex101.com.
The \Q...\E construct disables the ? and # "super-powers" (no need to escape them here).
In PHP this could be
<?php
$string = "1stparm?#?1111?#?2ndParm?#?2222?#?3rdParm?#?3333?#?4thparm?#?444?#?";
$regex = "~(?P<key>.+?)\Q?#?\E(?P<value>.+?)\Q?#?\E~";
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
echo $match["key"] . " = " . $match["value"] . "\n";
}
?>
Which yields
1stparm = 1111
2ndParm = 2222
3rdParm = 3333
4thparm = 444
Or shorter:
$result = array_map(
function($x) {return array($x["key"] => $x["value"]);}, $matches);
print_r($result);

Explode and assign it to a multi-dimensional array

I found this code in another post which I found quite helpful but for me it's only half the equation. In line with the following code, I need to take the string from a database, explode it into the 2d array, edit values in the array and implode it back ready for storage in the same format. So specifically backwards in the same order as the existing script.
The code from the other post >>
$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode(" \n ", $data);
$out = array();
$step = 0;
$last = count($data);
$last--;
foreach($data as $key=>$item){
foreach(explode(' ',$item) as $value){
$out[$key][$step++] = $value;
}
if ($key!=$last){
$out[$key][$step++] = ' '; // not inserting last "space"
}
}
print '<pre>';
print_r($out);
print '</pre>';
The quoted code inserts separate array elements which just have a space as value. One can wonder what benefit those bring.
Here are two functions you could use:
function explode2D($row_delim, $col_delim, $str) {
return array_map(function ($line) use ($col_delim) {
return explode($col_delim, $line);
}, explode($row_delim, $str));
}
function implode2D($row_delim, $col_delim, $arr) {
return implode($row_delim,
array_map(function ($row) use ($col_delim) {
return implode($col_delim, $row);
}, $arr));
}
They are each other's opposite, and work much like the standard explode and implode functions, except that you need to specify two delimiters: one to delimit the rows, and another for the columns.
Here is how you would use it:
$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$arr = explode2D(" \n ", " ", $data);
// manipulate data
// ...
$arr[0][2] = "scary";
$arr[2][2] = "balad";
// convert back
$str = implode2D(" \n ", " ", $arr);
See it run on repl.it.

PHP foreach and explode array code not functioning properly

<?php
$string = file_get_contents("csv.csv");
$array = explode(",", $string);
$q = strtolower($_GET[q]);
foreach ($array as $value) {
$result = explode(":", $value);
if (strpos($q, $result[0]) !== false) {
$output = $result[1];
}
}
echo $output;
?>
Here is the content of the file csv.csv which I am turning into a string.
hello: how are you doing,
hi: what are you,
df:df
If $_GET[q] (and $q) is hello, the $output is how are you doing. However, if it is hi, I do not get the output what are you or if I do df I do not get df.
Any reason why this is occuring? Thank you in advance for your kind help.
You are exploding by commas, but the truth is you have each value separated by comma plus line break.
After exploding, your array is ["hello","\nhi...","\ndf:..."], that's why there's no match for the strpos comparison.
try
$array = explode(",\n", $string);
Edit: as #Michael Berkowski said, you could also trim the parameter
if (strpos($q, trim($result[0])) !== false)
The order of the parameters depends on what kind of partial match do you want to offer. With your current parameter order, the parameter "hi" would match "hi", "h" and "i" but not "high".
If you flip them as Michael suggest, the parameter "hi" would match "hi" and "high" but not "h" or "i".
Instead of manually parsing a CSV use str_getcsv
Using str_getcsv and replacing $_GET[q] with $_GET['q'] fixes the issue
$csv = file_get_contents('csv.csv');
$array = str_getcsv($csv);
var_dump($array);
$q = $_GET['q'];
foreach ($array as $value) {
$result = explode(":", $value);
if (strpos($q, $result[0]) !== false) {
$output = $result[1];
}
}
echo $output;

PHP - split String in Key/Value pairs

I have a string like this:
key=value, key2=value2
and I would like to parse it into something like this:
array(
"key" => "value",
"key2" => "value2"
)
I could do something like
$parts = explode(",", $string)
$parts = array_map("trim", $parts);
foreach($parts as $currentPart)
{
list($key, $value) = explode("=", $currentPart);
$keyValues[$key] = $value;
}
But this seems ridiciulous. There must be some way to do this smarter with PHP right?
If you don't mind using regex ...
$str = "key=value, key2=value2";
preg_match_all("/([^,= ]+)=([^,= ]+)/", $str, $r);
$result = array_combine($r[1], $r[2]);
var_dump($result);
<?php parse_str(str_replace(", ", "&", "key=value, key2=value2"), $array); ?>
if you change your string to use & instead of , as the delimiter, you can use parse_str()
If you can change the format of the string to conform to a URL query string (using & instead of ,, among other things, you can use parse_str. Be sure to use the two parameter option.
Here's a single command solution using array_reduce formatted in multple lines for readability:
<?php
$str = "key=value, key2=value2";
$result = array_reduce(
explode(',', $str),
function ($carry, $kvp) {
list($key, $value)=explode('=', $kvp);
$carry[trim($key)]=trim($value);
return $carry;
}, []);

PHP: Split string into 2D array

I have a string which contains quite a bit of data. I want to split the data into a 2D array. The data in the string is split by a ~ (tilde) for the columns and a : (colon) for the different rows.
An example string could be: "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry".
Thanks.
$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$array = explode(':', $string);
foreach($array as &$value) $value = explode('~', $value);
Functional way (PHP 5.3.x needed):
$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$map = array_map(function($el) {
return explode('~', $el);
}, explode(':', $string));
Another alternative would be:
preg_match_all('/(.*?)~(.*?)~(.*?)~(.*?)(?:$|:)/', $string, $array,
PREG_SET_ORDER);
It's more cumbersome in that you have to predefine the column format. It also returns the complete match in each rows [0]. Otherwise (due to PREG_SET_ORDER) it's in your desired 2d format.
Just posting it here to please and annoy the microoptimizers at the same time. Despite the common Stackoverflow meme, the regex is three times faster than the explode loop.
You can split data in php with explode().
So first you have to split the string, than you have to split your entries again with explode().
$data = explode(':', $string);
$array = array();
foreach($data as $d) {
$d = explode('~', $d);
$array[] = $d; //e.g. $array[0][0] --> London
//$array[$d[0]] = array('temperature' => $d[1], 'dont-know' => $d[2], 'climate' => $d[3]); //e.g. $arra['London'] => array(...)
}
A "functional" style
$array = array_map(function($value) {
return explode('~', $value);
}, explode(':',$string););

Categories