合并数组的数组建立在PHP随机指标 [英] Merge array into established array at random indexes in PHP

查看:234
本文介绍了合并数组的数组建立在PHP随机指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个图片库,想扔在一些促销横幅在随机点,以促进用户一定的优惠。鉴于以下两个阵列已经从数据库查询过滤:

I'm building an image gallery and want to throw some promo banners in at random points to promote certain offers to users. Given the following two arrays have been filtered from a database query:

媒体图像阵列:

 Array
 (
      [0] => Array
         (
             [insertDate] => 2014-11-10 11:22:58
             [keyword] => standard
             [mediaClass] => image
             [mediaURL] => http://image1.jpg
             [promoURL] => 
         )

      [2] => Array
         (
             [insertDate] => 2014-11-10 11:23:18
             [keyword] => standard
             [mediaClass] => image
             [mediaURL] => http://image3.jpg
             [promoURL] => 
         )

      [3] => Array
         (
             [insertDate] => 2014-11-10 11:23:28
             [keyword] => standard
             [mediaClass] => image
             [mediaURL] => http://image4.jpg
             [promoURL] => 
         )

      [5] => Array
         (
            [insertDate] => 2014-11-10 11:23:48
            [keyword] => standard
            [mediaClass] => image
            [mediaURL] => http://image6.jpg
            [promoURL] => 
         )
    )

促销图像阵列:

    Array
    (
       [1] => Array
          (
            [insertDate] => 2014-11-10 11:23:08
            [keyword] => promo
            [mediaClass] => image
            [mediaURL] => http://image2.jpg
            [promoURL] => http://www.google.com
          )

       [4] => Array
          (
            [insertDate] => 2014-11-10 11:23:38
            [keyword] => promo
            [mediaClass] => image
            [mediaURL] => http://image5.jpg
            [promoURL] => http://www.google.com
          )
     )

我怎样才能插入宣传图片为随机指标在媒体图像阵列,同时通过的 insertDate媒体图片维护排序顺序?

How can I insert the promo images into the media images array at random indexes while maintaining the sort order by insertDate of the media images?

即添加促销横幅成图像的时间表。

i.e Adding promo banners into a timeline of images.

推荐答案

这是我会做什么:

<?php
$promo = Array (
   Array ('insertDate' => '2014-11-10 11:23:08', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image2.jpg', 'promoURL' => 'http://www.google.com'),
   Array ('insertDate' => '2014-11-10 11:23:38', 'keyword' => 'promo', 'mediaClass' => 'image', 'mediaURL' => 'http://image5.jpg', 'promoURL' => 'http://www.google.com')
 );

$media = Array (
  Array ('insertDate' => '2014-11-10 11:22:58', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image1.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:18', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image3.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:28', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image4.jpg', 'promoURL' => '', ),
  Array ('insertDate' => '2014-11-10 11:23:48', 'keyword' => 'standard', 'mediaClass' => 'image', 'mediaURL' => 'http://image6.jpg', 'promoURL' => '', )
);

//sort the promo in random order. This ensures they go into media in random order
shuffle($promo);

//get random keys to insert the promo image before
$randKeys = array_rand($media, count($promo));
//sort by the random key value in reverse. By inserting in reverse order,
//we won't have an issue with needing to increment keys to prevent two promo
//images ending up next to each other. Also, this is why we shuffle the promo
//array above, so the promo images go in random order because this is no longer random.
rsort($randKeys);

//loop over the random keys and insert the next promo image before each key
foreach($randKeys as $key){
    //get the first promo image and remove it
    $promoImage = array_shift($promo);
    //splice the promo image into the media array
    array_splice($media, $key, 0, array($promoImage));
}

//display
print_r($media);

,唯一要注意的是,无论我的媒体和宣传阵列基于从零开始数字索引和毗连这是默认的阵列,而不是像您发布的阵列,他们 1 4 键从媒体数组中删除。

Only thing to note is that both my media and promo arrays are numeric index based starting at zero and contiguous which is default for an array and not like the arrays you posted where they 1 and 4 keys are removed from the media array.

这篇关于合并数组的数组建立在PHP随机指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