Cut string by substrings after none left - php

I have a string Field-Text-Datepicker. I need to "explode" it to following array:
array(
[0] => field-text-datepicker
[1] => field-text
[2] => field
);
I've tried some combinations of strrchr(), recursion and for loops, but everything I've made seem to be crazily complicated and ineffective. Is there some simple way I don't see? If not, I will post the mess I've already written. :)
Why do I need it?
For better code organisation, I sometimes need to declare multiple classes per one file. This is a problem for my SPL autoloader, that loads files according to class names. Because of that, I need to get every possible filename to load from most probable to the least.
Thanks in advance! :)

Use array_slice() with variable offsets:
$arr = explode('-', strtolower($str));
for ($i = 1, $c = count($arr); $i < $c; $i++) {
$result[] = implode('-', array_slice($arr, 0, -$i));
}
Demo

There you go!
I just tested and it prints exactly what you need =)
<?php
$str = "Field-Text-Datepicker";
$str = strtolower($str);
$array = explode('-',$str);
$count = count($array);
$output_array = [];
for($i=1;$i<=$count;$i++)
$output_array[$count-$i] = implode('-',array_slice($array,0,$i));
var_dump($output_array);
?>

Related

Vigenère table in PHP

I am trying to make a Vigenère table using PHP. My goal is to make a big array with 26 smaller arrays in it like this:
$bigarray = [['a'-'z']['b'-'a']...['y'-'x']['z'-'y']];
I'm thinking of making the first array using the range() function, append that in the big array, then use a for loop to take the first letter, place that letter at the end and make that array append in a big array x25
$letterarray = range('a','z');
array_merge($bigarray, $firstarray);
for ($idx = 0; $idx < 26; $idx++) {
$letterarray = /* Take first letter from $letterarray, put that letter in the end. */
$bigarray = /* Put the $letterarray into the $bigarray. */
I don't know if I need to use the array_splice() or array_slice() function. I also don't know how to put the small array into the big array while keeping the 'array in array' form, because array_merge() just shoves every value into one array.
Your approach is solid. To execute, you just need to copy the previous array, and then use array_shift and array_push to "cycle" it.
$bigarray = [range('a','z')];
for( $i=1; $i<26; $i++) {
// $i=1 because we already have the first one.
$copy = $bigarray[$i-1]; // get most recent entry
array_push($copy,array_shift($copy));
$bigarray[$i] = $copy;
}
Thanks for your comment, after I wrote this thread I figured out a way myself.
$bigarray = array();
$alphas = range('a', 'z');
$bigarray[0] = $alphas;
for ($idx = 1; $idx <= 25; $idx++) {
$firstletter = $alphas[0];
$alphas = array_slice($alphas,1);
array_push($alphas, $firstletter);
$bigarray[$idx] = $alphas;
}
It stores the first letter of the [a-z] array ($alphas) in the variable $firstletter, slices the $alphas array and pushes the element in the $firstletter varible at the end and stores the new array [b-a] into the $bigarray.
The neat thing is that array_slice just changes the indices automatically.
Thanks for the comment :)
-Ed

How to count and output variables named in a specific numeric pattern

I have created few different strings with similar names, and I would like to display them all.
However, I will need to do this dynamically, because I will adding more of them, later on.
These strings are called:
$group1
$group2
$group3
$group4
My idea is to somehow count them all, and then display them with for loop. I just need help with counting part.
Though arrays are definitely the superior and appropriate solution in this case, since you insist in the comments that separate variables are required, you can solve this using a while loop to check for the existence of such consecutively named variables, creating the variable names dynamically with {}.
For example:
$group1 = '123';
$group2 = '456';
$group3 = '789';
$i=1;
while ($string = ${'group'.$i}) {
echo $string;
$i++;
}
Note how ${'group'.$i} dynamically creates each variable name. Also, naturally this approach would fail if the variables are not named consecutively (e.g. if you have $group1 followed by $group3). As said, you should definitely use an array for this.
See a live demo
Associative arrays are designed exactly for this:
$arr = array(
"group1" => "string1",
"group2" => "string2",
"group3" => "string3",
"group4" => "string4",
);
Now to get the length of your array:
$num = count($arr);
To access the first element
$firstElement = $arr["group1"];
Alternatively you can use an indexed array (access elements by their position):
$arr = array("string1", "string2", "string3", "string4");
$firstElement = $arr[0];
$num = count($arr);
you can use this to count+loop
$arr=array();
$add_to=array_push($arr,$group1);
$add_to=array_push($arr,$group2);
$add_to=array_push($arr,$group3);
$add_to=array_push($arr,$group4);
//count
echo count($arr);
//loop
foreach($arr as $key=>$value){
echo $value;
}

