从:: find和add值访问数组

时间:2015-08-21 15:18:45

标签: php database phalcon

我需要从$newFeatured[] = games::find访问数据,然后在那里添加值label

控制器:

 foreach ($used_games as $game_id) {

                 $newFeatured[] = games::find(array(
                        "conditions" => "id = '$game_id'",
                        "columns"    => "id, filetype "
                        ));

                 if (in_array($newFeatured->id, $new_games)) 
                  { $newFeatured->label = 'new'; }
                 else  
                  { $newFeatured->label = 'featured'; }
              }

                $this->view->newFeatured = $newFeatured;

$newFeatured->id无效。我该如何访问它?

$newFeatured->label也无效。

查看中,我需要像这样访问它。

foreach ($newFeatured as $hra) {

         echo $hra['0']->filetype."and".$hra['0']->label;
     } 

3 个答案:

答案 0 :(得分:0)

您可以按如下方式在数组中获取结果:

$newFeatured[] = games::find(array(
                        "conditions" => "id = '$game_id'",
                        "columns"    => "id, filetype "
                        ));

试图抓住 $ newFeatured-> id ,这对我来说似乎不对。 尝试将其作为简单变量( not array )获取,然后看看会发生什么,如下所示:

$newFeatured = games::find(array(
                            "conditions" => "id = '$game_id'",
                            "columns"    => "id, filetype "
                            ));

答案 1 :(得分:0)

试试这个:

foreach ($used_games as $game_id) {
   $newFeatured[] = games::find(array(
                    "conditions" => $game_id, //returns only id coming from as $game_id of your foreach
                    "columns"    => "filetype " //this one to filetype
                    ));
      if (in_array($newFeatured['conditions'], $new_games)) 
              { $newFeatured['label'] = 'new'; }
             else  
              { $newFeatured['label'] = 'featured'; }
          }

            $this->view->newFeatured = $newFeatured;

答案 2 :(得分:0)

在控制器

    $newFeatured = array();
    foreach ($used_games as $k => $game_id) {
        $newFeatured[$k] = games::findFirst(array(
            "conditions" => "id = '$game_id'",
            "columns" => "id, filetype "
        ));

        if (in_array($newFeatured[$k]->id, $new_games)) {
            $newFeatured[$k]->label = 'new';
        } else {
            $newFeatured[$k]->label = 'featured';
        }
    }
    $this->view->newFeatured = $newFeatured;

的ViewPage

    foreach ($newFeatured as $hra) {
        echo $hra->filetype . " and " . $hra->label;
    }