'cascading' explode string in php - php

How to explode this string :
00004.00001.00003.00001.00003
in an array like this :
array (size=3)
0 => string '00004' (length=5)
1 => string '00004.00001' (length=10)
2 => string '00004.00001.00003' (length=15)
3 => string '00004.00001.00003.00001.00003' (length=20)
Thx

$array = explode('.', '00004.00001.00003.00001.00003');
foreach($array as $key => $value) {
if($array[$key - 1]) {
$array[$key] = $array[$key - 1] . '.' . $value;
}
}

Explode it first as you would normally do $arr = explode('.', $str); and then build desired array with two loops.

Another version using array_map() :
$arr = explode('.', '00004.00001.00003.00001.00003');
$arr[] = 'blank';
$array = array_map(function(){
global $arr;
array_pop($arr);
return implode($arr);
}, $arr);

Related

Set multidimensional array in PHP

I have string
$s = 'controller/front/home';
value
$v = "some value";
and array
$a = [];
What is the best way to make multidimensional array like that?
$a['controller']['front']['home'] = $v;
[edit]
I don't know how many parts (separated by /) string $s can have, so manual array building by exploded parts is the last option I would consider.
I've found this after a small research:
PHP dynamic array path access
By this article, I've adapted it to following
$path = 'controller/front/home';
$value = 'some value';
$parts = explode('/', $path);
$arrayPath = [];
$temp = &$arrayPath;
foreach($parts as $part) {
if(!isset($temp[$part])) {
$temp[$part] = [];
}
$temp = &$temp[$part];
}
$temp = $value;
var_dump($arrayPath);
Prints:
array (size=1)
'controller' =>
array (size=1)
'front' =>
array (size=1)
'home' => string 'some value' (length=10)
$a = $v; foreach (array_reverse(explode('/', $s)) as $i) $a = array($i => $a);
You can do it like this
$array = explode('/', $s);
and then :
$a[$array[0]][$array[1]][$array[2]] = $v;

Extract numbers separated by special char from string

If I have a string like:
123*23*6594*2*-10*12
How can I extract the single numbers, in the string separated by *? That is, I want this output:
a=123, b=23, c=6594, d=2, e=-10, f=12.
Flexible:
$vars = range('a', 'z');
$vals = explode('*', $string);
$result = array_combine(array_slice($vars, 0, count($vals)), $vals);
Result:
Array
(
[a] => 123
[b] => 23
[c] => 6594
[d] => 2
[e] => -10
[f] => 12
)
Just for the sheer fun of setting the result as an iterable (rather than an actual array) with the alpha keys in a slightly different manner:
$data = '123*23*6594*2*-10*12';
function dataseries($data) {
$key = 'a';
while (($x = strpos($data, '*')) !== false) {
yield $key++ => substr($data, 0, $x);
$data = substr($data, ++$x);
}
yield $key++ => $data;
}
foreach(dataseries($data) as $key => $value) {
echo $key, ' = ', $value, PHP_EOL;
}
Requires PHP >= 5.5.0
You can use simple as this :
$str = "123*23*6594*2*-10*12";
$arr = explode("*",$str);
$arr['a']=$arr[0];
$arr['b']=$arr[1];
$arr['c']=$arr[2];
$arr['d']=$arr[3];
$arr['e']=$arr[4];
$arr['f']=$arr[5];
echo "a=" .$arr['a'] ." b=". $arr['b']." c=". $arr['c']." d=". $arr['d']." e=". $arr['e']." f=". $arr['f'];
EDIT:
To accomplish Rizier123 wish :
$str = "123*23*6594*2*-10*12";
$arr = explode("*",$str);
$vars = range('a', 'z');
foreach ($arr as $val => $key){
echo $vars[$val]."=".$key."<br>";
}
something like:
list($a,$b,$c,$d,$e,$f) = explode("*", $str);
Just a guess ;)

Split a string into keys of associative arrays (PHP)

