Separate string by array - php

I have no idea on how to do this, it'd probably use an explode but that'd not do exactly as I wanted.
Say I have a string:
a,1,1,0,0;0,1,1,0,0;r,1,1,0,0;
and I have an array:
a = atv
p = 330c
U = blista
c = BMW
D = bobcat
r = charger
I'd like it so that when it's called as a function on that string like seperateString($string); that it'd return something like "atv, charger"
I've tried some searching but to no avail have I found something which would return a string specific to the array.

Try this:
$string = "a,1,1,0,0;0,1,1,0,0;r,1,1,0,0;";
$array = array('a' => 'atv',
'p' => '330c',
'U' => 'blista',
'c' => 'BMW',
'D' => 'bobcat',
'r' => 'charger');
function separateString($array, $string) {
$result = array();
$chunks = preg_split('/[,;]/', $string);
foreach ($chunks as $chunk) {
if (array_key_exists($chunk, $array)) {
$result[] = $array[$chunk];
}
}
return $result;
}
print_r(separateString($array, $string));
Outputs:
Array
(
[0] => atv
[1] => charger
)
codepad example

The information you posted wasn't that great to go off of, but here's a start:
$string = "a,1,1,0,0;0,1,1,0,0;r,1,1,0,0;";
class Utility
{
protected static $classes = array(
'a' => 'atv',
'p' => '330c',
'U' => 'blista',
'c' => 'BMW',
'D' => 'bobcat',
'r' => 'charger'
);
public static function separateString($string)
{
$groups = explode(';', $string);
foreach ($groups as $group) {
$classId = array_shift(explode(',', $group));
if (array_key_exists($classId, self::$classes)) {
$classes[] = self::$classes[$classId];
}
}
return (isset($classes)) ? $classes : array();
}
}
$classes = Utility::separateString($string);
var_dump($classes);
/*
array(2) {
[0]=>
string(3) "atv"
[1]=>
string(7) "charger"
}
*/

Related

Convert array index in a custom string layout?

I have an array called $context with this structure:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "Foo"
["username"]=>
string(6) "Test"
}
[1]=>
array(2) {
["name"]=>
string(4) "John"
["username"]=>
string(3) "Doe"
}
}
I want convert it into this string:
string 1:
0: array(
'name' => 'Foo',
'username' => 'Test',
)
string 2:
1: array(
'name' => 'John',
'username' => 'Doe',
)
How you can see I want save the current index in the iteration and display the array content formatted as 'name' and 'username' in a single line. I already tried with this code:
$export = '';
foreach($context as $key => $value)
{
$export .= "{$key}: ";
print_r($value);
$export .= preg_replace(array(
'/=>\s+([a-zA-Z])/im',
'/array\(\s+\)/im',
'/^ |\G /m'
), array(
'=> $1',
'array()',
' '
), str_replace('array (', 'array(', var_export($value, true)));
print_r($export);
$export .= PHP_EOL;
}
return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
but I'm looking for a more optimized solution, any suggest?
This is my code:
$context = [['name'=>'Foo','username'=>'Test'],['name'=>'John','username'=>'Doe']];
$schema = " '%s' => '%s'";
$lineBreak = PHP_EOL;
foreach( $context as $idx => $array )
{
$lines = array();
foreach( $array as $key => $val )
{
$lines[] = sprintf( $schema, $key, $val );
}
$output = "{$idx}: array({$lineBreak}".implode( ",{$lineBreak}", $lines )."{$lineBreak})";
echo $output.$lineBreak;
}
3v4l.org demo
It will works independently from the number of elements in sub-arrays
I have used classic built-in function sprintf to format each array row: see more.
You can change $lineBreak with you preferred endLine character;
In the above example, each string is printed, but (you have a return in your function, so i think inside a function), you can modify in this way:
$output = array();
foreach( $context as $idx => $array )
{
(...)
$output[] = "{$idx}: array({$lineBreak}".implode( ",{$lineBreak}", $lines )."{$lineBreak})";
}
to have an array filled with formatted string.
You can easly transform it in a function:
function contextToString( $context, $schema=Null, $lineBreak=PHP_EOL )
{
if( !$schema ) $schema = " '%s' => '%s'";
$output = array();
foreach( $context as $idx => $array )
{
$lines = array();
foreach( $array as $key => $val )
{
$lines[] = sprintf( $schema, $key, $val );
}
$output[] = "{$idx}: array({$lineBreak}".implode( ",{$lineBreak}", $lines )."{$lineBreak})";
}
return implode( $lineBreak, $output );
}
to change each time the schema and the line break.
PS: I see that in you code there is a comma also at the end of the last element of eache array; thinking it was a typo, I have omitted it
Edit: I have forgot the comma, added-it.
Edit 2: Added complete function example.
Edit 3: Added link to sprintf PHP page
Try this with personnal toString
$a = array(array("name"=>"Foo", "username"=>"Test"), array("name"=>"John", "username"=>"Doe"));
function toString($array){
$s = ""; $i=0;
foreach ($array as $key => $value) {
$s.= $key."=>".$value;
if($i < count($array)-1)
$s.=",";
$i++;
}
return $s;
}
$result = array();
$index = 0;
foreach ($a as $value) {
array_push($result, $index. " : array(" . toString($value).")");
$index ++;
}
var_dump($result);
And the result :
array (size=2)
0 => string '0 : array(name=>Foo,username=>Test)' (length=35)
1 => string '1 : array(name=>John,username=>Doe)' (length=35)
The result is in an array but you can change and make what you want
But you can also use json_encode :
$result = array();
$index = 0;
foreach ($a as $value) {
array_push($result, $index. " : array(" . json_encode($value).")");
$index ++;
}
var_dump($result);
With this result :
array (size=2)
0 => string '0 : array({"name":"Foo","username":"Test"})' (length=43)
1 => string '1 : array({"name":"John","username":"Doe"})' (length=43)
Simplified solution with strrpos,substr_replace and var_export:
$arr = [
array(
'name' => 'John',
'username' => 'Doe'
),
array(
'name' => 'Mike',
'username' => 'Tyson'
)
];
/*****************/
$strings = [];
foreach($arr as $k => $v){
$dump = var_export($v, true);
$last_comma_pos = strrpos($dump,",");
$cleared_value = substr_replace($dump, "", $last_comma_pos, 1);
$strings[] = $k.": ".$cleared_value;
}
/*****************/
// Now $strings variable contains all the needed strings
echo "<pre>";
foreach($strings as $str){
echo $str . "\n";
}
// the output:
0: array (
'name' => 'John',
'username' => 'Doe'
)
1: array (
'name' => 'Mike',
'username' => 'Tyson'
)

