如何在php中更改嵌套数组值的值

时间:2016-08-31 11:37:59

标签: php arrays

我有一个像这样的数组

Array
(
    [0] => stdClass Object
        (
            [id] => 1522
            [image] => 1465458797-sug.jpg,1465458797-rajdhanee.jpg
            [user_name] => Suganya
            [thumbnail] => Taylor_Otwell_.jpg
            [feature_product] => 1
        )

    [1] => stdClass Object
        (
            [id] => 151
            [image] => 1465296555-14.jpg
            [user_name] => Sugan
            [thumbnail] => sug.jpg
            [feature_product] => 0
        )

    [2] => stdClass Object
        (
            [id] => 1
            [image] => 1401101483-best-kitchen-appliances.jpg,1401101483-home-appliance_laundry.jpg,1401101483-kitchen-appliances-contemporary.jpg
            [user_name] => admin
            [thumbnail] => images_(1).jpg
            [feature_product] => 0
        )

)

我需要像这样的结果数组

Array
(
    [0] => stdClass Object
        (
            [id] => 1522
            [image] => www.example.com/1465458797-sug.jpg,www.example.com/1465458797-rajdhanee.jpg
            [user_name] => Suganya
            [thumbnail] => Taylor_Otwell_.jpg
            [feature_product] => 1
        )

    [1] => stdClass Object
        (
            [id] => 151
            [image] => www.example.com/1465296555-14.jpg
            [user_name] => Sugan
            [thumbnail] => sug.jpg
            [feature_product] => 0
        )

    [2] => stdClass Object
        (
            [id] => 1
            [image] => www.example.com/1401101483-best-kitchen-appliances.jpg,www.example.com/1401101483-home-appliance_laundry.jpg,www.example.com/1401101483-kitchen-appliances-contemporary.jpg
            [user_name] => admin
            [thumbnail] => images_(1).jpg
            [feature_product] => 0
        )

)

即,

图像值必须将给定的URL附加到其中......

我尝试了类似下面的内容,但它对我不起作用..

    foreach ($result as $key => $value) {
        $req_i=0;
        $img = explode(',', $value->image);
           if(!isset($image)){
                $image = $url.$image_url.$img[$req_i];
           } else {
                // $value->image = $url.'images/'.$default_image;
                $image = $image.','.$url.$image_url.$img[$req_i];
           }
           $req_i++;
    }
    print_r($image);echo "<br>";exit;

在这里,我只是尝试打印这些值..

但它会返回这样的值。

www.example.com/1465458797-sug.jpg,www.example.com/1465296555-14.jpg,www.example.com/1401101483-best-kitchen-appliances.jpg

有人帮助我,我怎么能达到这个目的:(

谢谢,

2 个答案:

答案 0 :(得分:0)

我要保证你使用它在视图中显示为什么不在视图中传递它?

<image src="www.example.com/<?php 'echo Your image path '; ?> "

答案 1 :(得分:0)

I got it guyzz...

Finally I did it..

    $url = www.example.com/
    foreach ($result as $key => $value) {
        $image = array();
        $img = explode(',', $value->image);
        foreach ($img as $key_in => $value_in) {
            $image[] = $url.$value_in;
        }
        $value->image = implode(',', $image);
    }

And I get final array as

Array
(
    [0] => stdClass Object
        (
            [id] => 1522
            [image] => www.example.com/1465458797-sug.jpg,www.example.com/1465458797-rajdhanee.jpg
            [user_name] => Suganya
            [thumbnail] => Taylor_Otwell_.jpg
            [feature_product] => 1
        )

    [1] => stdClass Object
        (
            [id] => 151
            [image] => www.example.com/1465296555-14.jpg
            [user_name] => Sugan
            [thumbnail] => sug.jpg
            [feature_product] => 0
        )

    [2] => stdClass Object
        (
            [id] => 1
            [image] => www.example.com/1401101483-best-kitchen-appliances.jpg,www.example.com/1401101483-home-appliance_laundry.jpg,www.example.com/1401101483-kitchen-appliances-contemporary.jpg
            [user_name] => admin
            [thumbnail] => images_(1).jpg
            [feature_product] => 0
        )

)

Thank you everyone who are all trying to help me.. :)