Have an array with multiple data. Each array index has a string that is separated by a ">" symbol. Finally, it should display the last part of each index string.
Here is the current array. Please note the emoji is required.
(
[0] => val 1
[1] => val 1 > Search ➖
[2] => val 1 > Search ➖ > Plain search ✔
[3] => val 1 > val 2 ➖ > Val 3 ➖
[4] => val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖
[5] => val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖
[6] => val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ➖
[7] => val 1 > Search ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ✔
[8] => val 1 > val 2 ➖
[9] => val 1 > val 2 ➖ > Val 3 ✔
[10] => val 1 > val 2 ➖ > Val 3 ➖
)```
What should be the sorted output is,
```Array
(
[0] => val 1
[1] => Search ➖
[2] => Plain search ✔
[3] => Val 3 ➖
[4] => Val 4 ➖
[5] => Val 4 ➖
[6] => Val 5 ➖
[7] => Val 5 ✔
[8] => val 2 ➖
[9] => Val 3 ✔
[10] => Val 3 ➖
)```
What I have tried is,
```$x = ltrim(stristr($x, '>'), '>');
$x = ltrim(stristr($x, '>'), '>');
echo $x;
But this is only up to two ">"s. The number of ">" can't determine. It can be infinite. What is the best looping method to do this?
The issue once doing the following answers!
CodePudding user response:
You can use this code:
$x = trim(end(explode('>',$x)))
It first splits the string everywhere that the '>' character occours, than gets the last element of the splitted string and removes the leading and trailing whitespaces.
Splitting strings in PHP and get last part Here you can find some more examples.
Here is a more verbose example:
<?php
$search = [
"val 1",
"val 1 > Search ➖",
"val 1 > Search ➖ > Plain search ✔",
"val 1 > val 2 ➖ > Val 3 ➖",
"val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖",
"val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖",
"val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ➖",
"val 1 > Search ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ✔",
"val 1 > val 2 ➖",
"val 1 > val 2 ➖ > Val 3 ✔",
" val 1 > val 2 ➖ > Val 3 ➖",
];
$result = array_map(
function (string $item): string {
$splittedString = explode('>', $item);
$lastElement = end($splittedString);
$removedWhitespaceAtBeginningAndEnd = trim($lastElement);
return $removedWhitespaceAtBeginningAndEnd;
},
$search
);
print_r($result);
and here is the output
Array
(
[0] => val 1
[1] => Search ➖
[2] => Plain search ✔
[3] => Val 3 ➖
[4] => Val 4 ➖
[5] => Val 4 ➖
[6] => Val 5 ➖
[7] => Val 5 ✔
[8] => val 2 ➖
[9] => Val 3 ✔
[10] => Val 3 ➖
)
CodePudding user response:
You can do first a foreach, and next take the text after the last ">" with strrpos. Here you have an example. `
$search = array (
0 => "val 1",
1 => "val 1 > Search ➖",
2 => "val 1 > Search ➖ > Plain search ✔",
3 => "val 1 > val 2 ➖ > Val 3 ➖",
4 => "val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖",
5 => "val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖",
6 => "val 1 > val 2 ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ➖",
7 => "val 1 > Search ➖ > Val 3 ➖ > Val 4 ➖ > Val 5 ✔",
8 => "val 1 > val 2 ➖",
9 => "val 1 > val 2 ➖ > Val 3 ✔",
10 =>" val 1 > val 2 ➖ > Val 3 ➖"
) ;
$result = array ( ) ;
foreach ( $search as $row )
{
$result [ ] = str_replace ( "> " , "" , substr ( $row , strrpos ( $row , ">" ) ) ) ;
}
`