Reverse flatted array to multidimensional

In another thread i asked about flatten an array with a specific style to get something like this:
array(4) {
["one"]=> string(9) "one_value"
["two-four"]=> string(10) "four_value"
["two-five"]=> string(10) "five_value"
["three-six-seven"]=> string(11) "seven_value"
}
I've got some very good help there, but now im wondering how would i reverse this method to get again the same original array:
array(
'one' => 'one_value',
'two' => array
(
'four' => 'four_value',
'five' => 'five_value'
),
'three' => array
(
'six' => array
(
'seven' => 'seven_value'
)
)
)
I've tried with recursive method but with no luck.
I thank all the help in advance!
function expand($flat) {
$result = array();
foreach($flat as $key => $val) {
$keyParts = explode("-", $key);
$currentArray = &$result;
for($i=0; $i<count($keyParts)-1; $i++) {
if(!isSet($currentArray[$keyParts[$i]])) {
$currentArray[$keyParts[$i]] = array();
}
$currentArray = &$currentArray[$keyParts[$i]];
}
$currentArray[$keyParts[count($keyParts)-1]] = $val;
}
return $result;
}
Note that the code above is not tested and is given only to illustrate the idea.
The & operator is used for $currentArray to store not the value but the reference to some node in your tree (implemented by multidimensional array), so that changing $currentArray will change $result as well.
Here is an efficient recursive solution:
$foo = array(
"one" => "one_value",
"two-four" => "four_value",
"two-five" => "five_value",
"three-six-seven" => "seven_value"
);
function reverser($the_array) {
$temp = array();
foreach ($the_array as $key => $value) {
if (false != strpos($key, '-')) {
$first = strstr($key, '-', true);
$rest = strstr($key, '-');
if (isset($temp[$first])) {
$temp[$first] = array_merge($temp[$first], reverser(array(substr($rest, 1) => $value)));
} else {
$temp[$first] = reverser(array(substr($rest, 1) => $value));
}
} else {
$temp[$key] = $value;
}
}
return $temp;
}
print_r(reverser($foo));
strstr(___, ___, true) only works with PHP 5.3 or greater, but if this is a problem, there's a simple one-line solution (ask if you'd like it).

How to filter an array by a part of key?

