douqin231881 2014-10-18 18:12
浏览 39
已采纳

链接2阵列随机相互

I am creating a 'Tips' section which changes on refresh. I had only text earlier but now there is an image associate with each tip.

I am randomising both arrays which is wrong. How do I link them?

It should be 'Tip 1' => '1.jpg', 'Tip 2' => '2.jpg', 'Tip 3' => '3.jpg'

Any suggestions would be highly appreciated.

<?php
    function array_random($arr, $num = 1) {
            shuffle($arr);

            $r = array();
            for ($i = 0; $i < $num; $i++) {
            $r[] = $arr[$i];
        }
         return $num == 1 ? $r[0] : $r;
    }

    $a = array(
        "Tip 1", 
        "Tip 2", 
        "Tip 3");

    $img = array(
        "1.jpg", 
        "2.jpg", 
        "3.jpg");
?>

Thank you.

  • 写回答

2条回答 默认 最新

  • dougaoxian8922 2014-10-18 18:15
    关注

    Simply, join them in one array

    $tips = array();
    $tips[] = array("Tip" => "Tip text 1", "Image" => "Tip Image 1");
    $tips[] = array("Tip" => "Tip text 2", "Image" => "Tip Image 2");
    shuffle($tips);
    echo $tips[0]['Tip']; // tip text
    echo $tips[0]['Image']; // tip image
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据