0
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] => 
        )

)

Its my array values.

Here I send some values such as hotel_id & logo of that hotel..

But I should need to send the logo as image URL

i.e:,for this:1463466926-97157549.jpg I need to send as http://localhost/abservetech/laravel/abserve_crud_travelz/public/1463466926-97157549.jpg

By adding the path of that image..

And finally my array should be like this..

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
        )

)

Here, the last two hotels having null images.

For that I send an default image URL such as http://localhost/abservetech/laravel/abserve_crud_travelz/public/300x300.jpg

like this..

Someone could help me please..

Thanks in advance...

I have tried like this

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

Here, \URL::to('').'/' is my path AND $Roo is my array

But,Instead it retrieve only the logo in separate array.

4
  • What you have tried so far? Commented May 17, 2016 at 7:45
  • Very unclear and fuzzy ... I would suggest to change your question: what are the calls (inputs) you expect? What are the outputs? Could you provide a working case? Could you provide some tries you've done? Commented May 17, 2016 at 7:48
  • using foreach loop, traverse the whole array and replace your logo with new one if hotel_id is match. Commented May 17, 2016 at 7:49
  • @ Frayne Konok can you explain with an example..please Commented May 17, 2016 at 7:54

5 Answers 5

4

loop through your array and update your array as follows :

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Simple and easy :)..But I don't know why did you use & before $value,After adding that & only the results came
& is used as a reference. See what references are in php.net/manual/en/language.references.php
Hoo fine..Thank you :)
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

    }
}

Comments

1

Say your array name is $arr, so start traversing the whole array and replace your logo with new one if hotel_id is match.

Using the & you mean that the array is reference so what you change here must update in the main array, this is optional.

$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);

Note: If you want to change the whole array then it will be very easy, just remove the if condition.

2 Comments

Thank you Frayne Konok,Why did you assign values for hotel_id,In my array the hotel_id could randomly changed as per my results
Why you use this line Here I send some values such as hotel_id & logo of that hotel..???
0

There you should check about array_map() function.

With this entry array :

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
        )

)

You can get this output array :

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
        )

)

By simply doing this :

$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);

Where $arr is the array containing your hotels, and $res will be the modified array.

Comments

0

you can do achieve this by following code:

$url = 'http://localhost/abservetech/laravel/abserve_crud_travelz/public/';
foreach ($arr as &$value) {
    if ($value['logo'] != '') {
        $value['logo'] = $url.$value['logo'];
    } else {
        $value['logo'] = $url;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.