0

我正在使用 PHP,并且有一个看起来像这样的数组数组:

Array(
    [0] => Array(
           [id]  =>1,
           [name]=>"edward",
           [asset]=>"somesong.mp3"
           ),
    [1] => Array(
           [id]  =>1,
           [name]=>"edward",
           [asset]=>"somemovie.mov"
           ),
    [2] => Array(
           [id]  =>2,
           [name]=>"sally",
           [asset]=>"anothersong.mp3"
           ),
    [3] => Array(
           [id]  =>2,
           [name]=>"sally",
           [asset]=>"anothermovie.mov"
           ),
...
)

如您所见,它们是主数组中每个元素之间的相似之处,只是 [asset] 键的值不同。我想合并主数组中的元素,以便每个 [asset] 的两个值都保存在新元素中,就像这样

FinalArray(
    [0] => Array(
           [id]  =>1,
           [name]=>"edward",
           [song]=>"somesong.mp3",
           [movie]=>"somemovie.mov"
           ),
    [1] => Array(
           [id]  =>2,
           [name]=>"sally",
           [song]=>"anothersong.mp3",
           [movie]=>"anothermovie.mov"
           )
...
)

我开始探索使用内部和外部foreach()循环的组合使用结构

// $person and $person02 are copies of the same array
foreach($person as $key=>$value){
    // grab an element in this loop
    $currElement=$person[$key];
    foreach($person2 as $key2=>$value2){
        $currElement2=$person2[$key2];
        // compare $currElement to $currElement2
        if($currElement['id']==$currElement2['id']){
            // determine if [asset] is an mp3 or mov
            $currAsset2=$currElement2['asset'];
            $currAsset =$currElement['asset'];

            $ext = substr(strrchr($currAsset,'.'),1)
            if($ext=='mp3'){ 
                // then we have a song and should store it
                $song=$currAsset['asset'];
                $movie=$currAsset2['asset'];
            }else{
                //  switch sides if you like
                $song=$currAsset2['asset'];
                $movie=$currAsset['asset'];
            }
        }

        // create a new array and add it to the result array
        $newArrEl = array(
            'id'   =>$currElement['id'],
            'name' =>$currElement['id'],
            'song' => $song,
            'movie' => $movie
        );
        $resultArray.push(); // add to final array
    }
}
}

问题是,我探索了一堆 php 数组函数的组合,但似乎无法完全正确。所以我希望这里的人也可以帮助我解决这个问题。如何让原始数据与类似值合并到要添加到最终数组中的新元素中?

4

2 回答 2

0

尝试这个 :

$array = array(
    array(
        'id' => 1,
        'name' => 'edwards',
        'asset' => "somesong.mp3"
    ),
    array(
        'id' => 1,
        'name' => 'edwards',
        'asset' => "somemovie.mov"
    ),
    array(
        'id' => 2,
        'name' => 'sally',
        'asset' => "anothersong.mp3"
    ),
    array(
        'id' => 2,
        'name' => 'sally',
        'asset' => "anothermovie.mov"
    )
);

$res   = array();

foreach($array as $val){
   $res[$val['id']]['id'] =  $val['id'];
   $res[$val['id']]['name'] =  $val['name'];
   if(preg_match('/mp3$/',$val['asset'])){
    $res[$val['id']]['song'] =  $val['asset'];
   }
   if(preg_match('/mov$/',$val['asset'])){
    $res[$val['id']]['movie'] =  $val['asset'];
   }
}
$res  = array_values($res);

echo "<pre>";
print_r($res);
于 2013-03-07T04:50:30.317 回答
0

看起来你的主要标识符是 id,我会用 id 作为键创建一个目标数组,并只遍历原始数组一次,如下所示:

<?php 

$original_array = array(
    array(
        'id' => 1,
        'name' => 'edwards',
        'asset' => "somesong.mp3"
    ),
    array(
        'id' => 1,
        'name' => 'edwards',
        'asset' => "somemovie.mov"
    ),
    array(
        'id' => 2,
        'name' => 'sally',
        'asset' => "anothersong.mp3"
    ),
    array(
        'id' => 2,
        'name' => 'sally',
        'asset' => "anothermovie.mov"
    )
);

$result = array();

$fields_map = array(
    '/\.mp3$/iS' => 'song',
    '/\.mov$/iS' => 'movie'
);

foreach($original_array as $item)
{
    $id = $item['id'];

    if (!isset($result[$id]))
    {
        // initial population
        $result[$id] = array(
            'id' => $id,
            'name' => $item['name']
        );
    }

    if (isset($item['asset']))
    {
        foreach($fields_map as $re => $field_name)
        {
            if (preg_match($re, $item['asset']))
            {
                $result[$id][$field_name] = $item['asset'];
                break;
            }
        }
    }
}

var_dump($result);

注意:我对 field_map 使用正则表达式,而不是文件扩展名上的简单哈希图,因为我假设您可能需要更多扩展名来支持相同的字段(例如 mp3、ogg、wav、flac 所有映射到字段 'song ')。正则表达式使添加更多字段变得容易。

于 2013-03-07T03:26:36.143 回答