简体   繁体   English

PHP - 数组中的数组中的数组,等等

[英]PHP - Array within an Array within an Array, and so on

I'm working with a complicated Array and ultimately my goal is to get it into one simple array so I can insert each [record] into my database.我正在处理一个复杂的数组,最终我的目标是将它放入一个简单的数组中,以便我可以将每个 [record] 插入到我的数据库中。

Given the complexity of the array, I can't wrap my head around how to flatten it out.鉴于数组的复杂性,我无法理解如何将其展平。 I want to omit the [links] array and flatten out insert each [record] into the databse.我想省略 [links] 数组并将每个 [record] 插入到数据库中。

    Array
(
    [records] => Array
        (
            [0] => Array
                (
                    [links] => Array
                        (
                            [0] => Array
                                (
                                    [href] => linkgoeshere
                                    [rel] => self
                                )

                        )

                    [record] => Array
                        (
                            [id] => 8ca9df0a2dfc72f3983ffc55c1dd1b9f459ae2fa
                            [timestamp] => 2020-09-28T10:30:59.98Z
                            [size] => 825
                            [fields] => Array
                                (
                                    [projectdescription] => Description Goes Here
                                    [projectvalue] => 650000
                                    [applicant] => Applicant name
                                    [specificusecategory] => Category
                                    [issuedate] => 2020-09-01
                                    [propertyuse] => Array
                                        (etail Uses
                                            [0] => R
                                            [1] => Transportation and Storage Uses
                                        )

                                    [buildingcontractoraddress] => Address Here
                                    [buildingcontractor] => ABC Co
                                    [typeofwork] => ASDF
                                    [year] => 2020
                                    [permitnumber] => 123456
                                    [bi_id] => 123456
                                    [applicantaddress] => address 
                                    [address] => address
                                )

                        )

                )

        )

)

I currently cannot test, but I hope that there are no syntax errors here:我目前无法测试,但我希望这里没有语法错误:

function flatten($source, &$target){ // could also use call by reference for source for better memory management, but not needed
  foreach(array_keys($source) as $key){ // inspect all keys on current array
    if(is_array($source[$key])){ // recursively call for sub-array
      flatten($source[$key], $target);
    }else{ // copy value to new array, maybe add more logic for duplicates here as this code just overrides them
      $target[$key] = $source[$key];
    }
  }
}

Call this code with $source being the input and $target being the output.使用$source作为输入和$target作为输出调用此代码。 As the output is used as call by reference all function calls access the same output array.由于输出用作引用调用,因此所有函数调用都访问相同的输出数组。

You may add further logic to eg exclude the link array as you wanted.您可以添加进一步的逻辑,例如根据需要排除链接数组。

Full demo code showing how to flatten an array:显示如何展平数组的完整演示代码:

<?php 
// Include this go get more warnings etc.
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

function flatten($source, &$target){ // could also use call by reference for source for better memory management, but not needed
  foreach(array_keys($source) as $key){ // inspect all keys on current array
    if(is_array($source[$key])){ // recursively call for sub-array
      flatten($source[$key], $target);
    }else{ // copy value to new array, maybe add more logic for duplicates here as this code just overrides them
      $target[$key] = $source[$key];
    }
  }
}

$input =   Array( 0 => Array ("a" => 1, "b" => 2, "c" => 3), "hello" => "world", 2 => Array());

print_r($input);

$output = Array();

flatten($input, $output);

echo "<br />";

print_r($output);

This gives me the output:这给了我输出:

Array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3 ) [hello] => world [2] => Array ( ) )
Array ( [a] => 1 [b] => 2 [c] => 3 [hello] => world ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM