【问题标题】:Saving value in a different variable every time it loops through an array每次遍历数组时将值保存在不同的变量中
【发布时间】:2022-01-20 08:01:27
【问题描述】:

我有一个很大的列表具有相似的标题,我将这两个分解为数组。我想通过拼接在同一标题下插入数据并获得一个大数组示例:

Array1 (           Array2(           Result Array(
[0]Heading1           [0]Heading1             [0]Heading1  )  
[1]data1               [1]data1               [1]data1
[2]data1               [2]Heading2            [2]data1
[3]data1               [3]data2               [3]data1
[4]Heading2            [4]data2               [4]data1
[5]data2               [5]data2  )            [5]Heading2
[6]data2                                      [6]data2
[7]data2                                      [7]data2
[8]data2                                      [8]data2
[9]data2                                      [9]data2
)                                             [10]data2
                                              [11]data2
                                              [12]data2
                                              [13]data2 )

现在我已经匹配了标题值并希望在匹配值之间获取索引范围这是我尝试过的:

<?php

$original = array( '1','f','g','h','2','m','n','o','p', '3', '4', '5' );
$Test = array('1','a','b','c','d','e','2','i','j','k','l');
foreach ($original as $key1 => $value1) {   
    foreach ($Test as $key2 => $value2) {
                
                if($value1 == $value2 ){

                        if(isset($firsttime)){}else{$firsttime = ($key1+1);} 
                        if(isset($secondtime)){}else{$secondtime = ($key1-1);} 
                }
            }       
 }
 
 foreach(range(firsttime,secondtime) as $key){
    echo $original[$key];
   }
 echo $firsttime.'<br>';
 echo $secondtime.'<br>';
?>

第一个值在索引 0 处匹配,所以我得到值 1 和 -1 我想要的值是 1和3

【问题讨论】:

  • 数据的原始来源是什么,这看起来很复杂,通过构建不同的起始数组集可能更容易解决。
  • google sheet data 我已经用 , 分隔符加入了列,并将它分解成一个数组。工作表:docs.google.com/spreadsheets/d/…
  • 第二个数组中是否可以包含第一个数组中没有的标题?如果是这样,您究竟想如何处理这些?丢弃它们,在最后附加它们?
  • 如果第二个数组中的所有标题都保证在第一个数组中,那么我会简单地将数据分组,首先使用标题内容作为键(假设标题是唯一的),然后再次展平生成的数组。示例:3v4l.org/4v7rV 我在这里使用is_numeric 来“识别”示例数据中的标题,您需要将其替换为适合您实际数据的任何内容。第一个数组的标题被插入到辅助数组中,第二个数组的标题不被插入(因为我们不希望它们在结果中重复。)
  • 第二个数组中的值保证在第一个数组中我试试这个acual数据中的标题是城市名称

标签: php arrays range


【解决方案1】:

我在谷歌表格的两栏中有这个认证和机构列表:https://docs.google.com/spreadsheets/d/1XrQiNJzQckUdoeR6vhegoXFDpECSzqYtTMIyGGuNwVI/edit#gid=0

我必须使用逗号分隔的每个单元格中的相同标题和值来解析这些数据。 首先,我手动将相同的标题添加到第二列,然后使用 =JOIN(Google 表格公式)获取单个大字符串(值以逗号分隔)并将两列中的字符串分解为两个单独的数组。 下面是我用于测试的两个静态数组,值来自 $original 将被输入 $test。

$original = array( 'Hello','f','g','h','Hello2','m','n','o','p','Hello3','s','t','Hello4','x','y','z','Hello5','3','4','5','Hello6' );
$Test= array('Hello','a','b','c','d','e','Hello2','i','j','k','l','Hello3','q','r','Hello4','u','v','w','Hello5','1','2','Hello6');
$index = array();
$index1 = array();
$index2 = array();


foreach ($original as $key1 => $value1) {   
    foreach ($Test as $key2 => $value2) {

        if($value1 == $value2 ) :

            array_push($index,$key1);
            array_push($index1,$key1);
            array_push($index2,$key2);
        }
    }                   
}

foreach($index as $key => $numb){
    if($key % 2 == 0){
        $index[$key] = ($numb + 1);
    }
    else{
        $index[$key] = ($numb - 1);
    }
}
foreach($index1 as $key => $numb){
    if($key % 2 == 0){
        $index1[$key] = ($numb - 1);
    }
    else{
        $index1[$key] = ($numb + 1);
    }
}

$indexf  = array_merge($index,$index1);
sort($indexf);

array_shift($indexf);

array_pop($indexf);

array_shift($index2);

$next = array();
$new = array();
$i = 0 ;
$count = count($indexf);
$countf = ($count / 2);

for($p=0;$p < $countf;$p++) { 

    $zucc = range($indexf[$i], $indexf[($i + 1)]);

    foreach($zucc as $keye => $value )
    {                                               

        if((count($zucc)-1) == ($keye)){

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

                $zucc[$key]=$original[$val];

                array_push($new, $val);
            }

            array_push($next,count($new));
            if($p==0){
                $honka = count($new);
                array_splice($Test,($index2[$p]),0,$zucc);
            }
            else{   
                array_splice($Test,($index2[$p]+$next[($p-1)]),0,$zucc);
            }

        }                       
    }

    $i=($i+2);
}



print_r($Test);

我想通了! prolly 有一种更简单的方法可以做到这一点:3 但我得到了我的结果:

Array
(
    [0] => Hello
    [1] => a
    [2] => b
    [3] => c
    [4] => d
    [5] => e
    [6] => f
    [7] => g
    [8] => h
    [9] => Hello2
    [10] => i
    [11] => j
    [12] => k
    [13] => l
    [14] => m
    [15] => n
    [16] => o
    [17] => p
    [18] => Hello3
    [19] => q
    [20] => r
    [21] => s
    [22] => t
    [23] => Hello4
    [24] => u
    [25] => v
    [26] => w
    [27] => x
    [28] => y
    [29] => z
    [30] => Hello5
    [31] => 1
    [32] => 2
    [33] => 3
    [34] => 4
    [35] => 5
    [36] => Hello6
)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2019-01-25
  • 2022-09-30
相关资源
最近更新 更多