有没有办法比较两个数组的键,如果这些键是相同的,将第一个数组的值分配给类对象?

时间:2021-08-20 08:21:53

I have two arrays. The first one contains an id, username, and password from the database and it looks something like this:

我有两个数组。第一个包含数据库中的id,用户名和密码,它看起来像这样:

Array ( [id] => 3 [username] => Scott [password] => new )

The second array is

第二个数组是

get_class_vars();

and obviously contains all of the classes vars which are id, username, etc..

并且显然包含所有类变量,即id,username等。

How can I go about comparing the keys from both arrays and then if the keys are the same, for example username=username, how can I assign the values of username to the class property username? it's worth noting that I've already instantiated the class and am calling the methods from test.php:

如何比较两个数组中的键,然后如果键是相同的,例如username = username,如何将username的值分配给类属性用户名?值得注意的是,我已经实例化了这个类,并且正在调用test.php中的方法:

// authenticate returns user info from the db 
$user = new User();
$find = $user->authenticate($username, $password);
$user->instantiate($find) // this is the method that I need to create
                          // in user.php to assign the user properties

Hopefully this makes sense. I been trying to come up with a solution for hours. If you need any clarification please let me know.

希望这是有道理的。我一直试图想出几个小时的解决方案。如果您需要任何澄清,请告诉我。

3 个解决方案

#1


2  

$common_keys = array_intersect(array_keys($array), array_keys(get_class_vars($object));
foreach($common_keys as $key) $object->$key = $array[$key];

#2


1  

Try this:

foreach(get_class_vars() as $key->$value)
  $array[$key] = $value;

This gets the key from get_class_vars() and sets that value in $array (your other array) to $value.

这从get_class_vars()获取密钥,并将$ array(您的其他数组)中的值设置为$ value。

#3


1  

PHP supports storing variable and function names in other variables. Using that, you could iterate over all keys of your array, check if the key exists as an attribute of your User object, and if so, replace the value in the object with the one from your array:

PHP支持在其他变量中存储变量和函数名称。使用它,您可以迭代数组的所有键,检查键是否作为User对象的属性存在,如果是,则将对象中的值替换为数组中的值:

foreach ($array as $key => $value) {
    if (isset($user->$key)) {
        $user->$key = $value;
    }
}

So for Array ( [username] => JohnSmith ) it will check isset($user->username) and then assign $user->username = 'JohnSmith'.

因此对于Array([username] => JohnSmith),它将检查isset($ user-> username),然后分配$ user-> username ='JohnSmith'。

#1


2  

$common_keys = array_intersect(array_keys($array), array_keys(get_class_vars($object));
foreach($common_keys as $key) $object->$key = $array[$key];

#2


1  

Try this:

foreach(get_class_vars() as $key->$value)
  $array[$key] = $value;

This gets the key from get_class_vars() and sets that value in $array (your other array) to $value.

这从get_class_vars()获取密钥,并将$ array(您的其他数组)中的值设置为$ value。

#3


1  

PHP supports storing variable and function names in other variables. Using that, you could iterate over all keys of your array, check if the key exists as an attribute of your User object, and if so, replace the value in the object with the one from your array:

PHP支持在其他变量中存储变量和函数名称。使用它,您可以迭代数组的所有键,检查键是否作为User对象的属性存在,如果是,则将对象中的值替换为数组中的值:

foreach ($array as $key => $value) {
    if (isset($user->$key)) {
        $user->$key = $value;
    }
}

So for Array ( [username] => JohnSmith ) it will check isset($user->username) and then assign $user->username = 'JohnSmith'.

因此对于Array([username] => JohnSmith),它将检查isset($ user-> username),然后分配$ user-> username ='JohnSmith'。