0

I'm a little confused, and I need help.

I have a foreach loop in my code below. I need to put the foreach loop into the $data = array('image' => $final_image);

How can i put foreach loop in array ? Anyone can help me please.

$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage); 
 foreach($ExploreImage as $a) { 
     $newdata=$Post->Post_GetUploadChatImageID($a);  
     if($newdata){ 
          $final_image=$base_url."uploads/images/".$newdata['uploaded_image'];  
     } 
     echo $final_image;
 } 
 if($GetNewMessageU){
    $json = array();
    $data = array(  
       'image' => $final_image, 
    ); 
     $result =  json_encode( $data );   
   echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result); 
 } 
2
  • Not sure what you are asking. Do you want to create an array of the final images? Commented Jan 25, 2019 at 20:17
  • @JasonK Yes i want to create an array for final_images Commented Jan 25, 2019 at 20:26

2 Answers 2

1
$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage);

// Create an array to store final images

$final_images = [];

foreach($ExploreImage as $a) {
    $newdata=$Post->Post_GetUploadChatImageID($a);
    if($newdata){
        $final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
        // Save the image if its new data.
        $final_images[]= $final_image;
    }


}
if($GetNewMessageU){
    $json = array();
    $data = array(
        'image' => $final_images, // We pass the final images array
    );
    $result =  json_encode( $data );
    echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is working fine but there is a problem. It is repeats the last item. Like 1,2,2, there is just one 2 but it is repeating 2.
0

Made things a bit more verbose.

First define the array. Then in the if statement add element to the array.

$getImages = isset($GetNewMessageU['imageid']) ? $GetNewMessageU['imageid'] : NULL;
$ExploreImage = explode(",", $getImages);
$CountExplodes=count($ExploreImage); 
$final_images = array();                    // Define object as array
foreach($ExploreImage as $a) { 
  $newdata=$Post->Post_GetUploadChatImageID($a);  
    if($newdata){ 
      $final_image=$base_url."uploads/images/".$newdata['uploaded_image'];
      $final_images[] = array('image' => $final_image); // Will create new array item
    } 
    echo $final_image;
} 
if($GetNewMessageU){
  $json = array();
  $data = array(  
   'image' => $final_image, 
  ); 
  $result =  json_encode( $data );   
  echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $result); 
} 

5 Comments

why after the echo statement. I want to print 'image' => $final_image in echo.
please look at this screenshot. prnt.sc/mc8cwk i have two image but it is showin just one link for image
Is that's whats being sent to the server?
yes. The screenshot is from chrome developer console. It should be show two image link in array but your answer showing one image link. Because there is exploded two image id.
I would do a dump on the $ExploreImage to make sure that has multiple records.

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.