3

データベースから取得したレコードから多次元配列を作成する必要があります。

ここに私が持っているコードがあります:

<?php
/*
 ...some code...
 */
foreach($data['colleges'] as $college)
{
  $college_temp[] =  $college->name; 
  $college_temp[] =  $college->abbrev;
  $college_temp[] =  $college->long_name;
  $college_temp[] =  $college->long_abbrev;
  $college_temp[] =  $college->url;
  $college_temp[] =  $college->description;
}

データベースからのすべてのレコードは、配列内で次々と移動します。多次元配列を使用してこれを最適化する必要があります。

4

2 に答える 2

2
$college_temp = array();
foreach($data['colleges'] as $college)
{
    $college_temp[] =  $college;
}

// Echo the first one's name
echo $college_temp[0]['name'];

// Echo the second one's url
echo $college_temp[1]['url'];
于 2012-06-27T17:47:33.467 に答える
1
$college_temp = array();
$i = 0;
foreach($data['colleges'] as $college)
    {

        $college_temp[$i]['name'] =  $college->name; 
        $college_temp[$i]['abbrev'] =  $college->abbrev;
        $college_temp[$i]['long_name'] =  $college->long_name;
        $college_temp[$i]['long_abbrev'] =  $college->long_abbrev;
        $college_temp[$i]['url'] =  $college->url;
        $college_temp[$i]['description'] =  $college->description;
        $i++;

    }
    // var_dump($college_temp); // uncomment to check array contents
于 2012-06-27T17:55:21.807 に答える