简体   繁体   English

用数组PHP制作对象

[英]make an object out of an array PHP

I am trying to search one word in my whole table. 我试图在整个桌子上搜索一个单词。 So if you search Eminem, you have to get everything with the word Eminem. 因此,如果您搜索Eminem,则必须使用Eminem这个单词来获取所有内容。

I search 我搜索

<?php

 $sql="SELECT * FROM album WHERE albumartiest like '$zoek'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();                         
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumnaam like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumartiest like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumgenre like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumafspeelijst like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}

It works, but not exactly how I want it. 它可以工作,但不完全是我想要的。 The result is this: 结果是这样的:

Array ( [0] => Array ( [0] => Array ( [albumcode] => 45 [albumnaam] => recovery [albumafspeelijst] => ["Cold Wind Blows","Talkin' 2 Myself","On Fire","Won't Back Down","W.T.P.","Going Through Changes","Not Afraid","Seduction","No Love","Space Bound","Cinderella Man","To Life","So Bad","Almost Famous","Love The Way You Lie","You're Never Over",""] [albumartiest] => Eminem [albumgenre] => hip-hop [albumimage] => images\eminemrecovery.png [albumprijs] => 20 ) ) [1] => Array ( [0] => Array ( [albumcode] => 45 [albumnaam] => recovery [albumafspeelijst] => ["Cold Wind Blows","Talkin' 2 Myself","On Fire","Won't Back Down","W.T.P.","Going Through Changes","Not Afraid","Seduction","No Love","Space Bound","Cinderella Man","To Life","So Bad","Almost Famous","Love The Way You Lie","You're Never Over",""] [albumartiest] => Eminem [albumgenre] => hip-hop [albumimage] => images\eminemrecovery.png [albumprijs] => 20 ) ) )

that's okay, but what I want is take out variable's and use it. 没关系,但是我想要的是取出变量并使用它。

is there a way that I can get variable's out of the array and use it? 有没有一种方法可以让我从数组中获取变量并使用它? If you guys want more information about my code please ask! 如果你们想要有关我的代码的更多信息,请询问!

Try using this 试试这个

Yii::app()->db->CreateCommand($sql)->setFetchMode(PDO::FETCH_OBJ)->queryAll()

This will give you an array of objects with column name as the properties . 这将为您提供一个以列名作为属性的对象数组。

Eg:- 例如:-

foreach($result as $row)
{
echo $row->albumcode;
}

If you want to access the result set like an object you can use the native PHP class ArrayObject and provide the flag to indicate that. 如果要像对象一样访问结果集,则可以使用本机PHP类ArrayObject并提供标志来表明这一点。

$album = new ArrayObject($result, ArrayObject::ARRAY_AS_PROPS);

You can now access the results like the following: 现在,您可以像以下那样访问结果:

$code = $album->albumcode;
$name = $album->albumnaam;

Hope this can guide you, happy coding! 希望这可以指导您,编码愉快!

uhhh just do 嗯,只是做

foreach($zoekresultaat as $key => $value) {
   //do what I want with each seperate returened result. The array key is in $key and the result array is in $value
   echo $value['albumcode'] . ' = '. $value['albumnaam'];

} }

aka, basic php aka,基本的PHP

And please for the security of your app, learn how to do prepared statements in yii 并请确保您应用的安全性,以了解如何在yii中执行准备好的语句

The way your query is now I could wipe your entire database 现在您查询的方式可以清除整个数据库

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

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