当前位置: 代码迷 >> PHP >> 请教怎么将数组扩展
  详细解决方案

请教怎么将数组扩展

热度:58   发布时间:2016-04-28 17:58:52.0
请问如何将数组扩展
已经通过数据库查询得到一个数组,比如$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);

?>


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;
}
  相关解决方案