如何在php中替换嵌套数组值

时间:2016-05-17 07:42:26

标签: php arrays

Array
(
    [0] => Array
        (
            [hotel_id] => 79
            [logo] => 1463466926-97157549.jpg
        )

    [1] => Array
        (
            [hotel_id] => 78
            [logo] => 1463466942-15603675.jpg
        )

    [2] => Array
        (
            [hotel_id] => 77
            [logo] => 1463466953-25200244.jpg
        )

    [3] => Array
        (
            [hotel_id] => 76
            [logo] => 1463466967-62926110.jpg
        )

    [4] => Array
        (
            [hotel_id] => 75
            [logo] => 
        )

    [5] => Array
        (
            [hotel_id] => 74
            [logo] => 
        )

)

我的数组值。

在这里,我发送一些值,例如hotel_id&该酒店的logo ..

但我需要将徽标作为图片网址发送

即:为此:1463466926-97157549.jpg 我需要发送为 http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg

通过添加该图像的路径..

最后我的阵列应该是这样的..

Array
(
    [0] => Array
        (
            [hotel_id] => 79
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg
        )

    [1] => Array
        (
            [hotel_id] => 78
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466942-15603675.jpg
        )

    [2] => Array
        (
            [hotel_id] => 77
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466953-25200244.jpg
        )

    [3] => Array
        (
            [hotel_id] => 76
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466967-62926110.jpg
        )

    [4] => Array
        (
            [hotel_id] => 75
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
        )

    [5] => Array
        (
            [hotel_id] => 74
            [logo] => http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg
        )

)

这里,最后两家酒店有空图像。

为此,我发送了一个默认图片网址,例如 http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg

像这样......

有人可以帮助我..

提前致谢...

我试过这样的

$im =array();
foreach ($Roo as $key => $value) 
{
    $im[]=(\URL::to('').'/'.$value['logo']);
}

此处 \URL::to('').'/' 是我的路径,$Roo是我的数组

但是,它只检索单独数组中的logo

5 个答案:

答案 0 :(得分:4)

循环遍历数组并按如下方式更新数组:

foreach ($array as $key => &$value) {
    if ($value['logo'] != '') {
        $value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$value['logo'];
    } else {
        $value['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg';
    }
}
print_r($array);

答案 1 :(得分:1)

foreach($hotel_details as $key => $detail) {

    if(!empty($detail['logo']) {

        $detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/'.$detail['logo']
        $hotel_destails[$key] = $detail

    } else {

        $detail['logo'] = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg'
        $hotel_destails[$key] = $detail

    }
}

答案 2 :(得分:1)

假设您的阵列名称为$arr,因此如果hotel_id匹配,请开始遍历整个阵列并将徽标替换为新徽标。

使用&表示数组是引用,因此您在此处更改的内容必须在主数组中更新,这是可选的。

$path = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
$hotel_id = 79;

foreach($arr as $key => &$value){
    if($value['hotel_id'] == $hotel_id){
       $value['logo'] = $path.$value['logo'];
       break;
    }
}

print_r($arr);
  

注意:如果你想改变整个阵列那么它会很容易,   只需删除if条件。

答案 3 :(得分:0)

您应该查看array_map() function

使用此条目数组:

Array
(
    [0] => Array
        (
            [hotel_id] => 0
            [logo] => image_0.jpg
        )

    [1] => Array
        (
            [hotel_id] => 1
            [logo] => image_1.jpg
        )

    [2] => Array
        (
            [hotel_id] => 2
            [logo] => image_2.jpg
        )

    [3] => Array
        (
            [hotel_id] => 3
            [logo] => 
        )

    [4] => Array
        (
            [hotel_id] => 4
            [logo] => image_4.jpg
        )

)

您可以获得此输出数组:

Array
(
    [0] => Array
        (
            [hotel_id] => 0
            [logo] => http://some_addr.tld/some_dir/some_sub_dir/image_0.jpg
        )

    [1] => Array
        (
            [hotel_id] => 1
            [logo] => http://some_addr.tld/some_dir/some_sub_dir/image_1.jpg
        )

    [2] => Array
        (
            [hotel_id] => 2
            [logo] => http://some_addr.tld/some_dir/some_sub_dir/image_2.jpg
        )

    [3] => Array
        (
            [hotel_id] => 3
            [logo] => http://some_addr.tld/some_dir/some_sub_dir/300x300.jpg
        )

    [4] => Array
        (
            [hotel_id] => 4
            [logo] => http://some_addr.tld/some_dir/some_sub_dir/image_4.jpg
        )

)

只需执行此操作:

$base = 'http://some_addr.tld/some_dir/some_sub_dir/';

$default = '300x300.jpg';

$res = array_map(
    function($x) use ($base, $default)
    {
        if ( array_key_exists( 'logo', $x ) )
        {
            if ( !empty( $x['logo'] ) )
            {
                $x['logo'] = $base . $x['logo'];
            }
            else
            {
                $x['logo'] = $base . $default;
            }
        }

        return $x;
    }
    , $arr);

$arr是包含酒店的数组,$res将是修改过的数组。

答案 4 :(得分:0)

您可以通过以下代码来实现此目的:

match (a1:A {name:'a1'}), (b1:B {name:'b1'}), (b2:B {name:'b2'}), 
(c1:C {name:'c1'}), (c2:C {name:'c2'}), (d1:D {name:'d1'})
merge (a1)<-[:BA]-(b1)
merge (a1)<-[:BA]-(b2)
merge (b1)-[:BC]->(c1)
merge (b1)-[:BC]->(c2)
merge (b2)-[:BC]->(c1)
merge (b2)-[:BC]->(c2)
merge (c1)-[:CD {amount: 5}]->(d1)
merge (c2)-[:CD {amount: 7}]->(d1)