php在两个值之间分割数组

发布于 2024-11-06 22:39:37 字数 894 浏览 4 评论 0原文

我有一个像这样的数组:

Array ( [0] => #!A1#DC [1] => #IMSR102.71/74.82 [2] => #HV50 [3] => #PR7/7/ [4] => #RX0 [5] => #ERN/1//0 [6] => #Q2 [7] => #!A1#DC [8] => #IMSR102.50/74.82 [9] => #HV40 [10] => #PR5/5/ [11] => #RX0 [12] => #ERN/1//1 [13] => #Q2 etc etc with hundreds o values

我从文件中获取这个数组(使用函数 file($filename) ),我需要将它拆分为许多子数组。

"!A1#DC" 这是以 #Q2 结尾的一系列值的开头,但开头和结尾之间的值的数量并不总是相同,唯一相同的 2 个值是给定的两个值 ( “!A1#DC”为开头,“#Q2”为结尾) 我怎样才能得到这样的东西?

Array ( 
[0] => Array (  [0] => #!A1#DC [1] => #IMSR102.71/74.82 [2] => #HV50 [3] => #PR7/7/ [4] => #RX0 [5] => #ERN/1//0 [6] => #Q2 ) 
    [1] => Array (
     [1] => #!A1#DC [2] => #IMSR102.50/74.82 [3] => #HV40 [4] => #PR5/5/ [5] => #RX0 [6] => #ERN/1//1 [7] => #Q2 etc etc

请你帮助我好吗?

谢谢

i have an array like:

Array ( [0] => #!A1#DC [1] => #IMSR102.71/74.82 [2] => #HV50 [3] => #PR7/7/ [4] => #RX0 [5] => #ERN/1//0 [6] => #Q2 [7] => #!A1#DC [8] => #IMSR102.50/74.82 [9] => #HV40 [10] => #PR5/5/ [11] => #RX0 [12] => #ERN/1//1 [13] => #Q2 etc etc with hundreds o values

i get this array from a file (with the function file($filename) ) and i need to split it in many subarray.

"!A1#DC" this is the beginning of a series of values ​​that ends with #Q2 but the number of the values between the beginning and the end is not always the same and the only 2 values that are same are the two given ("!A1#DC" for the beginning and "#Q2" for the end)
how can i get somethings like this?

Array ( 
[0] => Array (  [0] => #!A1#DC [1] => #IMSR102.71/74.82 [2] => #HV50 [3] => #PR7/7/ [4] => #RX0 [5] => #ERN/1//0 [6] => #Q2 ) 
    [1] => Array (
     [1] => #!A1#DC [2] => #IMSR102.50/74.82 [3] => #HV40 [4] => #PR5/5/ [5] => #RX0 [6] => #ERN/1//1 [7] => #Q2 etc etc

could you please help me?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清音悠歌 2024-11-13 22:39:37

循环遍历数组。当达到起始值时,存储它的索引。当遇到结束值时,使用 array_slice() 提取最后一对开始值和结束值之间的部分,并将这部分存储到另一个数组中。

$source = array (
    '#!A1#DC',
    '#IMSR102.71/74.82',
    '#HV50',
    '#PR7/7/',
    '#RX0',
    '#ERN/1//0',
    '#Q2',
    '#!A1#DC',
    '#IMSR102.50/74.82',
    '#HV40',
    '#PR5/5/',
    '#RX0',
    '#ERN/1//1',
    '#Q2',
);

$dest = array();

$startValue = '#!A1#DC';
$endValue = '#Q2';

$startIndex = 0;
foreach ( $source as $index => $value ) {
    if ( $value === $startValue ) {
        $startIndex = $index;
    } else
    if ( $value === $endValue ) {
        $dest[] = array_slice($source, $startIndex, $index - $startIndex + 1);
    }
}

print_r($dest);

Loop through an array. When you meet starting value, store it's index. When you meet ending value, use array_slice() to extract the part between the last pair of starting and ending values, store this part into another array.

$source = array (
    '#!A1#DC',
    '#IMSR102.71/74.82',
    '#HV50',
    '#PR7/7/',
    '#RX0',
    '#ERN/1//0',
    '#Q2',
    '#!A1#DC',
    '#IMSR102.50/74.82',
    '#HV40',
    '#PR5/5/',
    '#RX0',
    '#ERN/1//1',
    '#Q2',
);

$dest = array();

$startValue = '#!A1#DC';
$endValue = '#Q2';

$startIndex = 0;
foreach ( $source as $index => $value ) {
    if ( $value === $startValue ) {
        $startIndex = $index;
    } else
    if ( $value === $endValue ) {
        $dest[] = array_slice($source, $startIndex, $index - $startIndex + 1);
    }
}

print_r($dest);
养猫人 2024-11-13 22:39:37

基本上,您需要循环遍历 $input 的每个元素,将 STARTEND 元素中的元素收集到一个单独的数组中:

$input = array("#!A1#DC", "A", "B", "#Q2");
$values = array();
$current = 0;

define("START", "#!A1#DC");
define("END", "#Q2");
for ($i = 0; $i < count($input); $i++) {
    if ($input[$i] == END) {
        // Ignore any elements after this point until we see START
        $current = null;
    } else if ($input[$i] == START) {
        // Create a new current collection array
        $current = count($values);
        $values[$current] = array();
    } else {
        // Store the value if we are collecting
        if ($current !== null) {
            $values[$current][] = $input[$i];
        }
    }
}

Basically you need to loop through each element of $input, collecting those within START and END elements into a separate array:

$input = array("#!A1#DC", "A", "B", "#Q2");
$values = array();
$current = 0;

define("START", "#!A1#DC");
define("END", "#Q2");
for ($i = 0; $i < count($input); $i++) {
    if ($input[$i] == END) {
        // Ignore any elements after this point until we see START
        $current = null;
    } else if ($input[$i] == START) {
        // Create a new current collection array
        $current = count($values);
        $values[$current] = array();
    } else {
        // Store the value if we are collecting
        if ($current !== null) {
            $values[$current][] = $input[$i];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文