代码如下Array ( [0] => Array ( [class_id] => 1101 [resource_author] => 1 [resource_status] => 6 ) [1] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 2 [resource_status] => 5 ) [2] => Array ( [class_id] => 1103 [resource_author] => 2 [resource_status] => 5 ) [3] => Array ( [class_id] => 1103 [resource_author] => 5 [resource_status] => 6 ) [4] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 5 [resource_status] => 6 ) )
现在的要求 是将 resource_author 这一项提出来,组合成新的数组 并且要去掉重复,希望能给出具体代码和每一步的注释,谢谢
现在的要求 是将 resource_author 这一项提出来,组合成新的数组 并且要去掉重复,希望能给出具体代码和每一步的注释,谢谢
$arr=Array ( [0] => Array ( [class_id] => 1101 [resource_author] => 1 [resource_status] => 6 ) [1] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 2 [resource_status] => 5 ) [2] => Array ( [class_id] => 1103 [resource_author] => 2 [resource_status] => 5 ) [3] => Array ( [class_id] => 1103 [resource_author] => 5 [resource_status] => 6 ) [4] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 5 [resource_status] => 6 ) );
foreach($arr as $key=>$val){ $arr1=$val[resource_author];
}
$result = array_unique ($arr1);
print_r($result);
$arr = Array ( [0] => Array ( [class_id] => 1101 [resource_author] => 1 [resource_status] => 6 ) [1] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 2 [resource_status] => 5 ) [2] => Array ( [class_id] => 1103 [resource_author] => 2 [resource_status] => 5 ) [3] => Array ( [class_id] => 1103 [resource_author] => 5 [resource_status] => 6 ) [4] => Array ( [class_id] => 1103 [resource_type] => [resource_author] => 5 [resource_status] => 6 ) );$newArr = array();
foreach($arr as $value){ //遍历第一层数组
foreach($value as $resource){ //遍历第二层数组
$author = $resource['resource_author']; //获取到需要重新组成数组的值
if(!in_array($author,$newArr)){ //判断该值是否已经存在新数组中
array_push($newArr,$author); //不存在则加入该新数组中
}
}
}
print_r($newArr);这个是我随便写的,没测试过不知道对你有没有用。
$arr = array ( array ( 'class_id' => 1101, 'resource_author' => 1, 'resource_status' => 6 ),
array ( 'class_id' => 1103, 'resource_type' =>'', 'resource_author' => 2, 'resource_status' => 5 ),
array ( 'class_id' => 1103, 'resource_author' => 2, 'resource_status' => 5 ),
array ( 'class_id' => 1103, 'resource_author' => 5, 'resource_status' => 6 ),
array ( 'class_id' => 1103, 'resource_type' =>'', 'resource_author' => 5, 'resource_status' => 6 ) );$newArr = array();
foreach($arr as $value){ //遍历第一层数组
$author = $value['resource_author']; //获取到需要重新组成数组的值
if(!in_array($author,$newArr)){ //判断该值是否已经存在新数组中
array_push($newArr,$author); //不存在则加入该新数组中
}
}
print_r($newArr);
这样可以,测试通过
$arr = Array( '0' => Array ( 'class_id' => 1101 , 'resource_author' => 1 , 'resource_status' => 6 ) , '1' => Array ( 'class_id' => 1103 , 'resource_type' => '' , 'resource_author' => 2 , 'resource_status' => 5 ) , '2' => Array ( 'class_id' => 1103 , 'resource_author' => 2 , 'resource_status' => 5 ) , '3' => Array ( 'class_id' => 1103 , 'resource_author' => 5 , 'resource_status' => 6 ) , '4' => Array ( 'class_id' => 1103 , 'resource_type' => '' , 'resource_author' => 5 , 'resource_status' => 6 ) );
$out = array();
foreach($arr as $v){
if(array_key_exists('resource_author',$v) && !in_array($v['resource_author'],$out)){
$out[] = $v['resource_author'];
}
}
print_r($out);
(PHP 4 >= 4.0.7, PHP 5)array_key_exists — 检查给定的键名或索引是否存在于数组中说明
bool array_key_exists ( mixed $key , array $search )
array_key_exists() 在给定的 key 存在于数组中时返回 TRUE。key 可以是任何能作为数组索引的值。array_key_exists() 也可用于对象。
Example #1 array_key_exists() 例子<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?> Note: 在 PHP 4.0.6 中本函数名为 key_exists()。 Example #2 array_key_exists() 与 isset() 对比isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会。 <?php
$search_array = array('first' => null, 'second' => 4);// returns false
isset($search_array['first']);// returns true
array_key_exists('first', $search_array);
?>
=============================================================in_array
(PHP 4, PHP 5)in_array — 检查数组中是否存在某个值说明
bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
在 haystack 中搜索 needle,如果找到则返回 TRUE,否则返回 FALSE。 如果第三个参数 strict 的值为 TRUE 则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。 Note: 如果 needle 是字符串,则比较是区分大小写的。
Note: 在 PHP 版本 4.2.0 之前,needle 不允许是一个数组。 Example #1 in_array() 例子<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为: Got Irix
Example #2 in_array() 严格类型检查例子<?php
$a = array('1.10', 12.4, 1.13);if (in_array('12.4', $a, true)) {
echo "'12.4' found with strict check\n";
}
if (in_array(1.13, $a, true)) {
echo "1.13 found with strict check\n";
}
?>
以上例程会输出:1.13 found with strict check
Example #3 in_array() 中用数组作为 needle<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');if (in_array(array('p', 'h'), $a)) {
echo "'ph' was found\n";
}
if (in_array(array('f', 'i'), $a)) {
echo "'fi' was found\n";
}
if (in_array('o', $a)) {
echo "'o' was found\n";
}
?>
以上例程会输出: 'ph' was found
'o' was found