I have an array like this:
Array(
[id-1] => N
[nm-1] => my_val_01;
[id-48] => S
[nm-48] => my_val_02;
[id-52] => N
[nm-52] => my_val_03;
[id-49] => N
[nm-49] => my_val_04;
[id-50] => N
[nm-50] => my_val_05;
}
and would like to filter by part of the key. In this case I would like to have all keys that have "id-", and to result in this:
Array(
[id-1] => N
[id-48] => S
[id-52] => N
[id-49] => N
[id-50] => N
}
Can anyone help?
foreach (array_keys($array) as $key) {
if (!preg_match('/^id-\d+/', $key)) {
unset($array[$key]);
}
}
this way you can do.
$arr = Array(
[id-1] => N
[nm-1] => my_val_01;
[id-48] => S
[nm-48] => my_val_02;
[id-52] => N
[nm-52] => my_val_03;
[id-49] => N
[nm-49] => my_val_04;
[id-50] => N
[nm-50] => my_val_05;
};
$new = array();
foreach($arr as $key => $value){
if(stripos($key, "id-") !== false){
$new[$key] = $value;
}
}
//so $new is your required array here
Use the following (untested):
function filter($key) {
return substr($key, 0, 3) == 'id-';
}
$keys = array_filter(array_keys($array), 'filter');
if(empty($keys))
$array = array();
else
$array = array_intersect_key(array_flip($keys), $input);
You can write a little function that works analog to array_filter:
<?php
$a = array(
'id-1' => 'N', 'nm-1' => 'my_val_01', 'id-48' => 'S', 'nm-48' => 'my_val_02', 'id-52' => 'N',
'nm-52' => 'my_val_03', 'id-49' => 'N', 'nm-49' => 'my_val_04', 'id-50' => 'N', 'nm-50' => 'my_val_05'
);
$b = array_filter_key($a, function($e) {
return 0===strpos($e, 'id-');
});
var_dump($b);
function array_filter_key($source, $callback) {
// TODO: check arguments
$keys = array_keys($source);
$keys = array_filter($keys, $callback);
return ( 0===count($keys) ) ?
array()
:
array_intersect_key(array_flip($keys), $source)
;
}
prints
array(5) {
["id-1"]=>
int(0)
["id-48"]=>
int(2)
["id-52"]=>
int(4)
["id-49"]=>
int(6)
["id-50"]=>
int(8)
}
Try with:
$input = array( /* your data */ );
$output = array();
$pattern = 'id-';
foreach ( $input as $key => $value ) {
if ( strpos($key, $pattern) === 0 ) { # Use identical not equal operator here
$output[$key] = $value;
}
}
Answers, on the internet, that utilize array_intersect_key are INcorrect.
They Filter Right, but Output "filtered keys index" values instead of input array values.
Here is a fixed version
/**
* Filter array keys by callback
*
* #param array $input
* #param $callback
* #return NULL|unknown|multitype:
*/
function array_filter_keys($input, $callback)
{
if (!is_array($input)) {
trigger_error('array_filter_key() expects parameter 1 to be array, ' . gettype($input) . ' given', E_USER_WARNING);
return null;
}
if (empty($input)) {
return $input;
}
$filteredKeys = array_filter(array_keys($input), $callback);
if (empty($filteredKeys)) {
return array();
}
$input = array_intersect_key($input, array_flip($filteredKeys));
return $input;
}
If you're using the latest PHP version:
$filtered_arr = array_filter($arr, 'filter_my_array', ARRAY_FILTER_USE_KEY);
function filter_my_array( $key ) {
return preg_match("/^id-\d+/", $key);
}

Array: set value using dot notation?

