PHP add an array inside an array

岁酱吖の 提交于 2019-12-13 07:36:29

问题


I am creating an array like so:

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

which gives me this:

[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
                )

        )

What I am trying to do inside this array is create another array called Location and have the Address, City, Province, Latitude and Longitude inside this array called Location which will be inside the Project array. How would accomplish this?

UPDATE

I tried the following:

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

                )

        )

回答1:


Assuming that your original $value contains the array itself you can do the following:

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



回答2:


If I am understanding you correctly, you are wanting a location element under the project element in your array. And this location element is also an associative array that will hold address, city, etc. If this is correct, you can instantiate it like this:

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



回答3:


This should works

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



回答4:


You will need to check each occurance in the $value array so you can decide what you want to do with each entry in that array.

Simply build 2 new temporary arrays, and at the end put them together and add them to your $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;
}



回答5:


// 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)));


来源:https://stackoverflow.com/questions/37616832/php-add-an-array-inside-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!