我如何在php中回应这种数组?

时间:2015-11-10 09:03:38

标签: php

我的数组如下:

[images] => Array
(
        [0] => Array
                (
                        [id] => 1
                        [path] => ../images/properties/1/1447053991.jpg
                )

        [1] => Array
                (
                        [id] => 3
                        [path] => ../images/properties/1/1447054231.jpg
                )

        [2] => Array
                (
                        [id] => 4
                        [path] => ../images/properties/1/1447054666.jpg
                )

        [3] => Array
                (
                        [id] => 17
                        [path] => ../images/properties/1/1447141341.jpg
                )

)

当我需要回显这个数组时,我想为这个数组的第一个键添加不同的HTML,并为其他键添加不同的HTML。

这是我尝试的方式:

foreach($property['images'] as $image) {
    //echo '<pre>',print_r($image).'</pre>';
    if ($image['id'] != '') {                   
        $html  .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>\n";
        $html  .= "     <img src='".$image['path']."' alt=''/>\n";
        $html  .= "     <span class='zoom-icon'></span>\n";
        $html  .= " </a>\n";
    } else {
        $html  .= " <a href='".$image['path']."' title='1' rel='prettyPhoto[group]'></a>\n";
    }
}

但它不适合我。希望有人可以帮助我。

2 个答案:

答案 0 :(得分:2)

因为您希望将first image显示为图片,其余部分显示在锚标记中,请尝试此操作。使用counter标识firstrest。可以有两个例。

当您的索引未以0开头时,

案例1 。     

$property=array('images' => Array
(
        '0' => Array
                (
                        'id' => 1,
                        'path' => '../images/properties/1/1447053991.jpg'
                ),

        '1' => Array
                (
                        'id' => 3,
                        'path' =>'../images/properties/1/1447054231.jpg'
                )
        )
        );


$html="";
$counter=0;
foreach($property['images'] as $image) {
    if ($image['id'] != '') {
        if($counter == 0){
            $html  .= "     <img src='".$image['path']."' alt=''/>\n";
            $counter++;
        }else{
        $html  .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>".$image['id']."\n";

        $html  .= "     <span class='zoom-icon'></span>\n";
        $html  .= " </a>\n";
        }
    } else {
        $html  .= " <a href='".$image['path']."' title='1' rel='prettyPhoto'group''></a>\n";
    }
}


echo $html;
?>
索引从0开始时

案例2:

foreach($property['images'] as $key=>$image) {
        if ($image['id'] != '') {
            if($key == 0){
                $html  .= "     <img src='".$image['path']."' alt=''/>\n";
            }else{
            $html  .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>".$image['id']."\n";

            $html  .= "     <span class='zoom-icon'></span>\n";
            $html  .= " </a>\n";
            }
        } else {
            $html  .= " <a href='".$image['path']."' title='1' rel='prettyPhoto'group''></a>\n";
        }
    }

答案 1 :(得分:2)

如果我没有错,你的foreach正在按预期打印值,你希望第一个索引打印出与其他索引不同的东西..

foreach($property['images'] as $index => $image) {
   //echo '<pre>',print_r($image).'</pre>';
   if ($index == 0) {                   
     // do stuff for the first index
   } else {
     // do stuff for other indexes
   }
}