doumubi6784 2016-10-09 17:49
浏览 29
已采纳

尽量减少与foreach的努力

I'm trying to build a script that will download users from a db table and attach a new random IP to each user based on his state.

The problem is that I wrote a lot of code and there is still much Copy/Paste job to be done if I keep it with this approach.

Can someone point me to the right direction on how to properly do that?

So first I have 50 of these:

$California_Text = file_get_contents('state/California.txt');
$California_textArray = explode("
", $California_Text);
$Idaho_Text = file_get_contents('state/Idaho.txt');
$Idaho_textArray = explode("
", $Idaho_Text);
$Illinois_Text = file_get_contents('state/Illinois.txt');
$Illinois_textArray = explode("
", $Illinois_Text);
$Indiana_Text = file_get_contents('state/Illinois.txt');
$Indiana_textArray = explode("
", $Indiana_Text);
$Iowa_Text = file_get_contents('state/Iowa.txt');

Then I have 50 of these:

while($row = $result->fetch_assoc()) {

    if (isset($row["state"])) {

        foreach ($row as $value){
            $California_randArrayIndexNum = array_rand($California_textArray);
            $p_California = $California_textArray[$California_randArrayIndexNum];

            $Texas_randArrayIndexNum = array_rand($Texas_textArray);
            $p_Texas = $Texas_textArray[$Texas_randArrayIndexNum];

            $Alabama_randArrayIndexNum = array_rand($Alabama_textArray);
            $p_Alabama = $Alabama_textArray[$Alabama_randArrayIndexNum];

            $Alaska_randArrayIndexNum = array_rand($Alaska_textArray);
            $p_Alaska = $Texas_textArray[$Alaska_randArrayIndexNum];

            $Arizona_randArrayIndexNum = array_rand($Arizona_textArray);
            $p_Arizona = $California_textArray[$Arizona_randArrayIndexNum];
.....

Then I have 50 of these:

if ($row["state"] == "california") {
            $stateip = $p_California; 
        }
        else if ($row["state"] == "texas") {
            $stateip = $p_Texas;
        }
        else if ($row["state"] == "alabama") {
            $stateip = $p_Alabama;
        }
        else if ($row["state"] == "alaska") {
            $stateip = $p_Alaska;
        }

I'm pretty much sure that it's a bad approach.. Maybe there's a way to do all this with like 3 lines of foreach?

  • 写回答

2条回答 默认 最新

  • drl47263 2016-10-09 18:02
    关注

    Something like this:

    // holds your content
    $state_content = [];
    
    while($row = $result->fetch_assoc()) {
    
        // check do we have state set
        if (!empty($row["state"])) {
            $stateip = getStateIpByName($row["state"]);
        }
    }
    
    /**
     *  Returns random IP
     */
    function getStateIpByName($state_name) {
        $content = getStateContent($state_name);
    
        return $content[array_rand($content)];
    }
    
    /**
     * Returns your's state content by state name
     */
    function getStateContent($state_name) {
    
        // checks do we already have content for this state
        if(!isset($state_content[$state_name])) {
    
            // generate file name
            $file_name = "state/";
            $file_name .= str_replace(" ", "", ucwords($state_name));
            $file_name .= ".txt";
    
            $state_text = file_get_contents($file_name);
            $state_content[$state_name] = explode("
    ", $state_text);
        }
    
        return $state_content[$state_name];
    }
    

    There are probably some errors but you will get idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题