1

私は PHP で OOP を初めて使用し、サードパーティの API を介してプログラムにデータを取り込んだ後、オブジェクト参照を動的に生成するのに苦労しています。

次のデータを含むレコード セットを取得しました。

    stdClass Object ( 
        [adventure] => stdClass Object ( 
            [shortname] => adventure 
            [name] => Adventure 
            [description] => Create a new column for each activity undertaken. The scouts need to complete three activities, and for each one:
                Know the safety issues involved and understand the use of any equipment needed for the activity.
                Show an awareness of environmental issues around the activity (such as erosion at popular climbing areas).
                Know about further opportunities to take part in the chosen activities.
            [picture] => graphics/badges/sc-cs-adch.png 
            [config] => {"sectionsneeded":"1","totalneeded":"1","sections":{"a":"3"}} 
            [order] => 1 
            [groupname] => 
            [status] => 3 
            [userid] => 0 
            [table] => scouts_challenge_adventure ) 
        [csg] => stdClass Object ( 
            [shortname] => csg 
            [name] => Chief Scouts Gold 
            [description] => 
            [picture] => graphics/badges/sc-cs-csa.png 
            [config] => {"sectionsneeded":"-1","totalneeded":"1","sections":{"a":"6","b":"2"}} 
            [order] => 0 
            [groupname] => 
            [status] => 3 
            [userid] => 0 
            [table] => scouts_challenge_csg ) 
        [community] => stdClass Object ( 
            [shortname] => community 
            [name] => Community [description] =>
            Create columns for each community project undertaken, and enter the number of hours each scout spend on them. 6 hours are required.
            [picture] => graphics/badges/sc-cs-coch.png 
            [config] => {"sectionsneeded": -1, "totalneeded": -1, "sections": {"a":1}} 
            [order] => 1 
            [groupname] => 
            [status] => 3 
            [userid] => 0 
            [table] => scouts_challenge_community )
)

このデータを $badges に取り込み、以下のコードに従ってこのデータセットへの参照を動的に作成してテーブルに含めようとしています。

  foreach ($badges as $badge) {
       $badgeShortname = $badge->shortname;
       $badgeObj = '$scoutChallenge-'.'>'."$badgeShortname";
          echo "<td>";  
      echo $badgeObj;
          echo "</td>";                                                                                 
      }

私はさまざまなアプローチを試しましたが、次の行に沿って参照を動的に生成することはできませecho "$scoutChallenge->adventure";

  foreach ($badges as $badge) {
      $badgeShortname = $badge->shortname;
  $badgeObj = '$scoutChallenge-'.'>'."$badgeShortname";
      echo "<td>";  
      switch ($badgeShortname )
      {
        case 'csg':
          echo "$scoutChallenge->csg";
          break;
        case 'adventure':
          echo "$scoutChallenge->adventure";
          break;
        case 'community':
          echo "$scoutChallenge->community";
          break;    
        case 'creative':
          echo "$scoutChallenge->creative";
          break;
        case 'expedition':
          echo "$scoutChallenge->expedition";
          break;
        case 'fitness':
          echo "$scoutChallenge->fitness";
          break;            
        case 'outdoor':
          echo "$scoutChallenge->outdoor";
          break;    
        case 'outdoorplus':
          echo "$scoutChallenge->outdoorplus";
          break;
        case 'promise':
          echo "$scoutChallenge->promise";
          break;
        case 'global':
          echo "$scoutChallenge->global";
          break;    
       default:
      echo "Not Found";
      break;                                                                                    
      }
      echo "</td>"; 
    }
  }

これは明らかに特にスケーラブルではありません。

$scoutChallenge は、私が API 経由で取得した別のデータセットであり、$badges データセットと参照を共有するものです。

オブジェクト参照を動的にエコーする方法はありますか? 「echo $scoutChallenge-> variableElement」のようなもので、switch ステートメントに従って variableElement は $badgeShortname と同等ですか?

4

2 に答える 2

1

使用する

echo $scoutChallenge->$badgeShortname

詳細については、PHP を参照してください: 変数変数

于 2012-09-14T14:30:59.590 に答える
0

以下も確認してください。

get_object_vars

そして、オブジェクトを反復処理できると思います

foreach($object as $attr_name => $attr_value){
     echo $attr_value;
}

パブリック属性でのみ機能します。

于 2012-09-14T15:15:13.550 に答える