1

I want add extra text to a particular element in array, from below INPUT, I want to change "Image":"12001116" to "Image":"wp-content/upload/12001116.jpg".

So I want add "wp-content/upload/" $value['Image']; ".jpg" Could someone please, Help!

INPUT

$json = '[
{
"Image":"12001116",
"Name":"Jean-Marc",
"CODE_POSTAL":"12630 ",
"VIL":"AGEN D AVEYRON",
"LATITUDE":"44.343518",
"LONGITUDE":"2.716004"
},
{
"Image":"1200558",
"Name":"Aurélien ",
"CODE_POSTAL":"12630 ",
"VIL":"AGEN D AVEYRON",
"LATITUDE":"42.343828",
"LONGITUDE":"2.920056"
}
]';

and OUTPUT should be

$json = '[
{
"Image":"wp-content/upload/12001116.jpg",
"Name":"Jean-Marc",
"CODE_POSTAL":"12630 ",
"VIL":"AGEN D AVEYRON",
"LATITUDE":"44.343518",
"LONGITUDE":"2.716004"
},
{
"Image":"wp-content/upload/1200558.jpg",
"Name":"Aurélien ",
"CODE_POSTAL":"12630 ",
"VIL":"AGEN D AVEYRON",
"LATITUDE":"42.343828",
"LONGITUDE":"2.920056"
}
]';
Ranjit T
  • 53
  • 5

1 Answers1

1

You can use array_map() to loop over the objects:

$json = json_decode($input); // $input is the JSON input string
$newJson = array_map(function ($obj) {
      $obj->Image = 'wp-content/upload/' . $obj->Image . '.jpg';
      return $obj;
}, $json);
$output = json_encode($newJson, JSON_PRETTY_PRINT);
echo $output; // done!

EDIT: If you don't want backslashes \, then change line 6 to:

$output = json_encode($newJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
Ar Rakin
  • 534
  • 3
  • 15
  • 1
    Missing extensions when concatenating. Please check your results. – Progrock Feb 02 '22 at 09:57
  • @RanjitT I'm checking... – Ar Rakin Feb 02 '22 at 09:57
  • 1
    @RanjitT please give specifics rather than 'some error'. The above code works for me (minus jpg extensions). Is `$input` assigned/equal to your JSON string? – Progrock Feb 02 '22 at 09:58
  • Hello, Thank you so much for your response. I've tried it, it seems to have some error. Infact, its showing "wp-content\ /upload\ /12001116" whic means its adding \/ instead of just / and also at the end, .jpg is missing. Could you please make it work – Ranjit T Feb 02 '22 at 09:59
  • Infact, its showing "wp-content\ /upload\ /12001116" which means its adding \/ instead of just / and also at the end, .jpg is missing. Could you please make it work – Ranjit T Feb 02 '22 at 10:00
  • @RanjitT Yes, it'll convert every `/` to `\/` for some safety reasons. I've updated the answer. Now it'll append `.jpg` after the image path. I checked the above code and it works. – Ar Rakin Feb 02 '22 at 10:04
  • 1
    @ArRakin , thank you so much you are a life saver. Its now showing "wp-content\/upload\/12001116.jpg" is it possible to make every \/ to just / because this is to load the image the server... but with this "\/" its not loading. Thanks again – Ranjit T Feb 02 '22 at 10:10
  • @RanjitT Sure! Check out the updated answer and have a nice day! – Ar Rakin Feb 02 '22 at 10:11
  • 1
    @ArRakin My goodness! It worked like a charm. Thank you so much. you too have a great day! – Ranjit T Feb 02 '22 at 10:13
  • 1
    @ArRakin Sorry to interrupt you again. if possible. could you please have a look at this. https://stackoverflow.com/questions/70948560/trying-to-remove-nesting-from-json-array-in-php – Ranjit T Feb 02 '22 at 10:23
  • 1
    @ArRakin Did you check this bro ? https://stackoverflow.com/questions/70958044/remove-intial-nesting-in-json-array-with-php – Ranjit T Feb 02 '22 at 15:14