将两个阵列的部分组合在一起

时间:2016-07-29 00:23:10

标签: php arrays

我有一个测试服务是否正在运行的脚本,我对它进行了一些调整并且遇到了一些问题。最初,脚本有一个基于url中提供的数据运行的数组。它看起来像:

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

我有第二个数组(多维),看起来像:

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

我想在$ states数组中为所有状态循环第一个数组,但是我想用$ states数组中的值替换$ request中的值(如果注意到的话)。

我循环遍历$ states数组:

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

并将这些变量用于$ request数组。

目前,当我只通过$ request数组运行时,我得到的结果如下所示:

    $current = array(
         "pe" => "2",
         "key" => "1234",
         "City" => "Sacramento",
         "State" => "California",
         "fname" => "John",
         "lname" => "Doe",
         "ZipCode" => "95632",
         "Email" => "jdoe@email.com",
         "Phone" => "2225550000",
    )

我需要它使用$ states数组中的新值为每个状态循环$ request数组,并为每个状态返回类似的结果。

我希望获得的结果是:

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

所以基本上发生的事情是,它的工作和价值正在被取代,但它只发生一次,我需要返回两个数组,就像我在$ result数组中提供的那样。

1 个答案:

答案 0 :(得分:0)

您无需合并阵列。此解决方案不适合您的问题,因为您的阵列不共享密钥,您需要这样的东西:

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

此代码会比较请求信息,如果匹配City,State,Zipcode,那些vars($ state,$ abbr,$ city,$ county,$ zip)将被填充。