PHP在数组中添加一个数组

PHP在数组中添加一个数组,php,arrays,Php,Arrays,我正在创建一个数组,如下所示: foreach($communitiesArray as $row => $value) { $newArray[]['Project'] = $value; } 这就给了我: [0] => Array ( [Project] => Array ( [ExternalProjectID] => 53

我正在创建一个数组,如下所示:

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的数组中包含地址、城市、省份、纬度和经度,该数组将位于项目数组中。我们将如何做到这一点

更新

我尝试了以下方法:

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] => 
                        )

                )

        )
$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;
}

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

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

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

$newArray[]['Project']['Location'] = array (
    'Address'   => $Address,
    'City'      => $City,
    'Province'  => $Province,
    'Latitude'  => $Latitude,
    'Longitude' => $Longitude
);
这应该行得通

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; 
            }
    }
这应该行得通

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; 
            }
    }

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

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] => 
                        )

                )

        )
$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;
}

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

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] => 
                        )

                )

        )
$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;
}

您需要检查
$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;
}

您需要检查
$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;
}

你想通过Foreach循环还是通过Foreach循环的外侧添加新的位置数组?@GokulShinde我猜是通过Foreach循环还是通过Foreach循环的外侧添加新的位置数组?@GokulShinde我猜是通过Foreach循环。