在foreach循环中更改的数组值并不坚持

时间:2015-08-25 14:03:35

标签: php arrays foreach

我有一个foreach循环,应该更改"设置"中某个设置的值。阵列。但是,这不会超出foreach循环。整个功能:

    public function GenerationModifiers(){
    $query = "SELECT `modifiers` FROM `settings`";
    $data = mysqli_query($this->dbc, $query);
    $row = mysqli_fetch_array($data);
    $modifiers = $row['modifiers'];
    $modifiers = explode(";", $modifiers);
    foreach($modifiers as $modifier){
        $mod = explode(".", $modifier);
        $control = $mod[0];
        $setting = $mod[1];
        switch($control){
            case "moduleOff":
                $modules[$setting]['enabled'] = 0;
                print_r($modules[$setting]);
                break;
            case "settingsChange":
                $s = explode(":", $setting);
                $toChange = $s[0];
                $changeTo = $s[1];
                $this->settings[$toChange] = $changeTo;
                print_r($this->settings[$toChange]);
                break;
        }
    }
}

此函数位于一个类中,该类也具有非常相似的构造函数,该类也设置$this->settings; 它在页面生成中使用的位置:

class pageGeneration {
    public function __construct($settings, $version, $dbc, $layout, $core, $parser, $admin){
        $this->settings = $settings;
        $this->version = $version;
        $this->dbc = $dbc;
        $this->layout = $layout;
        $this->core = $core;
        $this->parser = $parser;
        $this->admin = $admin;

    }
    public function Generate(){
        $this->core->GenerationModifiers(); ... //More unneeded code here

我见过人们这样做;

foreach($modifiers as &$modifier){

但是,由于我正在更改"设置"数组,对吧?

1 个答案:

答案 0 :(得分:3)

为了确保我们在同一首歌曲中唱出我所说的一句话。如果结果是fooy,我会删除它。

GET https://api.sandbox.paypal.com/v1/payments/payment/PAY-9NS54822BJ985714GKQ5RH2Y

{
"id": "PAY-9NS54822BJ985714GKQ5RH2Y",
"create_time": "2014-10-12T23:51:07Z",
"update_time": "2014-10-12T23:57:54Z",
"state": "approved",
"intent": "sale",
"payer": {
"payment_method": "paypal",
"status": "VERIFIED",
"payer_info": {
"email": "XXXXXXX",
"first_name": "XXXX Personal Test",
"last_name": "Account",
"payer_id": "XXXXX",
"shipping_address": {
"line1": "cxas",
"line2": "asa",
"city": "FL",
"state": "FL",
"postal_code": "95616",
"country_code": "US",
"recipient_name": "XXXXXPersonal Test Account"
},
"phone": "408-767-7151"
}
},
"transactions": [
{
"amount": {
"total": "20.00",
"currency": "USD",
"details": {
"subtotal": "18.00",
"tax": "1.00",
"shipping": "1.00"
}
},
"description": "This is payment description.",
"item_list": {
"items": [
{
"name": "Hat",
"sku": "product12345",
"price": "2.00",
"currency": "USD",
"quantity": "3",
"description": "This is desc"
},
{
"name": "Hat",
"sku": "product12345",
"price": "2.00",
"currency": "USD",
"quantity": "3",
"description": "This is desc"
},
{
"name": "Hat",
"sku": "product12345",
"price": "2.00",
"currency": "USD",
"quantity": "3",
"description": "This is desc"
}
]
},
"related_resources": [
{
"sale": {
"id": "0P200860V96590012",
"create_time": "2014-10-12T23:51:07Z",
"update_time": "2014-10-12T23:57:54Z",
"amount": {
"total": "20.00",
"currency": "USD"
},
"payment_mode": "INSTANT_TRANSFER",
"state": "completed",
"protection_eligibility": "ELIGIBLE",
"protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
"parent_payment": "PAY-9NS54822BJ985714GKQ5RH2Y",
"transaction_fee": {
"value": "0.88",
"currency": "USD"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/0P200860V96590012",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/0P200860V96590012/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9NS54822BJ985714GKQ5RH2Y",
"rel": "parent_payment",
"method": "GET"
}
]
}
}
]
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-9NS54822BJ985714GKQ5RH2Y",
"rel": "self",
"method": "GET"
}
]
}

来自

public function GenerationModifiers(&$settingsIN){      //<-- changed

    echo 'In GenerationModifiers before trying to change';
    print_r($settingsIN);

    $query = "SELECT `modifiers` FROM `settings`";
    $data = mysqli_query($this->dbc, $query);
    $row = mysqli_fetch_array($data);
    $modifiers = $row['modifiers'];
    $modifiers = explode(";", $modifiers);
    foreach($modifiers as $modifier){
        $mod = explode(".", $modifier);
        $control = $mod[0];
        $setting = $mod[1];
        switch($control){
            case "moduleOff":
                $modules[$setting]['enabled'] = 0;
                print_r($modules[$setting]);
                break;
            case "settingsChange":
                $s = explode(":", $setting);
                $toChange = $s[0];
                $changeTo = $s[1];
                $settingsIN[$toChange] = $changeTo;      //<-- changed
                print_r($settingsIN[$toChange]);         //<-- changed
                break;
        }
    }
}