0

I have an array like this.

[{
    "image": "image1.png",
    "url": "link1"
},{
    "image": "image2.png",
    "url": "link2"
},{
    "image": "image3.png",
    "url": "link3"
}]

How can I create a new array like this from the above one

["image1.png","image2.png","image3.png"]

This is my code and json encoded $main is added on the top

foreach ($main as $key =>$value){
            foreach($value as $key1 => $value1) {
         if($key1==='image'&&$key1!=url){
            $image=$value1; 
            $array12 = array( 'image' =>$image);    
        }
        }
        $array12=json_encode($array12);
        print_r($array12);
  }

The result I am getting is

{"image":"image1.png"}{"image":"image2.png"}{"image":"image3.png"}
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34

1 Answers1

2

I would do this like this:

$array12 = [];

foreach ($main as $key =>$value){
    $image=$value["image"]; 
    array_push($array12, $image);    
}

$array12=json_encode($array12);
print_r($array12);
Ronald Haan
  • 608
  • 4
  • 18