将两个数组的部分组合在一起[关闭]

将两个数组的部分组合在一起[关闭]

问题描述:

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" => "San Antonio", // This will be overwritten by data from the states array
    "State" => "Texas", // This will be overwritten by data from the states array
    "fname" => "John",
    "lname" => "Doe",
    "ZipCode" => "78201", // This will be overwritten by data from the states array
    "Email" => "jdoe@email.com",
    "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 $states array but I want to replace the values in the $request, (where noted), array with values from the $states 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:

    $current = array(
         "pe" => "2",
         "key" => "1234",
         "City" => "Sacramento",
         "State" => "California",
         "fname" => "John",
         "lname" => "Doe",
         "ZipCode" => "95632",
         "Email" => "jdoe@email.com",
         "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.

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",
    )
);

So basically what's happening is that it works and values are getting replaced but it's only happening once, I need to have two arrays returned as I provided in my $result array.

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.