What's the most efficient way to array_pop() the last n elements in an array?

What's an efficient way to pop the last n elements in an array?
Here's one:
$arr = range(1,10);
$n = 2;
$popped_array = array();
for ($i=0; $i < $n; $i++) {
$popped_array[] = array_pop($arr);
}
print_r($popped_array); // returns array(10,9);
Is there a more efficient way?
Use array_splice():
If you're trying to remove the last n elements, use the following function:
function array_pop_n(array $arr, $n) {
return array_splice($arr, 0, -$n);
}
Demo
If you want to retrieve only the last n elements, then you can use the following function:
function array_pop_n(array $arr, $n) {
array_splice($arr,0,-$n);
return $arr;
}
Demo
It's important to note, looking at the other answers, that array_slice will leave the original array alone, so it will still contain the elements at the end, and array_splice will mutate the original array, removing the elements at the beginning (though in the example given, the function creates a copy, so the original array still would contain all elements). If you want something that literally mimics array_pop (and you don't require the order to be reversed, as it is in your OP), then do the following.
$arr = range(1, 10);
$n = 2;
$popped_array = array_slice($arr, -$n);
$arr = array_slice($arr, 0, -$n);
print_r($popped_array); // returns array(9,10);
print_r($arr); // returns array(1,2,3,4,5,6,7,8);
If you require $popped_array to be reversed, array_reverse it, or just pop it like your original example, it's efficient enough as is and much more direct.
Why not use array_slice. You can give a start and a length, so if you do 2 from the end you will get the last two items in the array:
$arr = range(1,10);
$n = 2;
$start = count($arr) - $n;
print_r(array_slice($arr, $start, $n));
Thanks for the array_slice comments. I don't know why that didn't immediately come to mind.
It looks (to me) like the easiest way is:
$arr = range(1,10);
$n = 2;
$popped_array = array_slice($arr,-$n);
print_r($popped_array); // returns array(10,9);

Which is the best way to remove middle element of associative array in PHP?

