简体   繁体   English

PHP对象将值赋给mysqli_fetch_array的字符串

[英]PHP Object giving values to a string from a mysqli_fetch_array

public static function instantiate($record){
     $object = new self;


    // $object->id = $record['UserID'];
    // $object->username = $record['Username'];
    // $object->password = $record['Password'];
    // $object->first_name = $record['Fname'];
    // $object->last_name = $record['Lname'];
    // $object->email = $record['Email'];

    foreach (array_unique($record) as $attribute => $value) {

        if($object->has_attribute($attribute)){

            $object->$attribute = $value;
            //var_dump($attribute);echo '<br>';
        }
    }
    return $object;
}

In this function i get values from this $object in two ways. 在此函数中,我通过两种方式从此$ object获取值。 The long way from the commented part and the short way from the uncommented (obv). 距已注释部分很长的路,而距未注释(obv)很短的路。

The long way gives the good result as this : 很长的路要给出好的结果,因为:

object(User)#5 (6) { ["id"]=> string(1) "1" ["username"]=> string(6) "Parley" ["password"]=> string(5) "12345" ["first_name"]=> string(8) "Cristian" ["last_name"]=> string(8) "Pirloaga" ["email"]=> string(27) "cristian.pirloaga@gmail.com" } object(User)#5(6){[“ id”] =>字符串(1)“ 1” [“用户名”] =>字符串(6)“ Parley” [“密码”] =>字符串(5)“ 12345“ [” first_name“] =>字符串(8)” Cristian“ [” last_name“] =>字符串(8)” Pirloaga“ [”电子邮件“] =>字符串(27)” cristian.pirloaga@gmail.com“ }

The short way gives a bad result as this : 简短的方法给出了一个不好的结果,因为:

int(0) int(1) int(2) int(3) int(4) int(5) int(6) object(User)#5 (13) { ["id"]=> NULL ["username"]=> NULL ["password"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["email"]=> NULL ["0"]=> string(1) "1" ["1"]=> string(8) "Cristian" ["2"]=> string(8) "Pirloaga" ["3"]=> string(27) "cristian.pirloaga@gmail.com" ["4"]=> string(6) "Parley" ["5"]=> string(5) "12345" ["6"]=> string(19) "2016-02-12 05:04:22" } int(0)int(1)int(2)int(3)int(4)int(5)int(6)object(User)#5(13){[“ id”] => NULL [“用户名” ] => NULL [“ password”] => NULL [“ first_name”] => NULL [“ last_name”] => NULL [“ email”] => NULL [“ 0”] => string(1)“ 1” [“ 1”] =>字符串(8)“ Cristian” [“ 2”] =>字符串(8)“ Pirloaga” [“ 3”] =>字符串(27)“ cristian.pirloaga@gmail.com” [“ 4“] =>字符串(6)” Parley“ [” 5“] =>字符串(5)” 12345“ [” 6“] =>字符串(19)” 2016-02-12 05:04:22“}

the int(1)..int(6) are the $attribute values, after is the object. int(1).. int(6)是$ attribute值,之后是对象。

Im trying to match the $attribute values with those fields from the long way. 我试图从长远来看将$ attribute值与那些字段匹配。 How can i do it? 我该怎么做? This is an actually code from a tutorial and it works to him and not to me. 这实际上是教程中的代码,它对他有效,对我无效。

I have a secondary issue. 我有第二个问题。 The foreach loop always double the values.In this case if i remove the array_unique function it will show that result two times tho in data base its only one row.why's that? foreach循环总是将值加倍。在这种情况下,如果我删除array_unique函数,它将在数据库中显示结果两倍于其唯一一行。为什么呢?

You can have the result you want by using mysqli_fetch_object instead of mysqli_fetch_array . 您可以通过使用mysqli_fetch_object而不是mysqli_fetch_array获得所需的结果。

But to answer your question: If you use mysqli_fetch_array , by default you get an array containing numeric keys and associative keys as well. 但是要回答您的问题:如果您使用mysqli_fetch_array ,则默认情况下会得到一个包含数字键和关联键的数组。 For example something like: 例如:

array(
 1 => 'john',
 2 => 'doe',
 'firstname' => 'john',
 'lastname' => 'doe'
)

To avoid that you can set the third parameter int $result_type to MYSQL_ASSOC as described here: http://php.net/manual/de/function.mysql-fetch-array.php 为了避免这种情况,您可以MYSQL_ASSOC如下所述将第三个参数int $result_typeMYSQL_ASSOChttp : MYSQL_ASSOC

Using MYSQL_ASSOC you will get an array without numeric keys, for example: 使用MYSQL_ASSOC您将获得一个不带数字键的数组,例如:

array(
 'firstname' => 'john',
 'lastname' => 'doe'
)

Doing that your function will work, even without the array_unique in the foreach. 这样做,即使在foreach中没有array_unique ,功能也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM