如何获取第一个数组元素(数组本身)?

ct3nt3jp  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(390)

我有一个数据数组,它是:

Array
(
[0] => Array
    (
        [chat_categories] => one cat
        [display_name] => admin user
        [user_email] => testemail1@email.com
        [user_login] => user
    )

[1] => Array
    (
        [chat_categories] => one cat
        [display_name] => Test User
        [user_email] => testemail@testsite.com
        [user_login] => testuser
    )

)

现在我想从该数组中获取第一个元素,检查这些值是否与显示的\u name、user\u email和user\u login匹配,如果匹配成功,则更新wordpress用户表中的chat\u categories列。

bnlyeluc

bnlyeluc1#

所以,对我来说,这对我有帮助,

for($i=0; $i<sizeof($arr); $i++){
    $data = $arr[$i];
    $chat_categories = $data[chat_categories];
    $display_name = $data[display_name];
    $user_email = $data[user_email];
    $user_login = $data[user_login];
    $success = $wpdb->query("UPDATE $userTableName SET chat_categories = '$chat_categories' WHERE user_email = '$user_email' AND user_login ='$user_login' ");
    }
ig9co6j1

ig9co6j12#

你也可以使用 foreach loop 作为:

$arr = array(
            "0" => array(
                   "chat_categories" => "one cat",
                   "display_name" => "admin user",
                   "user_email" => "testemail1@email.com",
                   "user_login" => "user"
             ),
           "1" => array(
                  "chat_categories" => "one cat",
                  "display_name" => "Test User",
                  "user_email" => "testemail@testsite.com",
                  "user_login" => "testuser"
            )
   );

foreach($arr as $innerArr){    
    $chat_categories = $innerArr["chat_categories"];
    $display_name = $innerArr["display_name"];
    $user_email = $innerArr["user_email"];
    $user_login = $innerArr["user_login"];
    $success = $wpdb->query("UPDATE $userTableName SET chat_categories = '".$chat_categories."' WHERE user_email = '".$user_email."' AND user_login ='".$user_login."'");
    }

相关问题