0

I have here an array below:

<?php
print_r( $result ); 
?>

If I am going to execute the code above, it resulted below:

Array
(
[0] => Array
    (
        [id] => 1
        [uploaded_by] => 1
        [image_url] => http://localhost/dir/img_2.jpg
        [work_description] => test
        [date_added] => 2017-08-03 02:12:38
    )

[1] => Array
    (
        [id] => 2
        [uploaded_by] => 1
        [image_url] => http://localhost/dir/img_4.jpg
        [work_description] => test
        [date_added] => 2017-08-03 02:13:04
    )

[2] => Array
    (
        [id] => 3
        [uploaded_by] => 1
        [image_url] => http://localhost/dir/img_2.jpg
        [work_description] => test
        [date_added] => 2017-08-03 02:46:28
    )

[3] => Array
    (
        [id] => 4
        [uploaded_by] => 1
        [image_url] => http://localhost/dir/img_2.jpg
        [work_description] => sdfsdf
        [date_added] => 2017-08-03 02:46:34
    )
)

Now, from the $result array I wanted to change the values of all image_url programmatically using php into an image in html.

example:

http://localhost/dir/img_2.jpg will become

<img src="http://localhost/dir/img_2.jpg"/>

Those values must be changed if I am going to execute the code.

Does anybody know?

4
  • what do you want to get after the change ? are you looking for <img src="http://localhost/dir/img_2.jpg"/> ? if yes this is already there. Pls, let us know clearly. Commented Aug 3, 2017 at 3:17
  • okay. will modify my question. Hope you can understand. Commented Aug 3, 2017 at 3:19
  • What is the problem? Loop over array and concatenate strings. Provide what you have already tried. Commented Aug 3, 2017 at 5:35
  • this has been solved already.. thanks Commented Aug 3, 2017 at 6:08

2 Answers 2

1

You can put it in a foreach and modify only the part what you want:

foreach($result as $key => $value) {
    $result[$key]['image_url'] = '<img src="'.$value['image_url'].'"/>';
}

print_r($result);
Sign up to request clarification or add additional context in comments.

2 Comments

Although, the image_url is an array and not an object. Therefore, it must be $result[$key]['image_url'] = '<img src="'.$value['image_url'].'"/>'; Just edit though for future reference. thanks
Also with the $value->image_url .. Make it an array.. thanks. The person who downvoted my question did not understand it exactly. But thanks pablo because you understood it
0

You can use a compact syntax and modify the subarray elements by reference:

Code: (Demo)

foreach ($result as &$subarray) {  // & means modify by reference, so you are overwriting the input array, not traversing a copy.
    $subarray['image_url'] = "<img src=\"{$subarray['image_url']}\"/>";
}

var_export($result);

There is no need to declare $key because the foreach is traversing the actual input array, not a copy of the input array. The image_urls are simply overwritten with each iteration.

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.