0

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() ? Commented Jun 28, 2017 at 12:49
  • share sample of $image value ? Commented Jun 28, 2017 at 12:50

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

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.