kemosabe kemosabe - 3 months ago 8
PHP Question

combine portions of two arrays together

I've got a script that tests whether a service is running or not, I'm making some adjustments to it and have gotten a little stuck. Originally the script had one array that it ran over based on data provided in the url. It looks like:

$request = array(
"pe" => $_REQUEST['number'],
"key" => "1234",
"City" => "$city",
"State" => "$abbr",
"fname" => "First",
"lname" => "Last",
"ZipCode" => "$zip",
"Email" => "test@test.test",
"Phone" => "2225550000",
);


I have a second array (multidimensional) which looks like:

$states = array(
"California" => array(
"abbr" => "CA",
"city" => "Sacramento",
"zip" => "95632"
),
"Washington" => array(
"abbr" => "WA",
"city" => "Seattle",
"zip" => "98101"
),
);


I am wanting to loop over the first array for all states that I have in the second array but I want to replace the values in the original array with different values from the second array.

I am looping over the $states array by:

foreach ($states as $state => $details) {
if ($request['City'] == $details['city'] AND $request['State'] == $details['abbr'] AND $request['ZipCode'] == $details['zip'] AND $request['County'] == $details['county'] AND $request['PropertyCity'] == $details['city'] AND $request['PropertyState'] == $details['abbr'] AND $request['PropertyZip'] == $details['zip']) {
$state = $details['state'];
$abbr = $details['abbr'];
$city = $details['city'];
$county = $details['county'];
$zip = $details['zip'];
}


and using those variable for the $request array.

Currently I am getting a result that looks like the following when it runs through the $request array only:

"pe" => "20",
"key" => "1234",
"City" => "New York",
"State" => "New York",
"fname" => "First",
"lname" => "Last",
"ZipCode" => "$zip",
"Email" => "test@test.test",
"Phone" => "2225550000",


I need it to loop over the $request array for every state with the new values from the $states arrays and return a similar result for every state.

Once I have it loop through the arrays "combined" I don't get any results in the city and state fields.

The result I am looking to get is:

$result = array(
array(
"pe" => $_REQUEST['number'], // 2
"key" => "1234",
"city" => "Sacramento",
"state" => "California",
"fname" => "John",
"lname" => "Doe",
"ZipCode" => "95632",
"Email" => "jdoe@email.com",
"phone" => "2225550000",
),
array(
"pe" => $_REQUEST['number'], // 2
"key" => "1234",
"city" => "Seattle",
"state" => "Washington",
"fname" => "John",
"lname" => "Doe",
"ZipCode" => "98101",
"Email" => "jdoe@email.com",
"phone" => "2225550000",
)
);

Answer

You don't need to combine your array. This solution will not fit your problem because your arrays doesn't share keys, You need something like this:

foreach($states as $stateName => $stateInfo) {
    if($request['City'] == $stateInfo['city']) AND $request['State'] == $stateInfo['abbr'] AND $request['ZipCode'] == $stateInfo['zip']) {
        $state   =   $details['state'];
        $abbr    =   $details['abbr'];
        $city    =   $details['city'];
        $county  =   $details['county'];
        $zip     =   $details['zip'];
    }
}

This code compares request info and if something match City, State, Zipcode those vars ($state, $abbr, $city, $county, $zip) will be filled.

Comments