【问题标题】:Assign array value into new variable inside the loop changed the original array value将数组值分配给循环内的新变量更改了原始数组值
【发布时间】:2020-05-13 08:24:20
【问题描述】:

我正在使用 wordpress 并从 Mysql 中获取一些记录。 我有三种类型的帐户访问权限“Type1”、“Type2”和“Both”。我想将“Both”类型的数组项更改为 Type1 和 Type2 的 2 个数组。

这是我从 Mysql 得到的回复。

Array
(
    [0] => stdClass Object
        (
            [id] => 583
            [username] => test1@yopmail.com
            [name] => Test 1
            [user_pass] => oN3WIffffffMW
            [school_name] => Test SCHOOL
            [customer_email] => test1@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type1
            [customer_school_id] => 5455
            [is_parent] => 0
            [parent_id] => 1534
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => hh534446gr
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 1
            [email] => test1@yopmail.com
            [id_code] => 55555
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => GA
            [postal] => 444
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4466dgg
        )

    [1] => stdClass Object
        (
            [id] => 584
            [username] => test2@yopmail.com
            [name] => Test 2
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test2@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test2@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4433rrfty66
        )

    [2] => stdClass Object
        (
            [id] => 585
            [username] => test3@yopmail.com
            [name] => Test 3
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test3@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Both
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test3@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 44dd3rrfty66
        )

)

这是我的代码:- $all_records 包含上述数组值。

$all_records1 = array();
if (!empty($all_records) && count($all_records) > 0) {

    foreach($all_records as $key => $allitems ) {

        if($allitems->account_access == 'Both') {
             $newItem = $allitems;
             $allitems->account_access ='Type1';
             $newItem->account_access ='Type2';
              array_push($all_records1,$allitems);
             array_push($all_records1,$newItem);
        } else {
              array_push($all_records1,$allitems);
        }
    }
    echo "<pre>"; print_r($all_records1);   echo "</pre>";  die;
}

它给了我以下结果。

Array
(
    [0] => stdClass Object
        (
            [id] => 583
            [username] => test1@yopmail.com
            [name] => Test 1
            [user_pass] => oN3WIffffffMW
            [school_name] => Test SCHOOL
            [customer_email] => test1@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type1
            [customer_school_id] => 5455
            [is_parent] => 0
            [parent_id] => 1534
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => hh534446gr
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 1
            [email] => test1@yopmail.com
            [id_code] => 55555
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => GA
            [postal] => 444
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4466dgg
        )

    [1] => stdClass Object
        (
            [id] => 584
            [username] => test2@yopmail.com
            [name] => Test 2
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test2@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test2@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4433rrfty66
        )

    [2] => stdClass Object
        (
            [id] => 585
            [username] => test3@yopmail.com
            [name] => Test 3
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test3@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test3@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 44dd3rrfty66
        ) 

        [3] => stdClass Object
        (
            [id] => 585
            [username] => test3@yopmail.com
            [name] => Test 3
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test3@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test3@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 44dd3rrfty66
        )

)

但它应该给出以下结果

Array
(
    [0] => stdClass Object
        (
            [id] => 583
            [username] => test1@yopmail.com
            [name] => Test 1
            [user_pass] => oN3WIffffffMW
            [school_name] => Test SCHOOL
            [customer_email] => test1@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type1
            [customer_school_id] => 5455
            [is_parent] => 0
            [parent_id] => 1534
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => hh534446gr
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 1
            [email] => test1@yopmail.com
            [id_code] => 55555
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => GA
            [postal] => 444
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4466dgg
        )

    [1] => stdClass Object
        (
            [id] => 584
            [username] => test2@yopmail.com
            [name] => Test 2
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test2@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test2@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 4433rrfty66
        )

    [2] => stdClass Object
        (
            [id] => 585
            [username] => test3@yopmail.com
            [name] => Test 3
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test3@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type1
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test3@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 44dd3rrfty66
        ) 

        [3] => stdClass Object
        (
            [id] => 585
            [username] => test3@yopmail.com
            [name] => Test 3
            [user_pass] => QJWBsKfgggggszSk
            [school_name] => Test SCHOOL
            [customer_email] => test3@yopmail.com
            [trial_account] => 
            [expiry_date] => 2021-05-16
            [old_expiry_date] => 
            [created_date] => 2020-05-13 05:15:24
            [status] => 1
            [email_status] => 0
            [account_access] => Type2
            [customer_school_id] => 3333
            [is_parent] => 0
            [parent_id] => 1532
            [logged_count] => 0
            [library_count] => 0
            [starting_ip_address] => 
            [ending_ip_address] => 
            [district_id] => 
            [company] => test
            [37_days_flag] => 0
            [stop_email] => 0
            [monthly_emails] => 0
            [sent_monthly_email] => 
            [pwd_for_mail] => QJWBggg335KzSk
            [welcome_email] => 0
            [send_bcc] => 0
            [is_from_bulk] => 1
            [customer_name] => Test 2
            [email] => test3@yopmail.com
            [id_code] => 566666
            [customer_school] => Test SCHOOL
            [address] => Test
            [city] => Test
            [state] => NC
            [postal] => 455555
            [country] => US
            [telephone] => 555555
            [library_login_url] => 44dd3rrfty66
        )

)

知道我在这段代码中缺少什么吗?

【问题讨论】:

  • 下定决心!你真正使用的是什么代码?
  • 新代码会产生同样的问题吗?
  • 我认为在那种情况下你需要print_r($all_records1, true);
  • @RiggsFolly ,对不起,我之前输入了错误的代码,现在是正确的代码
  • 感谢 Arun 与我们分享这个出色的数组示例。非常感谢。

标签: php arrays wordpress loops


【解决方案1】:

如果这是一个数组数组,您的代码将可以工作,但是因为它是一个对象数组,当您复制数组时它不会克隆数组出现内的对象,它会保留引用。因此,当您修改 account_access 属性时,它实际上是一个引用,因此对原始属性进行了更改。这样做的效果是您对 account_access 的第二次更改也会更改第一次。

因此,您需要克隆数组中的对象,以确保获得该对象的唯一副本

$all_records1 = array();
if (!empty($all_records) && count($all_records) > 0) {

    foreach($all_records as $key => $item ) {

        if($item->account_access == 'Both') {   
            $copy = clone $item;
            $copy->account_access = 'Type1';
            $all_records1[] = $copy;

            $copy = clone $item;
            $copy->account_access = 'Type2';
            $all_records1[] = $copy;
        } else {
            $all_records1[] = $item;
        }
    }
    echo "<pre>"; print_r($all_records1);   echo "</pre>";  die;
} 

我还更改了array_push(),因为您只是将一项推送到新数组中,因此执行简单的$arr[]=$var 会更快,因为您不调用函数,因此您没有那么大的开销,它很小,但要老实说,我更喜欢这个结构而不是array_push()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多