2017-05-16 55 views
-2

這裏我有一個數組(第一個數組),我有一個更多的數組(第二個數組),現在我想只顯示來自第二個數組的第一個圖像(galleryImages) ,怎麼做到這一點。我試過但我無法得到結果如何獲得陣列內唯一的第一個圖像

print_r($ response);

Array 
(
    [0] => stdClass Object 
     (
      [gallery_id] => 2 
      [title] => Annual Day 2017 
      [description] => 
      [galleryImages] => ["1.jpg","2.jpg","3.jpg","4.jpg"] 
      [reg_on] => 2017-05-17 01:55:12 
      [created_by] => [email protected] 
      [school_id] => 2 
      [status] => 0 
     ) 

    [1] => stdClass Object 
     (
      [gallery_id] => 3 
      [title] => Sports Day 
      [description] => 
      [galleryImages] => ["1.jpg","2.jpg","3.jpg"] 
      [reg_on] => 2017-05-17 01:55:36 
      [created_by] => [email protected] 
      [school_id] => 2 
      [status] => 0 
     ) 

) 

預期結果

{ 
    "status": "Success", 
    "data": [ 
    { 
     "gallery_id": "2", 
     "title": "Annual Day 2017", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery":"1.jpg" 
    }, 
    { 
     "gallery_id": "3", 
     "title": "Sports Day 2017", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery":"1.jpg" 
    } 
    ], 
} 

我想這樣的,但我沒有得到確切的結果

$images = array(); 
foreach ($response as $key => $value) 
{ 
    $img['gallery_id'] = $value->gallery_id; 
    $img['title'] = $value->title; 
    $img['description'] = $value->description; 
    $img['galleryImagesCount'] = count(json_decode($value->galleryImages,true)); 
    $img['gallery'] = json_decode($value->galleryImages,true); 
    array_push($images,$img); 

} 

$return=array('status'=>"Success",'Images'=>$images); 
echo json_encode($return); 

獲取結果

{ 
    "status": "Success", 
    "Images": [ 
    { 
     "gallery_id": "2", 
     "title": "Annual Day 2017", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery": [ 
     "d17ac9d0aeb6435eaa294e0d69d4cc8f.jpg", 
     "a91945e0cf55379f51cf5faef10d7a4a.jpg", 
     "2d1501045ddbb3ccc238e70f9af05027.jpg", 
     "071c3b5f969bed1d1e2ee4b6531e4444.jpg" 
     ] 
    }, 
    { 
     "gallery_id": "3", 
     "title": "Sports Day", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery": [ 
     "f0ba574fd46a01ff5a41855a97c710ca.jpg", 
     "1d10802f1b74e660117f36bd6dd0aa26.jpg", 
     "e705fb66f767a1b914200ca8d3cae700.jpg", 
     "3d5d8828331e13d3decc94021a64e5ca.jpg" 
     ] 
    } 
    ] 
} 

這裏發生了什麼手段畫廊即將到來的數組,我不想陣列首先我應該只圖像,請檢查我的預期結果,更新的答案

  • 更新預期結果
{ 
    "status": "Success", 
    "Images": [ 
    { 
     "gallery_id": "2", 
     "title": "Annual Day 2017", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery": [ 
     { 
     "galleryimage": "1.jpg" 
     }, 
     { 
     "galleryimage": "2.jpg" 
     } 
     ] 
    }, 
    { 
     "gallery_id": "3", 
     "title": "Sports Day", 
     "description": "", 
     "galleryImagesCount": 4, 
     "gallery": [ 
     { 
     "galleryimage": "1.jpg" 
     }, 
     { 
     "galleryimage": "2.jpg" 
     } 
     ] 
    } 
    ] 
} 
+2

'$ IMG [ '畫廊'] = json_decode($值 - > galleryImages,true)[0];'php5.4以上。 –

+0

我知道了,謝謝 –

+0

Mr @ u_mulder,我有一個疑問,這裏的圖庫是在數組中顯示的,如何使圖庫中的對象,請檢查我更新的預期結果 –

回答

0

代替[galleryImages] => ["1.jpg","2.jpg","3.jpg"],用於遍歷galleryImages進行迭代。

使用key =>value對創建一個關聯數組,如[galleryImages] => ["galleryimage1" => "1.jpg", "galleryimage2" => "2.jpg", "galleryimage3" => "3.jpg"]

雖然json_decode你會得到預期的輸出。

0

我的改變和解釋是在代碼塊:

代碼:(Demo

// assumed that previous line was something like $response=json_decode($json); 
$response=[ 
    (object)[ 
     'gallery_id'=>2, 
     'title'=>'Annual Day 2017', 
     'description'=>'', 
     'galleryImages'=>["1.jpg","2.jpg","3.jpg","4.jpg"], 
     'reg_on'=>'2017-05-17 01:55:12', 
     'created_by'=>'[email protected]', 
     'school_id'=>2, 
     'status'=>0 
    ], 
    (object)[ 
     'gallery_id'=>3, 
     'title'=>'Sports Day', 
     'description'=>'', 
     'galleryImages'=>["1.jpg","2.jpg","3.jpg"], 
     'reg_on'=>'2017-05-17 01:55:36', 
     'created_by'=>'[email protected]', 
     'school_id'=>2, 
     'status'=>0 
    ] 
]; 

foreach ($response as $value){ // removed $key=> because it was unnecessary 
    $img['gallery_id'] = $value->gallery_id; 
    $img['title'] = $value->title; 
    $img['description'] = $value->description; 
    $img['galleryImagesCount'] = count($value->galleryImages); // removed json_decode() 
    $img['gallery'] = $value->galleryImages[0]; // removed json_decode and added [0] to access first 
    $images[]=$img; // swapped push() call with identical function-less "push" 
} 

if(isset($images)){ // added this condition to ensure there was something to return 
    $return=array('status'=>"Success",'Images'=>$images); 
    //var_export($return); 
    echo json_encode($return); 
}else{ 
    // enter some sort of error message/default behavior 
} 

輸出:

{"status":"Success","Images":[{"gallery_id":2,"title":"Annual Day 2017","description":"","galleryImagesCount":4,"gallery":"1.jpg"},{"gallery_id":3,"title":"Sports Day","description":"","galleryImagesCount":3,"gallery":"1.jpg"}]} 
相關問題