Looking into Kohana documentation, i found this really usefull function that they use to get values from a multidimensional array using a dot notation, for example:
$foo = array('bar' => array('color' => 'green', 'size' => 'M'));
$value = path($foo, 'bar.color', NULL , '.');
// $value now is 'green'
Im wondering if there is a way to set the an array value in the same way:
set_value($foo, 'bar.color', 'black');
The only way i found to do that is re-building the array notation ($array['bar']['color']) and then set the value.. using eval.
Any idea to avoid eval?
function set_val(array &$arr, $path,$val)
{
$loc = &$arr;
foreach(explode('.', $path) as $step)
{
$loc = &$loc[$step];
}
return $loc = $val;
}
Sure it's possible.
The code
function set_value(&$root, $compositeKey, $value) {
$keys = explode('.', $compositeKey);
while(count($keys) > 1) {
$key = array_shift($keys);
if(!isset($root[$key])) {
$root[$key] = array();
}
$root = &$root[$key];
}
$key = reset($keys);
$root[$key] = $value;
}
How to use it
$foo = array();
set_value($foo, 'bar.color', 'black');
print_r($foo);
Outputs
Array
(
[bar] => Array
(
[color] => black
)
)
See it in action.
Look at https://gist.github.com/elfet/4713488
$dn = new DotNotation(['bar'=>['baz'=>['foo'=>true]]]);
$value = $dn->get('bar.baz.foo'); // $value == true
$dn->set('bar.baz.foo', false); // ['foo'=>false]
$dn->add('bar.baz', ['boo'=>true]); // ['foo'=>false,'boo'=>true]
That way you can set the following values ​​more than once to the same variable.
You can make these two ways (by static variable and reference variable):
<?php
function static_dot_notation($string, $value)
{
static $return;
$token = strtok($string, '.');
$ref =& $return;
while($token !== false)
{
$ref =& $ref[$token];
$token = strtok('.');
}
$ref = $value;
return $return;
}
$test = static_dot_notation('A.1', 'A ONE');
$test = static_dot_notation('A.2', 'A TWO');
$test = static_dot_notation('B.C1', 'C ONE');
$test = static_dot_notation('B.C2', 'C TWO');
$test = static_dot_notation('B.C.D', 'D ONE');
var_export($test);
/**
array (
'A' =>
array (
1 => 'A ONE',
2 => 'A TWO',
),
'B' =>
array (
'C1' => 'C ONE',
'C2' => 'C TWO',
'C' =>
array (
'D' => 'D ONE',
),
),
*/
function reference_dot_notation($string, $value, &$array)
{
static $return;
$token = strtok($string, '.');
$ref =& $return;
while($token !== false)
{
$ref =& $ref[$token];
$token = strtok('.');
}
$ref = $value;
$array = $return;
}
reference_dot_notation('person.name', 'Wallace', $test2);
reference_dot_notation('person.lastname', 'Maxters', $test2);
var_export($test2);
/**
array (
'person' =>
array (
'name' => 'Wallace',
'lastname' => 'Maxters',
),
)
*/
I created a small class just for this!
http://github.com/projectmeta/Stingray
$stingray = new StingRay();
//To Get value
$stingray->get($array, 'this.that.someother'):
//To Set value
$stingray->get($array, 'this.that.someother', $newValue):
Updated #hair resins' answer to cater for:
When a sub-path already exists, or
When a sub-path is not an array
function set_val(array &$arr, $path,$val)
{
$loc = &$arr;
$path = explode('.', $path);
foreach($path as $step)
{
if ( ! isset($loc[$step]) OR ! is_array($loc[$step]))
$loc = &$loc[$step];
}
return $loc = $val;
}
None of the examples here worked for me, so I came up with a solution using eval() (read about the risks here, but if you don't use user data, it shouldn't be much of an issue). The if-clause in the set-method allows you to push your item onto a new or existing array at that location ($location[] = $item).
class ArrayDot {
public static function get(array &$array, string $path, string $delimiter = '.') {
return eval("return ".self::getLocationCode($array, $path, $delimiter).";");
}
public static function set(array &$array, string $path, $item, string $delimiter = '.') : void {
//if the last character is a delimiter, allow pushing onto a new or existing array
$add = substr($path, -1) == $delimiter ? '[]': '';
eval(self::getLocationCode($array, $path, $delimiter).$add." = \$item;");
}
public static function unset(array &$array, $path, string $delimiter = '.') : void {
if (is_array($path)) {
foreach($path as $part) {
self::unset($array, $part, $delimiter);
}
}
else {
eval('unset('.self::getLocationCode($array, $path, $delimiter).');');
}
}
public static function isSet(array &$array, $path, string $delimiter = '.') : bool {
if (is_array($path)) {
foreach($path as $part) {
if (!self::isSet($array, $part, $delimiter)) {
return false;
}
}
return true;
}
return eval("return isset(".self::getLocationCode($array, $path, $delimiter).");");
}
private static function getLocationCode(array &$array, string $path, string $delimiter) : string {
$path = rtrim($path, $delimiter); //Trim trailing delimiters
$escapedPathParts = array_map(function ($s) { return str_replace('\'', '\\\'', $s); }, explode($delimiter, $path));
return "\$array['".implode("']['", $escapedPathParts)."']";
}
}
Example usage:
echo '<pre>';
$array = [];
ArrayDot::set($array, 'one.two.three.', 'one.two.three.');
ArrayDot::set($array, 'one.two.three.four.', 'one.two.three.four.');
ArrayDot::set($array, 'one.two.three.four.', 'one.two.three.four. again');
ArrayDot::set($array, 'one.two.three.five.', 'one.two.three.five.');
ArrayDot::set($array, 'one.two.three.direct set', 'one.two.three.direct set');
print_r($array);
echo "\n";
echo "one.two.three.direct set: ".print_r(ArrayDot::get($array, 'one.two.three.direct set'), true)."\n";
echo "one.two.three.four: ".print_r(ArrayDot::get($array, 'one.two.three.four'), true)."\n";
Output:
Array
(
[one] => Array
(
[two] => Array
(
[three] => Array
(
[0] => one.two.three.
[four] => Array
(
[0] => one.two.three.four.
[1] => one.two.three.four. again
)
[five] => Array
(
[0] => one.two.three.five.
)
[direct set] => one.two.three.direct set
)
)
)
)
one.two.three.direct set: one.two.three.direct set
one.two.three.four: Array
(
[0] => one.two.three.four.
[1] => one.two.three.four. again
)

