Category Archives: 面试题&杂记

去除php数组中,重复的值

一维数组的重复项

      使用array_unique函数即可,使用实例如下:

              <?php
                  $aa=array(“chengxusheji”,”banana”,”pear”,”chengxusheji”,”wail”,”watermalon”);
                  $bb=array_unique($aa);
                  print_r($bb);
               ?>

      结果如下:Array ( [0] => chengxusheji [1] => banana [2] => pear [4] => wail [5] => watermalon ) 。

二维数组的重复项

     对于二维数组咱们分两种情况讨论,一种是因为某一键名的值不能重复,删除重复项;另一种因为内部的一维数组不能完全相同,而删除重复项,下面举例说明:

           ㈠因为某一键名的值不能重复,删除重复项

          <?php
           function assoc_unique($arr, $key)
            {
              $tmp_arr = array();
              foreach($arr as $k => $v)
             {
                if(in_array($v[$key], $tmp_arr))//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
               {
                  unset($arr[$k]);
               }
             else {
                 $tmp_arr[] = $v[$key];
               }
             }
           sort($arr); //sort函数对数组进行排序
           return $arr;
           }

           $aa = array(
           array(‘id’ => 123, ‘name’ => ‘张三’),
           array(‘id’ => 123, ‘name’ => ‘李四’),
           array(‘id’ => 124, ‘name’ => ‘王五’),
           array(‘id’ => 125, ‘name’ => ‘赵六’),
           array(‘id’ => 126, ‘name’ => ‘赵六’)
           );
           $key = ‘id’;
           assoc_unique(&$aa, $key);
           print_r($aa);
           ?>

     显示结果为:Array ( [0] => Array ( [id] => 123 [name] => 张三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 赵六 ) [3] => Array ( [id] => 126 [name] => 赵六 ) )

       ㈡因内部的一维数组不能完全相同,而删除重复项

           <?php
           function array_unique_fb($array2D){
                foreach ($array2D as $v){
                    $v = join(“,”,$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
                    $temp[] = $v;
                }
                $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
               foreach ($temp as $k => $v){
                   $temp[$k] = explode(“,”,$v);   //再将拆开的数组重新组装
               }
               return $temp;
           }

           $aa = array(
           array(‘id’ => 123, ‘name’ => ‘张三’),
           array(‘id’ => 123, ‘name’ => ‘李四’),
           array(‘id’ => 124, ‘name’ => ‘王五’),
           array(‘id’ => 123, ‘name’ => ‘李四’),
           array(‘id’ => 126, ‘name’ => ‘赵六’)
           );
           $bb=array_unique_fb($aa);
           print_r($bb)
           ?>

         显示结果:Array ( [0] => Array ( [0] => 123 [1] => 张三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] => 126 [1] => 赵六 ) ) 

PHP计算任意两个日期之间的天数

PHP面试题中,关于日期的题目作为基础考题经常出现,下面讨论一下获取两个日期之间的天数的方法。

收到一个答案,拆分年、月、日,分别进行减法,然后统计天数,好累。

针对低版本的PHP可以用下面的方式搞定:

 

$date1 = '2014-02-18 00:09:13';
	$date2 = '2014-03-19 00:02:18';

	function differDays($date1, $date2) {

		$differNums = strtotime($date2) - strtotime($date1);

		$differNums = $differNums / (3600 * 24);

		echo floor(abs($differNums));

	}

	echo differDays($date1, $date2);

	echo "\n";
//或者
	function difDays($date1,$date2){ 

		$date1 = substr($date1, 0, 10);
		$date2 = substr($date2, 0, 10);

		$time1 = explode("-", $date1); 
		$time2 = explode("-", $date2); 

		$date1 = mktime(0, 0, 0, $time1[1],$time1[2],$time1[0]); 
		$date2 = mktime(0, 0, 0, $time2[1],$time2[2],$time2[0]);

		return ($date2 - $date1)/(3600*24) - 1; 
	}

	echo difDays($date1, $date2);

PHP5.3以上版本可以使用date_diff:

 

function dateDiff($date1, $date2) {
		$datetime1 = new DateTime($date1);
		$datetime2 = new DateTime($date2);
		$interval = $datetime1->diff($datetime2);
		return $interval->format('%a');
	}

	echo dateDiff($date1, $date2);

 

本文例子下载:http://pan.baidu.com/s/1eQ1Nn14

 

随机取出一维数组中的N个元素 PHP

<?php

	$testArr = range(a, z);

	#######  the first method  

	$newArr = arrayRand($testArr);

	var_dump($newArr);

	function arrayRand($arr, $num = 3) {
		$arr1 = array_rand($arr, $num);

		$newArr = array();

		foreach ($arr1 as $key => $value) {
			$newArr[] = $arr[$value];
		}

		return $newArr;
	}
	echo "\n";


	######  the second method

	$arr2 = arrayRandom($testArr);

	var_dump($arr2);



	function arrayRandom($arr, $num = 3) {
		shuffle($arr);

		// list($a, $b, $c) = $arr;

		$newArr = array();

		for ($i = 0; $i < $num; $i++) {
			$newArr[] = $arr[$i];
		}

		return $newArr;
	}



	###### the third method    getting random value from assoc arrays

	$testArr = array(
		'a' => 'siyuantlw',
		'b' => 'chengxusheji',
		'c' => 'redisfans',
		'd' => 'phpzl',
		'e' => 'yemiansheji'
	);

	echo "\n";

	$newArr = assocArrayRandom($testArr);

	var_dump($newArr);

	function assocArrayRandom($arr, $num = 2) {
		$keyArr = array_keys($arr);

		shuffle($keyArr);

		$newArr = array();

		for ($i = 0; $i < $num; $i++) {
			$newArr[$keyArr[$i]] = $arr[$keyArr[$i]];
		}

		return $newArr;
	}

?>