尽量减少与foreach的努力

时间:2016-10-09 17:49:38

标签: php

我正在尝试构建一个脚本,该脚本将从db表中下载用户,并根据其状态为每个用户附加一个新的随机IP。

问题是我写了很多代码,如果我继续使用这种方法,还有很多复制/粘贴工作要做。

有人能指出我如何正确地做到这一点的正确方向吗?

首先,我有50个这样的人:

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

然后我有50个这样的人:

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];
.....

然后我有50个这样的人:

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

我非常肯定这是一个糟糕的方法..也许有一种方法可以用3行foreach做这一切?

2 个答案:

答案 0 :(得分:1)

这样的事情:

// 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("\n", $state_text);
    }

    return $state_content[$state_name];
}

可能有一些错误,但你会明白。

答案 1 :(得分:0)

将所有状态存储在数组中,并在 $states=['california',..]; foreach($states as $state){ //Your code for one state //Replace state name with $state variable } 块中执行所有操作

<frutis>((?:(?!mango|grapes).)*)<\/frutis>