Reputation: 11851
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 protected]
[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 protected]
[SalesOfficeAddress] => 123 Fake St
[SalesOfficeCity] => Toronto
[SalesOfficeProvince] => ON
)
)
[1] => Array
(
[Project] => Array
(
[Location] => Array
(
[Address] =>
[City] =>
[Province] =>
[Latitude] =>
[Longitude] =>
)
)
)
Upvotes: 0
Views: 2549
Reputation: 26153
// 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)));
Upvotes: 0
Reputation: 94642
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;
}
Upvotes: 0
Reputation: 1736
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;
}
Upvotes: 2
Reputation: 3485
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;
}
}
Upvotes: 0
Reputation: 1124
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
);
Upvotes: 2