使用多个数组进行操作

时间:2015-10-19 08:26:38

标签: php arrays

$newarry =   array
            (
            [0] => 2
            [1] => 4
            )
$serial_model_itemid = array
                         (
                         [0] => 0
                         [1] => 2
                         [2] => 1
                         [3] => 2
                        )

需要合并两个数组并得到像

array
         (
           [2] => 2 // in 2nd array [1]+[3] as single count
           [4] => 1 // in 2nd array [0] + [2] as  single count
         )

我试过了:

$indent_det_id1 = array_unique($data['indent_detail_id1tabl']);
$serial_model_itemid = array_count_values($data['item_id']); 
$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC); 
$itmid_zero = array_splice($serial_model_itemid,0);
unset($itmid_zero[0]); 
$newarry = array_filter($indent_det_id1); 
$serial_model_itemid = array_count_values($data['item_id']); 
print_r( $serial_model_itemid); 
$a = array_unique( $serial_model_itemid);

4 个答案:

答案 0 :(得分:1)

print_r( $data['item_id']);


 $serial_model_itemid = array_count_values($data['item_id']);
 //$sortted_itm_id = sort($serial_model_itemid, SORT_NUMERIC);
 $pos = array_search (0,$serial_model_itemid);
 echo "0 founded at ".$pos;
 unset($serial_model_itemid[$pos]);
 print_r($serial_model_itemid);
 print_r( $serial_model_itemid);
$a = array_unique( $serial_model_itemid);

答案 1 :(得分:0)

使用函数array_count_values

<?php
   $n = array(0,2,1,2);
   $newarray = array_count_values($n);
   echo '<pre>'.print_r($newarray, true).'</pre>';
?>

<强>结果

Array
(
    [0] => 1
    [2] => 2
    [1] => 1
)

答案 2 :(得分:0)

这是我发现的工作:

 // count items in array
 $serial_model_itemid = array_count_values($data['item_id']);
 // search the array
 $pos = array_search (0,$serial_model_itemid);
 // echo where it was found
 echo "0 found at ".$pos;
 // unset it
 unset($serial_model_itemid[$pos]);
 // print out for debug
 print_r($serial_model_itemid); 
 // Assign the unique key
 $a = array_unique( $serial_model_itemid);

答案 3 :(得分:0)

 // count items in array
 $serial_model_itemid = array_count_values($data['item_id']);
 // search the array
 $pos = array_search (0,$serial_model_itemid);
 // echo where it was found
 echo "0 found at ".$pos;
 // unset it
 unset($serial_model_itemid[$pos]);
 // print out for debug
 print_r($serial_model_itemid); 
 // Assign the unique key
 $a = array_unique( $serial_model_itemid);