2013-03-22 85 views
0

第一值I有一個陣列的圖像從JSON:Mootools的獲得從每個

images=JSON.decode(images); 

這是數組值:

[ 
    { 
     "title": "", 
     "description": "", 
     "name": "loc commerciale.jpg" 
    }, 
    { 
     "title": "", 
     "description": "", 
     "name": "foto2.jpg" 
    }, 
    { 
     "title": "", 
     "description": "", 
     "name": "foto 1.jpg" 
    }, 
    { 
     "title": "", 
     "description": "", 
     "name": "a01.jpg" 
    } 
] 

我從名稱得到的值:

images.each(function(image){     
    alert(image.name); 
}); 

我只需要得到第一個數值名稱

L IKE PHP:

$images = (array) json_decode($row->images); 
$first = true; 
foreach ($images as $k => $image) { 
    if ($first) 
    { 
     $firstimage= $image->name; 
     $first = false; 
    } 
} 
+4

這有什麼錯:'VAR firstImageName =圖像[0]。名稱;'? – Leri 2013-03-22 13:14:42

+0

我需要在mootools中獲得第一個值名稱,例如php – user1672131 2013-03-22 13:38:53

+1

我也可以對你的php代碼提出相同的要求。爲了改變我的問題:爲什麼你想在循環中取第一個元素? – Leri 2013-03-22 13:40:48

回答

0

嘗試:

if (images[0].length){ 
    var firstImageName = images[0].name; 
}