php foreach详解

php 4 引入foreach,用于遍历数组的键值对.两种语法结构:

第一种: 获取一个数组的值()

   foreach( $variable  as  $value ){

       #code;

     }

例:

1  <?php
2      $arr = array( 1, 2, 3, 4 );
3      foreach ( $arr as $val ) {
4          echo $val ;
5      }
6  ?>

// 输出: 1  2  3  4

 

第二种: 获取一个数组的键值对,重组成一个新数组


    foreach ($variable as $key => $value) {
        # code...
    }

     $arr = array( 1, 2, 3, 4, 5 );
     $arr1 = array();
     foreach ( $arr as $key => $value ) {
         $arr1 = array(
             $key =>$value  
             );
         print_r($arr1);
     }

 

 

 

php 5 foreach可以用来遍历对象

 

下面说下利用遍历数组 ,重组数组结构

不同键的数组求差集:

数组1构造  one

Array
(
    [0] => Array
        (
            [Platform] => htc
            [OrderID] => ZF-1ef459cd00cd47ad81982daca3a141ca
            [game_order_id] => 2001-0744382800107
            [Account] => 200101103
            [GameCoin] => 60
            [RoleName] => 未传递
            [ChargeTime] => 2015-07-21 09:57:34
        )

    [1] => Array
        (
            [Platform] => htc
            [OrderID] => ZF-21abd01c3a64492c928df1972b06e8cc
            [game_order_id] => 2001-0744387100207
            [Account] => 200101103
            [GameCoin] => 60
            [RoleName] => 未传递
            [ChargeTime] => 2015-07-21 09:58:11
        )

    [2] => Array
        (
            [Platform] => htc
            [OrderID] => ZF-01e95ae0c01940928765198f80342dbc
            [game_order_id] => 2001-0744723000207
            [Account] => 200101109
            [GameCoin] => 60
            [RoleName] => 未传递
            [ChargeTime] => 2015-07-21 10:54:13
        )

.....

 

数组2 构造  two
Array
(
    [0] => Array
        (
            [ID] => 2001-0744382800107
        )

    [1] => Array
        (
            [ID] => 2001-0763066100107
        )

)

 

数组3构造 three

Array
(
    [0] => Array
        (
            [GameOrderId] => 2001-0744811600107
        )

    [1] => Array
        (
            [GameOrderId] => 2001-0744387100207
        )

    [2] => Array
        (
            [GameOrderId] => 2001-0744723000207
        )

    [3] => Array
        (
            [GameOrderId] => 2001-0744834400107
        )

)

 

求数组1和2 的差集
foreach ($one $ num => $o) {
            $exist = false;
            foreach ($two as $key => $t) {
                 if($o['game_order_id'] == $t['ID'] ){
                     $exist = true;
                     break;
                 }
            }
            if ($exist == false) {
                $listA[] = $pre;
            }
}

 

$list 就是要求的数组1和2的差集数组 

测试另外一种写法
foreach($one as $num => $o){
            foreach ($two as $key => $t) {
               if($o['game_order_id'] == $t[ID]){
                unset($o);
               }
            }
            $c[] = $o;
         }
printf_r(array_values(array_filter($c)));

 

结果是可行的,就是不知道有没有bug,之前还用了一个数组函数,array_diff_assoc($arr1,$arr2),结果结果比较类似,错以为一样,经测试array_diff_assoc 先比较键值,去掉不同键值得键值对,
手册里是这么介绍的,比较键名和键值,并返回两个数组的差集数组。欢迎补充

 

posted @ 2015-07-09 17:06  单 车  阅读(4572)  评论(0编辑  收藏  举报