douzhi1937 2016-02-12 18:16
浏览 13
已采纳

在PHP中使用公共密钥合并数组

I'm merging a couple of arrays like this in PHP

//all the other stuff
$array1 = (array) $new;
$array2 =  (array) $info;
$ab = array('a' => $array1, 'b' => $array2);
print_r($ab);

print_r($ab) gives me something like this:

Array
(
    [a] => Array
        (
            [entries] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => Serial - This American Life
                            [id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig. 
                        )

                    [1] => stdClass Object
                        (
                            [title] => This American Life - This American Life
                            [id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations. 
                        )

                    [2] => stdClass Object
                        (
                            [title] => Real Crime Profile - Real Crime Profile
                            [id] => Podcast talking about criminal cases and personality profiling.
                        )

                )

        )

    [b] => Array
        (
            [entries] => Array
                (
                    [0] => stdClass Object
                        (
                            [artistName] => This American Life
                            [feedUrl] => http://feeds.serialpodcast.org/serialpodcast
                            [primaryGenreName] => News & Politics
                            [artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
                        )

                    [1] => stdClass Object
                        (
                            [artistName] => This American Life
                            [feedUrl] => http://feed.thisamericanlife.org/talpodcast
                            [primaryGenreName] => Personal Journals
                            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
                        )

                    [2] => stdClass Object
                        (
                            [artistName] => Real Crime Profile
                            [feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
                            [primaryGenreName] => History
                            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
                        )

                )

        )

)

This is close to what I'm looking for, but ideally, I'd like to simplify this even more so I don't have an a and b "tree" within this array.

This is what I'm trying to end up with:

Array
(
    [a] => Array
        (
            [entries] => Array
                (
                    [0] => stdClass Object
                        (
                            [title] => Serial - This American Life
                            [id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig. 
                            [artistName] => This American Life
                            [feedUrl] => http://feeds.serialpodcast.org/serialpodcast
                            [primaryGenreName] => News & Politics
                            [artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
                        )

                    [1] => stdClass Object
                        (
                            [title] => This American Life - This American Life
                            [id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations.
                            [artistName] => This American Life
                            [feedUrl] => http://feed.thisamericanlife.org/talpodcast
                            [primaryGenreName] => Personal Journals
                            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
                        )

                    [2] => stdClass Object
                        (
                            [title] => Real Crime Profile - Real Crime Profile
                            [id] => Podcast talking about criminal cases and personality profiling.
                            [artistName] => Real Crime Profile
                            [feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
                            [primaryGenreName] => History
                            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
                        )

                )

        )

)

I've also tried array_merge_recursive($array1['entries'],$array2['entries'], but this give me something like this (6 objects [2 from one array and 4 from the other] instead of 3):

Array
(
    [0] => stdClass Object
        (
            [title] => Serial - This American Life
            [id] => Serial is a podcast from the creators of This American Life, hosted by Sarah Koenig. Serial unfolds one story - a true story - over the course of a whole season. 
        )

    [1] => stdClass Object
        (
            [title] => This American Life - This American Life
            [id] => This American Life is a weekly public radio show, heard by 2.2 million people on more than 500 stations. 
        )

    [2] => stdClass Object
        (
            [title] => Real Crime Profile - Real Crime Profile
            [id] => Podcast talking about criminal cases and personality profiling.
        )

    [3] => stdClass Object
        (
            [artistName] => This American Life
            [feedUrl] => http://feeds.serialpodcast.org/serialpodcast
            [primaryGenreName] => News & Politics
            [artworkUrl60] => http://is2.mzstatic.com/image/thumb/Music69/v4/70/c9/71/70c97133-f3a8-738e-ea2c-27a6dc7d9731/source/60x60bb.jpg
        )

    [4] => stdClass Object
        (
            [artistName] => This American Life
            [feedUrl] => http://feed.thisamericanlife.org/talpodcast
            [primaryGenreName] => Personal Journals
            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music4/v4/1f/b8/0f/1fb80f69-bd94-8cad-0a2f-b082541d5f64/source/60x60bb.jpg
        )

    [5] => stdClass Object
        (
            [artistName] => Real Crime Profile
            [feedUrl] => http://feeds.soundcloud.com/users/soundcloud:users:202076064/sounds.rss
            [primaryGenreName] => History
            [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music69/v4/e4/0d/1e/e40d1efe-f625-8d15-4e2e-706fecead1e8/source/60x60bb.jpg
        )

)

Thoughts on how to merge these into 3 objects?

  • 写回答

3条回答 默认 最新

  • dst3605528 2016-02-12 18:45
    关注

    This is only an example, to explain how you can use function multiArrayCombine from my answer to another question:

    foreach( $array['a']['entries'] as &$val ) $val = (array) $val;
    foreach( $array['b']['entries'] as &$val ) $val = (array) $val;
    
    $result = multiArrayCombine( $array['a']['entries'], $array['b']['entries'], T_OBJECT_CAST );
    
    $array['a']['entries'] = $result;
    unset( $array['b'] );
    
    print_r( $array );
    

    <kbd>eval.in demo</kbd>

    As I said, this is only an example: the function has the option to return objects but not to analyze it. You can modify it to process also objects and use it directly, without above first two lines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同