dqnz43863 2012-11-12 10:32
浏览 30
已采纳

以自定义模式打印自定义数组

If I have array looking like :

Array ( [date_type] => Date [date_text] => 20/12/2012 [place_type] => Place [place_text] => NY )

How can I print result like :

<span>Date</span><div>20/12/2012</div>
<span>Place</span><div>NY</div>
so on.... (if more custom array)

I came to this step now

$custom_info = array (
  "date_type" => "Date",
  "date_text" => "20/12/2012",
  "place_type"=>"Place",
  "place_text" => "NY" 
) ;
$count = count($custom_info)/2;

for ($i=1; $i<$count; $i++)
{

}

Someone can help me ?

  • 写回答

2条回答 默认 最新

  • dongzhenshen7435 2012-11-12 10:37
    关注

    if your array always has the structure that you wrote, use this

        $i = 0;
        foreach ($custom_info as $k => $v) {
            if ($i % 2 == 0){
                echo "<span>".$v."</span>";
            } else {
                echo "<div>".$v."</div>";
            }
    
            $i++;
        }
    

    tested

    explanation:

    according to your array structure, you should echo first element's value in span, and second element's value in div, and so on: $i % 2 == 0 shows that when it is element number 0, 2, 4 and so on, print in span, and if 1, 3, 5 and so on, print in div. a % b gives the remainder when you divide a on b

    $i = 0,  $i % 2 --> 0
    $i = 1,  $i % 2 --> 1
    $i = 2,  $i % 2 --> 0
    $i = 3,  $i % 2 --> 1
    $i = 4,  $i % 2 --> 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教