Please tell me which is the best way to unset middle element of associative array in PHP?
Suppose I have an array of 10,000 elements and I want to remove middle element of that array, which is efficient way to remove middle element?
$temp = array('name1'=>'value1','name2'=>'value2',...,'name10000'=>'value10000');
$middleElem = ceil(count($temp) / 2);
$i = 0;
foreach ($temp as $key=>$val) {
if ($i == $middleElem) {
unset($temp[$key]);
break;
}
$i++;
}
Is above code efficient way?
Considering $array is your array, this code remove the middle element if it has odd number of elements. If its event it'll remove the first of 2 middle elements.
$i = round(count($array)/2) - 1;
$keys = array_keys($array);
unset ($array[$keys[$i]]);
Test Result: http://ideone.com/wFEM2
The thing you have to figure out is what you want to do when you have an array with an even number of elements. What element do you want to get then?
The above code picks the 'lower' element, the code could easily be edited to make it pick the 'higher' element. The only thing you have to check is (what all others answers failed to do) what happens if you have three elements. It doesn;t pick the middle element, but the last. So you would have to add a check for that then.
$temp = Array("name1"=>"value1","name2"=>"value2",...,"name10000"=>"value10000");
$middleElem = ceil(count($temp)/2);
$keys = array_keys($temp);
$middleKey = $keys[$middleElem];
unset($temp[$middleKey]);
There ^_^
I think it's a proper way to do it. Try this:
array_remove_at( $temp, ceil(count($temp) / 2) - 1);
function array_remove_at(&$array, $index){
if (array_key_exists($index, $array)) {
array_splice($array, $index, 1);
}
}
You can find the size of the array, divide that number by two and then proceed to remove the element. Not sure about the performance isssues about that though
Firstly, I wouldn't worry too much about what is the most efficient way at this point. You're much better off coding for how easy the code is to read, debug and change. Micro-optimisations like this rarely produce great results (as they're often not the biggest bottlenecks).
Having said that, if you want a solution that is easy to read, then how about using array_splice.
$temp = array('name1'=>'value1','name2'=>'value2',...,'name10000'=>'value10000');
$middleElem = ceil(count($temp) / 2);
array_splice( $temp, $middleElem, 1 );
I would take it that the following code is more efficient, because you don't have to execute it in a loop. I generally follow the same pattern as Kolink, but my version checks if there actually is a "middle element". Works on all types of arrays, I think.
<?php
for( $i = 0; $i <= 9; $i ++ ) {
$temp['name'.$i] = 'value'.$i;
}
if( ( $count = count( $temp ) ) % 2 === 0 ) {
/** Only on uneven arrays. */
array_splice( $temp, ( ceil( $count ) / 2 ), 1 );
}
var_dump( $temp );
EDIT: Thavarith seems to be right; array_splice is much faster than simply unsetting the value. Plus, you get the added benefit of not having to use array_keys, as you already now at what $offset the middle is.
Proper way seems to be:
unset(arr[id]);
arr = array_values(arr);
For first removing element at index id and then re-indexing the array arr properly.
unset($myArray[key])
since your array is associative, you can drop any element easily this way

Ordered array to associative array, odd values as keys

Pretty straightforward:
// turn
array('foo', 'bar', 'hello', 'world');
// into
array('foo' => 'bar', 'hello' => 'world');
Right now I'm using:
do{
$arrayOut[current($arrayIn)] = next($arrayIn);
}while(next($arrayIn));
I'm wondering if there's a way to do it without the intermediary variable, $arrayOut. I could write a function, however this is a single use case, and I'm trying to keep my script uncluttered. I'm just wondering if there's something I missed in the docs that would serve this purpose.
The values are coming from a routing path:
route/to/controller/action/key1/value1/key2/value2
It's exploded, and eventually after using the other components, I'm left with ('key1', 'value1', 'key2', 'value2', ...)
Thank you folks for the insight and suggestions. Long Ears won this one for the concise approach, which when expanded to more than "1 line", is not terribly cryptic (I don't think at least)
However, also with regard to Long Ears' suggestion, perhaps my desire for semantically precise code with minimal verbosity has gotten the better of me, and I was chasing daisies trying to keep my variable scope "pollutant-free", to paraphrase myself.
You can make your existing code slightly more efficient (removes one function call per iteration, for one at the beginning:)
$b = array();
array_unshift($a, false);
while (false !== $key = next($a)) {
$b[$key] = next($a);
}
Ok, (shudder) here's your one liner:
$b = call_user_func_array('array_merge', array_map(function($v) { return array($v[0] => $v[1]); }, array_chunk($a, 2)));
You do not need an array, you can do it directly with the path text:
$path = "foo/bar/hello/world";
preg_match_all("#(?J)(?<key>[^/]+)/(?<val>[^/]*)#", $path, $p);
$params = array_combine($p['key'], $p['val']);
print_r($params);
/*
Array
(
[foo] => bar
[hello] => world
)
*/
I don't think you can do better than what you have. But what about trying to put the odd and even elements in different arrays when your input is read? You could then use array_combine to make an one-liner.
Update: array_map won't help, as it will produce an array with an equal number of elements. You can do things with array_filter and array_combine, but again, that won't end up being shorter.
I really like what you have; short, simple, does the job. I 'd simply refactor it out into a function, let's say array_interleave_combine or some such.
Update 2:
Well, you asked for it, so here it is. Tries to be too clever if you ask me. Is an one-liner, but I really really don't think the "pureness" of this is enough to pay back the lost time someone would need to understand what's going on:
$result = array_reduce(
array('foo', 'bar', 'hello', 'world'),
function($partial, $item) {
static $nextKey;
if ($nextKey === null) {
$nextKey = $item;
}
else {
$partial[$nextKey] = $item;
$nextKey = null;
}
return $partial;
});
This solution does not need an additional array:
$arr = array('foo', 'bar', 'hello', 'world');
for($i = 0, $count = count($arr); $i < $count; $i += 2)
{
$arr[$arr[$i]] = $arr[$i + 1];
unset($arr[$i], $arr[$i + 1]);
}

Categories