如何用数组值替换数组值[有两个数组]

时间:2017-01-02 03:26:01

标签: php arrays

我尝试使用array_merge()无效的数组。

我有数组(数组一);

Array (
    [0] => mf_3
    [1] => mf_2
    [2] => mf_1
    [3] => mf_7
    [4] => mf_6
    [5] => mf_4
)

再次在数组上(第二个数组),

Array (
    [0] => stdClass Object
        (
            [id] => 1
            [name] => review_smartphone_display
            [label] => Layar
            [post_type] => review_smartphone
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => review_smartphone_launch
            [label] => Peluncuran
            [post_type] => review_smartphone
        )

    [2] => stdClass Object
        (
            [id] => 3
            [name] => review_smartphone_platform
            [label] => Platform
            [post_type] => review_smartphone
        )

    [3] => stdClass Object
        (
            [id] => 4
            [name] => review_smartphone_camera
            [label] => Kamera
            [post_type] => review_smartphone
        )

    [4] => stdClass Object
        (
            [id] => 6
            [name] => review_smartphone_design
            [label] => Desain
            [post_type] => review_smartphone
        )

    [5] => stdClass Object
        (
            [id] => 7
            [name] => review_smartphone_battery
            [label] => Baterai
            [post_type] => review_smartphone
        )
)

现在,如何用第一个数组中的值替换第二个数组 [id]

我想要这样的结果;

Array (
        [0] => stdClass Object
            (
                [id] => mf_3
                [name] => review_smartphone_display
                [label] => Layar
                [post_type] => review_smartphone
            )

        [1] => stdClass Object
            (
                [id] => mf_2
                [name] => review_smartphone_launch
                [label] => Peluncuran
                [post_type] => review_smartphone
            )

        [2] => stdClass Object
            (
                [id] => mf_1
                [name] => review_smartphone_platform
                [label] => Platform
                [post_type] => review_smartphone
            )

        [3] => stdClass Object
            (
                [id] => mf_7
                [name] => review_smartphone_camera
                [label] => Kamera
                [post_type] => review_smartphone
            )

        [4] => stdClass Object
            (
                [id] => mf_6
                [name] => review_smartphone_design
                [label] => Desain
                [post_type] => review_smartphone
            )

        [5] => stdClass Object
            (
                [id] => mf_4
                [name] => review_smartphone_battery
                [label] => Baterai
                [post_type] => review_smartphone
            )
    )

请帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

利用下面的foreach

app.controller('exploreController', function($scope, dataService, $cookies, $cacheFactory, $http) {
// dataService.explorePosts();

$scope.explore = function(){
    dataService.explorePosts();
    var cache = $cacheFactory.get('$http');
    console.log(cache);
    var usersCache = cache.get('http://dstm.herokuapp.com/api/explore');
    console.log(usersCache);

};

$scope.explore();
});

答案 1 :(得分:0)

foreach ($first_array as $key => $value) {
    $second_array[$key]->id = $value;  
}