doulou1970 2016-07-29 00:23
浏览 15
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • duanchun1881 2016-07-29 00:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历