Get array's key recursively and create underscore separated string

Right now i got an array which has some sort of information and i need to create a table from it. e.g.
Student{
[Address]{
[StreetAddress] =>"Some Street"
[StreetName] => "Some Name"
}
[Marks1] => 100
[Marks2] => 50
}
Now I want to create database table like which contain the fields name as :
Student_Address_StreetAddress
Student_Address_StreetName
Student_Marks1
Student_Marks2
It should be recursive so from any depth of array it can create the string in my format.
You can use the RecursiveArrayIterator and the RecursiveIteratorIterator (to iterate over the array recursively) from the Standard PHP Library (SPL) to make this job relatively painless.
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$key = $iterator->getSubIterator($i)->key() . '_' . $key;
}
$keys[] = $key;
}
var_export($keys);
The above example outputs something like:
array (
0 => 'Student_Address_StreetAddress',
1 => 'Student_Address_StreetName',
2 => 'Student_Marks1',
3 => 'Student_Marks2',
)
(Working on it, here is the array to save the trouble):
$arr = array
(
'Student' => array
(
'Address' => array
(
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => '100',
'Marks2' => '50',
),
);
Here it is, using a modified version of #polygenelubricants code:
function dfs($array, $parent = null)
{
static $result = array();
if (is_array($array) * count($array) > 0)
{
foreach ($array as $key => $value)
{
dfs($value, $parent . '_' . $key);
}
}
else
{
$result[] = ltrim($parent, '_');
}
return $result;
}
echo '<pre>';
print_r(dfs($arr));
echo '</pre>';
Outputs:
Array
(
[0] => Student_Address_StreetAddress
[1] => Student_Address_StreetName
[2] => Student_Marks1
[3] => Student_Marks2
)
Something like this maybe?
$schema = array(
'Student' => array(
'Address' => array(
'StreetAddresss' => "Some Street",
'StreetName' => "Some Name",
),
'Marks1' => 100,
'Marks2' => 50,
),
);
$result = array();
function walk($value, $key, $memo = "") {
global $result;
if(is_array($value)) {
$memo .= $key . '_';
array_walk($value, 'walk', $memo);
} else {
$result[] = $memo . $key;
}
}
array_walk($schema, 'walk');
var_dump($result);
I know globals are bad, but can't think of anything better now.
Something like this works:
<?php
$arr = array (
'Student' => array (
'Address' => array (
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => array(),
'Marks2' => '50',
),
);
$result = array();
function dfs($data, $prefix = "") {
global $result;
if (is_array($data) && !empty($data)) {
foreach ($data as $key => $value) {
dfs($value, "{$prefix}_{$key}");
}
} else {
$result[substr($prefix, 1)] = $data;
}
}
dfs($arr);
var_dump($result);
?>
This prints:
array(4) {
["Student_Address_StreetAddress"] => string(11) "Some Street"
["Student_Address_StreetName"] => string(9) "Some Name"
["Student_Marks1"] => array(0) {}
["Student_Marks2"] => string(2) "50"
}
function getValues($dataArray,$strKey="")
{
global $arrFinalValues;
if(is_array($dataArray))
{
$currentKey = $strKey;
foreach($dataArray as $key => $val)
{
if(is_array($val) && !empty($val))
{
getValues($val,$currentKey.$key."_");
}
else if(!empty($val))
{
if(!empty($strKey))
$strTmpKey = $strKey.$key;
else
$strTmpKey = $key;
$arrFinalValues[$strTmpKey]=$val;
}
}
}
}

Categories