将一个数组的值分配给另一个数组作为具有分隔符逻辑的键(认真思考)

Assigning value of an array to another array as key with a delimiter logical (hard thinking)

下午好:-)

嗯,我有两个这样的数组:

第一个数组

Array
(
    [0]  => DATA 1\n
    [1]  => DATA 2\n
    [2]  => DATA 3\n
    [3]  => =\n
    [4]  => DATA 4\n
    [5]  => DATA 5\n
    [6]  => DATA 6\n
    [7]  => DATA 7\n
    [8]  => =\n
    [9]  => DATA 8\n
    [10] => DATA 9\n
)

第二个数组

Array
(
    [0] => 567
    [1] => 568
    [2] => 569
)

如您所见,在我的第一个数组中,我有 = 元素作为分隔符,我的最终数组必须如下所示:

最终数组

Array
(
    [567] => Array
             (
                  [0] => DATA 1\n
                  [1] => DATA 2\n
                  [2] => DATA 3\n
             )

    [568] => Array
             (
                  [0] => DATA 4\n
                  [1] => DATA 5\n
                  [2] => DATA 6\n
                  [2] => DATA 7\n
             )

    [569] => Array
             (
                  [0] => DATA 8\n
                  [1] => DATA 9\n
             )
)

我正在使用此代码读取第一个数组并检测 = 分隔符,

for ($i = 0; $i < count($raw_data); $i++) {
    if ($raw_data[$i] == "=\n") {
        # Code here...
    }
}

但我对以下逻辑感到困惑:

我们将不胜感激。

谢谢。

这里有一个双 foreach 循环解决方案:

// Value array
$one    =   array(
0  => 'DATA 1\n',
1  => 'DATA 2\n',
2  => 'DATA 3\n',
3  => '=\n',
4  => 'DATA 4\n',
5  => 'DATA 5\n',
6  => 'DATA 6\n',
7  => 'DATA 7\n',
8  => '=\n',
9  => 'DATA 8\n',
10 => 'DATA 9\n');

// Key array
$two    =   array(567,568,569);

// Loop through to-be-keys array
foreach($two as $nums) {
        // Loop through your to-be-values array
        foreach($one as $key => $data) {
                // If value is not =\n
                if($data !== '=\n') {
                        // Save new array with assigned key and value
                        $new[$nums][]   =   $data;
                        // Unset keys as you go
                        unset($one[$key]);
                    }
                else {
                        // If you hit =\n, unset the value
                        //break the loop
                        unset($one[$key]);
                        break;
                    }
            }
    }

// Output new array
print_r($new);

输出:

Array
(
    [567] => Array
        (
            [0] => DATA 1\n
            [1] => DATA 2\n
            [2] => DATA 3\n
        )

    [568] => Array
        (
            [0] => DATA 4\n
            [1] => DATA 5\n
            [2] => DATA 6\n
            [3] => DATA 7\n
        )

    [569] => Array
        (
            [0] => DATA 8\n
            [1] => DATA 9\n
        )

)

1。使用 For-each 循环

    $inner = array();

    foreach($short_array as $key => $val){

        foreach($long_array as $k => $v){

           if($v == '=\n'){ continue; }

           $inner .= $key.'=>'.$v;

        }

    }

这里的表现可能不太好

你可以这样做:

// $raw_data = 1st Array
// $keys = 2nd Array

$output = array();
foreach ($raw_data as $data) {
    if ($data == '=\n') {
        next($keys);
        continue;
    }
    $output[current($keys)][] = $data;
}