如何在循环中构建自定义的2D数组 我需要从我的数据库检索的记录中构建一个多维数组。 这是我拥有的代码:

问题描述 投票:0回答:1
数据库中的所有记录都在数组中的一个接一个。我需要使用多维数组来优化它。

$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'];


$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

php arrays multidimensional-array
1个回答
2
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.