Do you have an idea, how can a string be converted to a variable, e.g.
there is a string -> $string = 'one|two|three';
there is an array -> $arr = array('one' => array('two' => array('three' => 'WELCOME')));
I want to use all with explode(); exploded values to access the array $arr. I tried this code:
$c = explode('|', $string);
$str = 'arr[' . implode('][', $c) . ']';
echo $$str;
It doesnt work, sadly :( Any ideas?
You're doing it wrong.
You can do what you want with a loop to go through the array level by level
$string = 'one|two|three';
$arr = array('one' => array('two' => array('three' => 'WELCOME')));
$c = explode('|', $string);
$result = $arr;
foreach($c as $key)
$result = $result[$key];
echo $result; // WELCOME
Here's a sort of recursive function:
$ex_keys = array('one', 'two', 'three');
$ex_arr = array('one' => array('two' => array('three' => 'WELCOME')));
function get_recursive_var($keys, $arr) {
if (sizeof($keys) == 1)
return $arr[$keys[0]];
else {
$key = array_shift($keys);
return get_recursive_var($keys, $arr[$key]);
}
}
var_dump(get_recursive_var($ex_keys, $ex_arr));

Explode only by last delimiter

is there a way to use explode function to explode only by last delimiter occurrence?
$string = "one_two_ ... _three_four";
$explodeResultArray = explode("_", $string);
Result Should Be:
echo $explodeResultArray[0]; // "one_two_three ...";
echo $explodeResultArray[1]; // "four";
Straightforward:
$parts = explode('_', $string);
$last = array_pop($parts);
$parts = array(implode('_', $parts), $last);
echo $parts[0]; // outputs "one_two_three"
Regular expressions:
$parts = preg_split('~_(?=[^_]*$)~', $string);
echo $parts[0]; // outputs "one_two_three"
String reverse:
$reversedParts = explode('_', strrev($string), 2);
echo strrev($reversedParts[0]); // outputs "four"
There is no need for a workaround. explode() accepts a negative limit.
$string = "one_two_three_four";
$part = implode('_', explode('_', $string, -1));
echo $part;
Result is
one_two_three
I chose to use substring becasue you want a string up to a particular point:
$string = "one_two_three_four_five_six_seven";
$part1 = substr("$string",0, strrpos($string,'_'));
$part2 = substr("$string", (strrpos($string,'_') + 1));
var_dump($part1,$part2);
RESULTS:
string(27) "one_two_three_four_five_six"
string(5) "seven"
You could do the following:
$string = "one_two_three_four";
$explode = explode('_', $string); // split all parts
$end = '';
$begin = '';
if(count($explode) > 0){
$end = array_pop($explode); // removes the last element, and returns it
if(count($explode) > 0){
$begin = implode('_', $explode); // glue the remaining pieces back together
}
}
EDIT:
array_shift should have been array_pop
<?php
$lastPos = strrpos($string, '_');
if ($lastPos !== false) {
$start = substr($string, 0, $lastPos);
$end = substr($string, $lastPos+1);
} else {
// no delimeter found!
}
If you only care about the last part, it's even simpler.
<?php
$end = substr(strrchr($string, '_'), 1);
Use preg_match()
$string = "one_two_three_four";
$arr = array();
preg_match("/(^.*)_(.*?)$/", $string, $arr);
print_r($arr);
Output: Array ( [0] => one_two_three_four [1] => one_two_three [2] => four )
use end + explode
$explodeResultArray = end(explode("_", $string));
$explodeResultArray will = four
// reverse $string right after definition
$string = "one_two_three_four_five_six";
$string = implode("_",array_reverse(explode("_",$string)));
// chop off the first part
list($result, $string) = explode("_", $string, 2);
echo "$result --- $string";
Output:
six --- five_four_three_two_one
$explodeResultArray = explode("_", $string);
$last_item = end($explodeResultArray);
$key = count($explodeResultArray) - 1;
unset($explodeResultArray[$key]);
$arr[] = (implode($explodeResultArray,'_'));
$arr[] = $last_item;
print_r($arr);
Output
Array
(
[0] => one_two_ ... _three
[1] => four
)
I had similar needs and inspired by #NLZ's answer I've made a reusable function with the same features as regular explode(), but backwards (although I added an option to reverse the array order contra regular explode()):
function backward_explode($delimiter, $string, $limit = null, $keep_order = true) {
if ((string)$delimiter === "") {
return false;
}
if ($limit === 0 || $limit === 1) {
return array($string);
}
$explode = explode($delimiter, $string);
if ($limit === null || $limit === count($explode)) {
return $keep_order? $explode : array_reverse($explode);
}
$parts = array();
if ($limit > 0) {
for ($i = 1; $i < $limit; $i++) {
$parts[] = array_pop($explode);
}
$remainder = implode($delimiter, $explode);
$parts[] = $remainder;
if ($keep_order) {
$parts = array_reverse($parts);
}
} else {
if (strpos($string, $delimiter) === false) {
return array();
}
$parts = $explode;
array_splice($parts, 0, abs($limit));
if (!$keep_order) {
$parts = array_reverse($parts);
}
}
return $parts;
}
(Also as a gist.)
So with:
$string = 'one two three four';
var_dump(backward_explode(' ', $string));
var_dump(backward_explode(' ', $string, 2));
var_dump(backward_explode(' ', $string, 3));
var_dump(backward_explode(' ', $string, 2, false));
var_dump(backward_explode(' ', $string, -1));
var_dump(backward_explode(' ', $string, 1)); // same as with $limit = 0
var_dump(backward_explode('#', $string, -2));
var_dump(backward_explode('', $string, 3));
We get:
array (size=4)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
2 => string 'three' (length=5)
3 => string 'four' (length=4)
array (size=2)
0 => string 'one two three' (length=13)
1 => string 'four' (length=4)
array (size=3)
0 => string 'one two' (length=7)
1 => string 'three' (length=5)
2 => string 'four' (length=4)
array (size=2)
0 => string 'four' (length=4)
1 => string 'one two three' (length=13)
array (size=3)
0 => string 'two' (length=3)
1 => string 'three' (length=5)
2 => string 'four' (length=4)
array (size=1)
0 => string 'one two three four' (length=18)
array (size=0)
empty
boolean false
For such a taks, you can just use strstr and strrchr:
$valueBeforeLastUnderscore = rtrim(strrev(strstr(strrev($value), '_')), '_');
$valueAfterLastUnderscore = ltrim(strrchr($value, '_'), '_');
That being said, I like the regular expression answer more.
simply use sub string
substr($string ,stripos($string ,'_'))

Sort PHP array by date

I have a bunch of files that are in an array
im(month)(day)(year)
im01012007
im01022006
im01022012
im01032011
im01042010
im01042012
im01052009
im01052011
im01062012
im01072008
im01072011
etc..
is there a way to sort the array so that it sorted by year then month then day?
You can try use usort
$string = "im01012007.jpg,im01022006.jpg,im01022012.jpg,im01032011.jpg,im01042010.jpg,im01042012.jpg,im01052009.jpg,im01052011.jpg,im01062012.jpg,im01072008.jpg,im01072011.jpg";
$array = explode(",", $string); // rebulding your array ;
var_dump($array);
usort($array, function ($a, $b) {
$a = DateTime::createFromFormat("mdY", substr(pathinfo($a, PATHINFO_FILENAME), 2));
$b = DateTime::createFromFormat("mdY", substr(pathinfo($b, PATHINFO_FILENAME), 2));
return ($a == $b) ? 0 : (($a < $b) ? - 1 : 1);
});
var_dump($array);
Output
Before
array
0 => string 'im01012007.jpg' (length=14)
1 => string 'im01022006.jpg' (length=14)
2 => string 'im01022012.jpg' (length=14)
3 => string 'im01032011.jpg' (length=14)
4 => string 'im01042010.jpg' (length=14)
5 => string 'im01042012.jpg' (length=14)
6 => string 'im01052009.jpg' (length=14)
7 => string 'im01052011.jpg' (length=14)
8 => string 'im01062012.jpg' (length=14)
9 => string 'im01072008.jpg' (length=14)
10 => string 'im01072011.jpg' (length=14)
After
array
0 => string 'im01022006.jpg' (length=14)
1 => string 'im01012007.jpg' (length=14)
2 => string 'im01072008.jpg' (length=14)
3 => string 'im01052009.jpg' (length=14)
4 => string 'im01042010.jpg' (length=14)
5 => string 'im01032011.jpg' (length=14)
6 => string 'im01052011.jpg' (length=14)
7 => string 'im01072011.jpg' (length=14)
8 => string 'im01022012.jpg' (length=14)
9 => string 'im01042012.jpg' (length=14)
10 => string 'im01062012.jpg' (length=14)
Yes there is. Convert it to a sensible format first.
I suggest using PHP's DateTime class and keeping all your dates as DateTime objects while you're in the PHP program.
If you don't want to do that, here's a function to sort them in as-is.
usort($myDates, function($a, $b) {
$aBits = array_chunk($a,2);
$bBits = array_chunk($a,2);
$aDate = $aBits[3].$aBits[4].$aBits[2].$aBits[1];
$bDate = $aBits[3].$aBits[4].$aBits[2].$aBits[1];
return ($aDate < $bDate) ? -1 : 1;
});
Hope that helps.
Use substr() and divide your string into parte (day,month,year). Then You can sort them.
No built in function will do this out of the box, i would use usort like this:
usort($files, function($a, $b) {
return strcmp(preg_replace('/^.*(\d{2})(\d{2})(\d{4})$/', '$3$1$2', $a), preg_replace('/^.*(\d{2})(\d{2})(\d{4})$/', '$3$1$2', $b));
});
This will sort oldest to newest. Use krsort to sort newest to oldest.
$files = array();
if ($handle = opendir('/path/to/files')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$y = substr($entry, 6, 4);
$m = substr($entry, 2, 2);
$d = substr($entry, 4, 2);
$files[$y . $m . $d] = $entry;
}
}
closedir($handle);
}
ksort($files);
I used these PHP Manual entries to write this solution:
readdir
ksort
substr

Categories