如何在php中更改数组键值而不更改数组格式

时间:2016-07-29 06:40:11

标签: php

请解释如何将一个数组值更改为第二个值。

请在下面找到示例。

第一个数组

Array
(
    [20239802] => one test
    [20239801] => two testttttt
)

第二个数组

Array (
    [content] => Array (
        [0] => Array (
            [content_pack_id] => 10002
            [content_pack_name] => 100 Days Of Love-FLA
            [image_path] => Array ( [0] => pack_image_10002_width. )
            [content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
            [content_id] => 20239802
            [track] => Lede Inthati Santhosham
            [duration] => 0
        )
        [1] => Array (
            [content_pack_id] => 10003
            [content_pack_name] => 1001 fdfdf 
            [image_path] => Array ( [0] => pack_image_10002_width. )    
            [content_image_path] => Array ( [imgjpeg80] => http://content.jpg )    
            [content_id] => 20239801
            [track] => Lede Inthati Santhosham
            [duration] => 0
        )
    )
    [autoshuffle_pack] => no
)

如果第一个数组[track]与第二个数组[20239802]匹配,我们需要替换第二个数组中的[content_id]

需要放弃: -

Array
(
    [content] => Array
        (
            [0] => Array
                (
                    [content_pack_id] => 10002
                    [content_pack_name] => 100 Days Of Love-FLA
                    [image_path] => Array
                        (
                            [0] => pack_image_10002_width.
                        )

                    [content_image_path] => Array
                        (

                            [imgjpeg80] => http://content.jpg
                        )

                    [content_id] => 20239802
                    [track] => one test
                    [duration] => 0
                )

            [1] => Array
                (
                    [content_pack_id] => 10003
                    [content_pack_name] => 1001 fdfdf 
                    [image_path] => Array
                        (
                            [0] => pack_image_10002_width.
                        )

                    [content_image_path] => Array
                        (

                            [imgjpeg80] => http://content.jpg
                        )

                    [content_id] => 20239801
                    [track] => two testttttt
                    [duration] => 0
                )   
        )

    [autoshuffle_pack] => no
)

检查我需要的[track]值变化

依赖于第一个数组。

Array
(
    [20239802] => one test
    [20239801] => two testttttt
)

第二个数组

[content_id] => 20239801
[track] => two testttttt

2 个答案:

答案 0 :(得分:2)

这是你想要做的:

foreach($second_array as $key => $second_row) {
    $content_id = $second_row['content_id'];
    if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
        $second_array['track'] = $first_array[$content_id];
    }
}

答案 1 :(得分:0)

你可以自己动手。

第一个阵列中的Foreach记录:

  • 循环搜索第二个并搜索key == content_id
  • 如果匹配,请将array2[$index][track]的值设置为array1[key]
  • 如果不是继续

您还可以使用array_search()功能更快地找到匹配项,因为我们可以让您查看PHP文档