0
votes

I have a array of 3 images

$images

          for ($i=0;$i<count($image); $i++) {
                   array_push($imgArray, $image[i]);
                    $valString = implode(',', $imgArray);
                    $setting = $valString; 
                       print_r(settings);
          }

o/p: img1.jpg,img2.jpg,img3.jpg

But I wnat the o/p as

{'ad1':img1.jpg,'ad2':img2.jpg,'ad3':img3.jpg}

i.e like a json.

Can anyoe please suggest help.Thanks.

2
json_encode() ?James Lalor
share sample of $image value ?Niklesh Raut

2 Answers

1
votes

if your array is ['img1.jpg','img2.jpg','img3.jpg'] then you should use below code

<?php
    $images = ['img1.jpg','img2.jpg','img3.jpg'];
    foreach($images as $key=>$image){
      $images['ad'.($key+1)] = $images[$key];
      unset($images[$key]);
    }
    print_r(json_encode($images));
?>

live demo : https://eval.in/823702

0
votes
$arr = [];
for ($i=0;$i<count($image); $i++) {
    $arr['ad'.$i+1] = $image[$i];
}
$settings = json_encode($arr);
print $settings;