PHP在数组中添加一个数组

时间:2016-06-03 14:20:10

标签: php arrays

我正在创建一个这样的数组:

foreach($communitiesArray as $row => $value)
{
        $newArray[]['Project'] = $value; 
}

给了我这个:

[0] => Array
        (
            [Project] => Array
                (
                    [ExternalProjectID] => 53
                    [ProjectName] => Doon Creek
                    [Address] => 123 Fake St
                    [City] => Toronto
                    [Province] => ON
                    [Latitude] => 43.0000
                    [Longitude] =>  -80.0000
                    [Website] => http://www.website.com/our-communities.php?newcommunity=53
                    [ContactPhone] => 555-5555
                    [ContactEmail] => email@email.com
                    [SalesOfficeAddress] => 123 Fake St
                    [SalesOfficeCity] => Toronto
                    [SalesOfficeProvince] => ON
                )

        )

我想在这个数组中做的是创建另一个名为Location的数组,并在这个名为Location的数组中包含Address,City,Province,Latitude和Longitude,它将位于Project数组中。怎么做到这一点?

更新

我尝试了以下内容:

foreach($communitiesArray as $row => $value) {
        $newArray[]['Project'] = $value;
        $newArray[]['Project']['Location'] = array (
    'Address'   => $Address,
    'City'      => $City,
    'Province'  => $Province,
    'Latitude'  => $Latitude,
    'Longitude' => $Longitude
);
}


[0] => Array
            (
                [Project] => Array
                    (
                        [ExternalProjectID] => 53
                        [ProjectName] => Doon Creek
                        [Address] => 123 Fake St
                        [City] => Toronto
                        [Province] => ON
                        [Latitude] => 43.0000
                        [Longitude] =>  -80.0000
                        [Website] => http://www.website.com/our-communities.php?newcommunity=53
                        [ContactPhone] => 555-5555
                        [ContactEmail] => email@email.com
                        [SalesOfficeAddress] => 123 Fake St
                        [SalesOfficeCity] => Toronto
                        [SalesOfficeProvince] => ON
                    )

            )
[1] => Array
        (
            [Project] => Array
                (
                    [Location] => Array
                        (
                            [Address] => 
                            [City] => 
                            [Province] => 
                            [Latitude] => 
                            [Longitude] => 
                        )

                )

        )

5 个答案:

答案 0 :(得分:2)

假设原始$value包含数组本身,您可以执行以下操作:

$locationKeys = array('Address', 'City', 'Province', 'Latitude', 'Longitude');
$newArray = array();

//going over all the projects
foreach($communitiesArray as $projects) {

    $project = array('Location' => array());

    //Going over the keys and values of the current project
    foreach($projects as $key => $value) {
        //if the current key is the location info, we put it under Location
        if(in_array($key, $locationKeys)) {
            $project['Location'][$key] = $value;
        } else {
            $project[$key] = $value;
        }
    }

    $newArray[] = $project;
}

答案 1 :(得分:1)

如果我理解正确,那么您需要在数组中的project元素下使用location元素。此location元素也是一个关联数组,它将保存地址,城市等。如果这是正确的,您可以像这样实例化它:

$newArray[]['Project']['Location'] = array (
    'Address'   => $Address,
    'City'      => $City,
    'Province'  => $Province,
    'Latitude'  => $Latitude,
    'Longitude' => $Longitude
);

答案 2 :(得分:0)

这应该有效

foreach($communitiesArray as $row => $value)
    { 
            if($row == 'Address'){
               $newArray[]['Project']['Location']['Address'] = $value; 
            }elseif($row=="City"){
               $newArray[]['Project']['Location']['City'] = $value; 
            }
            elseif($row=="Province"){
               $newArray[]['Project']['Location']['Province'] = $value; 
            }
            elseif($row=="Latitude"){
               $newArray[]['Project']['Location']['Latitude'] = $value; 
            }
            elseif($row=="Longitude"){
               $newArray[]['Project']['Location']['Longitude'] = $value; 
            }else{
               $newArray[]['Project'] = $value; 
            }
    }

答案 3 :(得分:0)

您需要检查$value数组中的每个出现情况,以便您可以决定要对该数组中的每个条目执行什么操作。

只需构建2个新的临时数组,最后将它们放在一起并将它们添加到$newArray

foreach($communitiesArray as $row => $value) {

    $t1 = array();
    $t2 = array();

    foreach ( $value as $name => $val ) {

        switch ($name) {
            case 'Address':
            case 'City':
            case 'Province':
            case 'Latitude':
            case 'Longitude':
                $t1[$name] = $value;
                break;
            default:
                $t2[$name] = $value;
        }
        $t2['Location'] = $t1;
        $newArray[]['Project'] = $t2;
}

答案 4 :(得分:0)

// List of keys for new Location 
$keys = array_flip(['Address', 'City', 'Province', 'Latitude', 'Latitude',    'Longitude']);

$newArray[] = array_merge (
       //  Those not in list
       array_diff_key($communitiesArray[0]['Project'], $keys),
       // And those in list
       array('Location' => array_intersect_key($communitiesArray[0]['Project'], $keys)));