使用PHP进行数组划分问题

时间:2017-04-22 07:48:59

标签: php arrays sorting arraylist multidimensional-array

我有一个非常小的问题,但我无法解决这个问题。

我有以下数组:

Array
(
    [0] => Array
        (
            [Asset Address 1 Label] => 
            [Asset Address 1 Organization] => 
            [Asset Address 1 Name prefix] => Hotel Taj
            [Asset Address 1 First name] => Maan
            [Asset Address 1 Middle name] => Singh
            [Asset Address 1 Last name] => Hotel
            [Asset Address 1 Name suffix] => 
            [Asset Address 1 Street] => 
            [Asset Address 1 Street 2] => 
            [Asset Address 1 Zip/Postal Code] => 110020
            [Asset Address 1 City] => 
            [Asset Address 1 State] => 
            [Asset Address 1 State Combined code] => IN-DL
            [Asset Address 1 Country ISO2 code] => IN
            [Asset Address 1 Types 1 Name] => billing
            [Asset Address 1 Types 2 Name] => 
            [Asset Address 2 Label] => Himachal
            [Asset Address 2 Organization] => 
            [Asset Address 2 Name prefix] => 
            [Asset Address 2 First name] => Hotel
            [Asset Address 2 Middle name] => 
            [Asset Address 2 Last name] => Taj
            [Asset Address 2 Name suffix] => 
            [Asset Address 2 Street] => 
            [Asset Address 2 Street 2] => 
            [Asset Address 2 Zip/Postal Code] => 110020
            [Asset Address 2 City] => 
            [Asset Address 2 State] => 
            [Asset Address 2 State Combined code] => IN-CT
            [Asset Address 2 Country ISO2 code] => IN
            [Asset Address 2 Types 1 Name] => shipping
            [Asset Address 2 Types 2 Name] => 
        )

)

我正试图打破内部资产地址1& 2进入数组中的另一个维度

    Array
    (

    [0] => Array
        (
        [0] => Array
            (
            [Asset Address 1 Label] => asas
            [Asset Address 1 Organization] => 
            [Asset Address 1 Name prefix] => 
            [Asset Address 1 First name] => asas
            [Asset Address 1 Middle name] => asasas
            [Asset Address 1 Last name] => asasa
            [Asset Address 1 Name suffix] => asasa
            [Asset Address 1 Street] => asasa
            [Asset Address 1 Street 2] => 
            [Asset Address 1 Zip/Postal Code] => 121212
            [Asset Address 1 City] => 
            [Asset Address 1 State] => 
            [Asset Address 1 State Combined code] => AO-CUS
            [Asset Address 1 Country ISO2 code] => AO
            [Asset Address 1 Types 1 Name] => billing
            [Asset Address 1 Types 2 Name] => shipping
            )
        [1] => Array
            (
            [Asset Address 2 Label] => 
            [Asset Address 2 Organization] => 
            [Asset Address 2 Name prefix] => 
            [Asset Address 2 First name] => 
            [Asset Address 2 Middle name] => 
            [Asset Address 2 Last name] => 
            [Asset Address 2 Name suffix] => 
            [Asset Address 2 Street] => 
            [Asset Address 2 Street 2] => 
            [Asset Address 2 Zip/Postal Code] => 
            [Asset Address 2 City] => 
            [Asset Address 2 State] => 
            [Asset Address 2 State Combined code] => 
            [Asset Address 2 Country ISO2 code] => 
            [Asset Address 2 Types 1 Name] => 
            [Asset Address 2 Types 2 Name] =>
            )

        )

    )

请注意,因为数组中的其他字段未显示在上方,因此我必须尝试使用​​正则表达式进行过滤,如下所示:

        $filtred = array();
        foreach($csv as $oldkey => $value){
            $regExpression = '/(Asset Address )['.$oldkey.'] [\w]+/';
            foreach ($value as $key => $value) {
                if(preg_match($regExpression , $key)){
                    $filtred[$oldkey][$key] = $value;
                }
            }
        }
        print_r($filtred);

问题:

$oldkey为0时,它基本上不在数组的开头,它会跳过$oldkey1。为什么呢?

此外,通过这种方法,我正在创建一个单独的数组,我试图将其作为现有数组中的额外维度。

我希望我能解释清楚。

1 个答案:

答案 0 :(得分:0)

如上所述,尝试使用以下代码进行格式化。

$filtred = array();
$n = 1;
foreach($csv as $oldkey => $values){
    $i = 0; $j = 0;
    foreach (array_keys($values) as $key => $value) {
        if(!preg_match('/(Asset Address )['.$n.'] [\w]+/' , $value)){
            $n++; $j++;
        }
        $filtred[$i][$j][$value] = $values[$value];
    }
    $i++;
}