-2

我在 php 中有一个这样的数组:

Array
(
    [0] => Array
    (
        [name] => abc
        [id] => 107            
        [CycleNumber] => 1
        [Type] => People
    )
    [1] => Array
    (
        [name] => john
        [id] => 312         
        [CycleNumber] => 5
        [Type] => People
    )
    [2] => Array
    (
        [name] => jenny
        [id] => 110        
        [CycleNumber] => 3
        [Type] => People
    )
    [3] => Array
    (
        [name] => metting room
        [id] => 590 
        [CycleNumber] => 4
        [Type] => Facility
    )
    [4] => Array
    (
        [name] => projector
        [id] => 470    
        [CycleNumber] => 4
        [Type] => Facility
    )

 )

我想用空白替换重复的类型。但第一个应该有该类型名称,其他应该有空白。所以结果数组应该是这样的:

 Array
 (
    [0] => Array
    (
        [name] => abc
        [id] => 107            
        [CycleNumber] => 1
        [Type] => People
    )
    [1] => Array
    (
        [name] => john
        [id] => 312         
        [CycleNumber] => 5
        [Type] => 
    )
    [2] => Array
    (
        [name] => jenny
        [id] => 110        
        [CycleNumber] => 3
        [Type] => 
    )
    [3] => Array
    (
        [name] => metting room
        [id] => 590 
        [CycleNumber] => 4
        [Type] => Facility
    )
    [4] => Array
    (
        [name] => projector
        [id] => 470    
        [CycleNumber] => 4
        [Type] => 
    )

 )

我只想要这种格式的数组。我正在使用 PHP zend。

我搜索了这个,但他们中的大多数都显示要从数组中删除该元素。但我不想删除它。我想用空白替换它,但想显示第一个。

尝试过的代码

$result = array(); 
$result1 = array(); 
$result2 = array(); 
$y = array(); 
$y1 = array(); 
foreach ($data as $entry) {  
    $type= $entry["type"]; 
    if (!isset($y[$type])) {
        $y[$type] = array(); 
        unset($entry["type"]);   
        $result[$type][] = $entry;   
    }
} 

谁能告诉我该怎么做?

4

1 回答 1

0

代码

$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
    if($data[$i]['Type'] == $data[$found]['Type']) {
        $data[$i]['Type'] = "";
    }
    else {
        $found = $i;
    }
} 
$i++;
}
echo "<pre>";
print_r($data);

输出

Array
(
[0] => Array
    (
        [name] => abc
        [id] => 107
        [CycleNumber] => 1
        [Type] => People
    )

[1] => Array
    (
        [name] => john
        [id] => 312
        [CycleNumber] => 5
        [Type] => 
    )

[2] => Array
    (
        [name] => jenny
        [id] => 110
        [CycleNumber] => 3
        [Type] => 
    )

[3] => Array
    (
        [name] => metting room
        [id] => 590
        [CycleNumber] => 4
        [Type] => Facility
    )

[4] => Array
    (
        [name] => projector
        [id] => 470
        [CycleNumber] => 4
        [Type] => 
    )

)

例子

于 2013-04-29T06:16:20.237 回答