Home > Backend Development > PHP Tutorial > 请问如何将数组扩展

请问如何将数组扩展

WBOY
Release: 2016-06-23 13:41:55
Original
958 people have browsed it

已经通过数据库查询得到一个数组,比如$data[0]['name']="张三",$data[1]['name']="李四",$data[0]['分数']=100,$data[0]['分数']=90,现在我想在数组中加一个字段,比如【毕业院校】,而这个毕业院校不在数据库中,在xml文件中,我会写一个函数function getyx('张三'),这样就能得到院校了,这个我会,我想问的是怎样把【毕业院校】这个字段加到$data里,使得我能通过$data[0]['毕业院校']这种方法得到对应的值。谢谢。

当然我知道不必非这样,但是这里主要是想弄会这个问题。谢谢。


回复讨论(解决方案)

这样吗?

<?php$data = array(    array(        'name' => '张三',        'score' => '100'    ),    array(        'name' => '李四',        'score' => '90'    ));$data[0]['school'] = 'school1';$data[1]['school'] = 'school2';echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';print_r($data);?>
Copy after login


Array
(
[0] => Array
(
[name] => 张三
[score] => 100
[school] => school1
)

[1] => Array
(
[name] => 李四
[score] => 90
[school] => school2
)

)

既然是 通过数据库查询得到一个数组
那么你在读取查询结果时就可加进去了

while($row = mysql_fetch_assoc($rs)) {  $row['毕业院校'] = getyx($row['name']);  $data[] = $row;}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template