php向list数组中增加值的方法

文章是一个简单应用实例利用foreach遍历数组,然后给数组增加一些值进去了,方法比较简单,本文章后来介绍了直接使用array_push 来给数组增加值两者本人更喜欢后者一些.

例1代码如下:

  1. foreach($list as &$value){
  2. if(isOnline($value['uid'])){
  3. $value['sid'] = '1';
  4. }else{
  5. $value['sid'] = '0';
  6. }
  7. $value['likes']='1';
  8. $sql="SELECT uid,likes,flower FROM ".DB::table("dmember_member_count")." WHERE u.$value['uid']."'";
  9. $fff = DB::query($sql);
  10. $ffff = DB::fetch($fff);
  11. //print_r($ffff);
  12. $value['flower']=$ffff['flower'];
  13. $value['likes']=$ffff['likes'];
  14. }

例子 2,array_push()函数代码如下:

  1. <?php
  2. $a=array("Dog","Cat");
  3. array_push($a,"Horse","Bird");
  4. print_r($a);
  5. ?>
  6. //输出
  7. //Array ( [0] => Dog [1] => Cat [2] => Horse [3] => Bird )

例子 3,带有字符串键的数组,代码如下:

  1. <?php
  2. $a=array("a"=>"Dog","b"=>"Cat");
  3. array_push($a,"Horse","Bird");
  4. print_r($a);
  5. ?>
  6. //输出:
  7. //Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird )