一维数组的重复项:
二维数组的重复项:
一维数组的重复项:
二维数组的重复项:
<?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; } ?>
直接贴例子 */ $data = array( array('ID' => 1, 'Title' => '程序设计', 'Desc' => '编程珠玑', 'Url' => 'Http://www.chengxusheji.com'), array('ID' => 2, 'Title' => 'Redisfans', 'Desc' => 'Redis爱好者', 'Url' => 'Http://www.redisfans.com'), array('ID' => 3, 'Title' => 'PHP之路', 'Desc' => 'PHP学习之路', 'Url' => 'Http://www.phpzl.com') ); $xmlString =<<<XML <?xml version='1.0' encoding='utf-8'?> <document> </document> XML; $xml = simplexml_load_string($xmlString); $xml->addChild('webSite', 'http://www.chengxusheji.com');//给 SimpleXML 元素添加一个子元素。 $xml->addChild('webMaster', '40285090@qq.com'); $xml->addChild('updatePeri', '10000'); //元素属性数组 $attributeArray = array( 'Title' => array( 'author' => 'siyuantlw' ) ); foreach($data as $key => $value) { $item = $xml->addChild('item'); foreach ($value as $k => $val) { $node = $item->addChild($k, $val); if(isset($attributeArray[$k]) && is_array($attributeArray[$k])) { foreach ($attributeArray[$k] as $aKey => $aVal) { $node->addAttribute($aKey, $aVal);//给 SimpleXML 元素添加一个属性 } } } } //输出 header("Content-type: text/xml"); echo $xml->asXml();//从 SimpleXML 元素获取 XML 字符串 //保存文件 //$xml->asXml("filepath/video.xml"); 去下例子吧,wp编辑器过滤掉好多东西~ 例子文件下载:http://pan.baidu.com/s/1c0jAsgg 提取密码:9j1i