Php 打乱关联数组的一部分
Php shuffle part of associative array
我需要打乱关联数组的一部分。数组示例如下。规则:随机播放需要发生在相同的键上(广告前、广告中、广告结束)。键的顺序始终是这样的(ad-pre,ad-mid,ad-end),但是数组可能并不总是包含所有键(例如,可能没有 ad-pre 键)。因此,例如,使用键 'ad-pre' 获取所有数组项并将它们洗牌并放回相同的开始,结束索引。
我最终得到了这段代码,但它很乱,我正在寻找一种更简洁的方法。另外,next_key 可能不存在!
$data = array(
array(
"ad_type"=> "ad-pre",
"type"=> "a1"
),
array(
"ad_type"=> "ad-pre",
"type"=> "a2"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b1"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b2"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b3"
),
array(
"ad_type"=> "ad-end",
"type"=> "c1"
)
);
echo '<pre>';
var_dump($data);
echo '</pre>';
$sub = array();
$index = 0;
$len = 0;
$start;
$key = 'ad-mid';
$next_key = 'ad-end';
foreach($data as $row){
if($row['ad_type'] == $key){
if(!isset($start))$start = $index;
$len++;
}
else if($row['ad_type'] == $next_key){//I want to break by next key (so it doesnt loop all array), but this is not good because this key may not exist!
break;
}
$index++;
}
var_dump($start,$len);
$sub = array_splice($data, $start, $len);
shuffle($sub);
array_splice($data, $start,0, $sub);
echo '<pre>';
var_dump($data);
echo '</pre>';
我在这里假设您的输入数组 仅 分别包含这三个 ad_types。你想在 所有 中洗牌,如果超过这三个,并且输入数组中没有其他需要按原样保留的东西。
// group items in a helper array, under the ad_type
$helper = [];
foreach($data as $item) {
$helper[$item['ad_type']][] = $item;
}
// loop over the grouped ad_type arrays, shuffle their items,
// add shuffled items to result array
$result = [];
foreach($helper as $items) {
shuffle($items);
$result = array_merge($result, $items);
}
var_dump($result);
我需要打乱关联数组的一部分。数组示例如下。规则:随机播放需要发生在相同的键上(广告前、广告中、广告结束)。键的顺序始终是这样的(ad-pre,ad-mid,ad-end),但是数组可能并不总是包含所有键(例如,可能没有 ad-pre 键)。因此,例如,使用键 'ad-pre' 获取所有数组项并将它们洗牌并放回相同的开始,结束索引。
我最终得到了这段代码,但它很乱,我正在寻找一种更简洁的方法。另外,next_key 可能不存在!
$data = array(
array(
"ad_type"=> "ad-pre",
"type"=> "a1"
),
array(
"ad_type"=> "ad-pre",
"type"=> "a2"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b1"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b2"
),
array(
"ad_type"=> "ad-mid",
"type"=> "b3"
),
array(
"ad_type"=> "ad-end",
"type"=> "c1"
)
);
echo '<pre>';
var_dump($data);
echo '</pre>';
$sub = array();
$index = 0;
$len = 0;
$start;
$key = 'ad-mid';
$next_key = 'ad-end';
foreach($data as $row){
if($row['ad_type'] == $key){
if(!isset($start))$start = $index;
$len++;
}
else if($row['ad_type'] == $next_key){//I want to break by next key (so it doesnt loop all array), but this is not good because this key may not exist!
break;
}
$index++;
}
var_dump($start,$len);
$sub = array_splice($data, $start, $len);
shuffle($sub);
array_splice($data, $start,0, $sub);
echo '<pre>';
var_dump($data);
echo '</pre>';
我在这里假设您的输入数组 仅 分别包含这三个 ad_types。你想在 所有 中洗牌,如果超过这三个,并且输入数组中没有其他需要按原样保留的东西。
// group items in a helper array, under the ad_type
$helper = [];
foreach($data as $item) {
$helper[$item['ad_type']][] = $item;
}
// loop over the grouped ad_type arrays, shuffle their items,
// add shuffled items to result array
$result = [];
foreach($helper as $items) {
shuffle($items);
$result = array_merge($result, $items);
}
var_dump($result);