Splitting a String after every nth element php - php

I have a string in this form
$navigation="<li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li>";
The list is generated dynamically from database and hence the number of elements is not fixed. I want to split this list after every 5th element, so if there are 10 "<li></li>" elements in this string I should get two strings output1 and output2.
I have tried explode but it doesn't work as we have to impose restriction on nth element and I have also tried string_split but that is not working also.
So what is the solution?
Ahmar

You can do it this way:
$s = "<li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li><li>A</li><li>B</li><li>C</li>";
if (preg_match_all('~((?:<li>.*?</li>){5})~i', $s, $arr))
print_r($arr);
OUTPUT:
Array
(
[0] => Array
(
[0] => <li>A</li><li>B</li><li>C</li><li>A</li><li>B</li>
)
[1] => Array
(
[0] => <li>A</li><li>B</li><li>C</li><li>A</li><li>B</li>
)
)

Related

Find the maximum and parent value in an array

I searched many thread but i can't find this solution
I have this Array
Array
( [0] => [1] => Array ( [0] => 2019-01-11T23:30:00CET [1] => -12.6 ) [2] => [3] => Array ( [0] => 2019-01-11T23:20:00CET [1] => -12.5 ) [4] => [5] => Array ( [0] => 2019-01-11T23:10:00CET [1] => -12.6 ) [10] => [11] => Array ( [0] => 2019-01-11T22:40:00CET [1] => -12.4 )
I found the path to have the maximum or minimum value ( Column [1] ) from this Array but i need to find the relative Parent
(example the minimum -12.6 is in the [1][0] as 2019-01-11T22:20:00CET)
of this two values that are show in the first column ( Column[0] )
Thanks
If you use array_column() to extract the second column of your data, then you can use min() or max() with that array to pick which one you want. This code then extracts the ones that match using a foreach() and if to check if it matches (not exactly sure what you want as output, but this should help)...
$source = [["2019-01-11T23:30:00CET", -12.6],
["2019-01-11T23:20:00CET", -12.5],
["2019-01-11T23:10:00CET", -12.6]
];
$extract = min(array_column($source, 1)); // or use max()
$output = [];
foreach ($source as $key => $element) {
if ( $element[1] == $extract ) {
// Matches, so add to output
$output[$key] = $element[0];
}
}
print_r($output);
will give
Array
(
[0] => 2019-01-11T23:30:00CET
[2] => 2019-01-11T23:10:00CET
)
You could use array_filter() to extract the matching rows, but a foreach() is enough for a straightforward thing like this (IMHO).
If there is a possibility of blank values or strings in the value column, this may confuse the min() as it will consider the values and compare them as strings, to ensure they are all compared as numbers you can add...
$values = array_map("floatval", array_column($source, 1));
$extract = min($values); // or use max()
The array_map("floatval",... goes through the list and converts them all to float values.
Also, here's a generalized algorithm-sketch for "finding the max in some array", expressed as pseudo-code:
"Leave quietly" if the array is empty, or throw an exception.
Otherwise, assume that the first element in the array is the biggest one.
Now, loop through the remaining elements, testing if each one is, in fact, bigger than the "biggest one" that you have so far. If so, select it as the "biggest."
When the loop is finished, return your answer.
Now – this is what a geek would call "an O(n) algorithm," which is to say that its execution-time will be "on the order of" the number of elements in the array. Well, if this is a "one-off" requirement, that's fine. Whereas if what you actually want to do is to get "more than one" max-element, sorting the array (once, then holding on to the sorted result ...) becomes significantly better, because the sort is going to be O(log(n)) ... "on the order of some logarithm of the number of elements," ... and the subsequent cost of "popping off" elements from that sorted array becomes non-existent.
There are other ways to do it, of course – trees and such - but I've already blathered-on too long here.

Slice an array in php

I have an array
print_r($myarray);
Array ( [0] => text one ) Array ( [0] => text two ) Array ( [0] => text three ) ...
When i use slice
print_r(array_slice($myarray,1,2));
Array ( ) Array ( ) Array ( ) Array ( ) ......
I get an empty array, how can i slice this array in half?
Thanks
You actually have an array that contains arrays which probably might be what is causing the issue for you. Though I do not see how you get the result you posted... It mighty be that you actually apply the slice function to each element of the output array. Then certainly you will get an empty array for each iteration. As to be expected, since each element you iterate over contains only a single element. So slicing from position 1 will result in an empty array each time...
Consider this simple example using a plain array:
<?php
$input = ["one", "two", "three", "four", "five", "six"];
$output = array_slice($input, 1, 2);
print_r($output);
The output is:
Array
(
[0] => two
[1] => three
)
So php's array_slice() function works just as expected...
Same with an array of arrays as you suggest in your post:
<?php
$input = [["one"], ["two"], ["three"], ["four"], ["five"], ["six"]];
$output = array_slice($input, 1, 2);
print_r($output);
The output of that is, as expected:
Array
(
[0] => Array
(
[0] => two
)
[1] => Array
(
[0] => three
)
)
Also considering your second comment below, that you have some words in a single string (which mighty be what you describe) I get a meaningful result myself:
<?php
$input = explode(' ', "one two three four five six");
$output = array_slice($input, 1, 2);
print_r($output);
The output, as expected, is:
Array
(
[0] => two
[1] => three
)

How to create an array from string seperated values?

I have an array like:
Array
(
[0] => Mechanical Engineering,CEO
)
Now I want to convert it in this form:
Array
(
[0] => Mechanical Engineering
[1] => CEO
)
You can use like below.
$str = 'Mechanical Engineering,CEO';
$arrayStr = explode(',',$str );
print_r($arrayStr);
Note: Assuming your input array has only one key "Mechanical Engineering,CEO".
We need current() function to access the array element.
Since you want to split your data every time there's a comma, we need explode()
function.
$input = array("Mechanical Engineering,CEO");
$output = explode(",", current($input));

php - multidimensional array regex search and print out partial array to look at structure

I have this array structure, it is stored in a variable $xxx
Array
(
[xyz] => Array
(
[1] => 3
[0] => s
)
[d2s] => Array
(
[a] => 96
[d] => 4
)
...
)
It is a long array, and I don't want to out put the whole thing, how do print only the first 5 (1st dimension) values along with the 2nd dimension values?
Secondly, if I want this array to contain only alphabets in the FIRST dimension, how do I either delete values that don't match that requirement or retain values that match the requirement? so that my final array would be
Array
(
[xyz] => Array
(
[1] => 3
[0] => s
)
...
)
TIA
To output only the first 5 elements, use array_slice:
array_slice($arr, 0, 5)
To remove any elements whose index contains non-alpha characters.
foreach ($arr AS $index => $value) {
// Remove the element if the index contains non-alpha characters
if (preg_match('/[^A-Za-z]/', $index))
unset($arr[$index]);
}
Check it out in action.

How to reset indexes in array_diff result?

I have two arrays: Array ( [0] => 2 [1] => 3 ) and Array ( [0] => 2 ).
I want to get the value, which is not in second array. So I have used the array_diff function but my result will get Array ( [1] => 3 )
Actually this is the result. But a small problem here, its position is (key) 1. I want the result in to a new array starts from 0th position, i.e., Array ( [0] => 3 ).
How can I achieve this?
you can use array_values(array_diff($arr1, $arr2)); if order doesn't matter
You should run array_values() on the result and this would give you a new array with indexes starting at 0.
This is a known shortcoming of array_diff(), check the php docs.

Categories