随机没有,但从给定的没有

时间:2017-07-22 19:44:35

标签: php mysql

想要$ ar数组必须为$ arr的每个元素shuffle,$ query结果有$ row等于$ ar的元素

    $arr =["11","12"];
    $t_length= count($arr);
    for($i=0; $i < $t_length; $i++)
    {
         $ar=["1","2","3","4","5"];
         foreach ($query->result() as $row)
         {
              $data= array("stdid"=>$arr[$i],"tid"=>1);
                // tid for 11 may be some thing like
                // $ar=["1","2","3","4","5"]; and for next item  which 
                //12 may be $ar=["5","4","1","2","3"]; 
           }
       }

1 个答案:

答案 0 :(得分:1)

如果public class DateBetweenAgesAttributeAdapter : AttributeAdapterBase<DateBetweenAgesAttribute> { private readonly DateBetweenAgesAttribute _attribute; public DateBetweenAgesAttributeAdapter(DateBetweenAgesAttribute attribute, IStringLocalizer localizer) : base(attribute, localizer) { _attribute = attribute; } public override void AddValidation(ClientModelValidationContext context) { MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-datebetweenages", GetErrorMessage(context)); MergeAttribute(context.Attributes, "data-val-datebetweenages-min", _attribute.MinProperty); MergeAttribute(context.Attributes, "data-val-datebetweenages-max", _attribute.MaxProperty); } public override string GetErrorMessage(ModelValidationContextBase validationContext) { return GetErrorMessage(validationContext.ModelMetadata, validationContext.ModelMetadata.GetDisplayName(), //would like to pass the actual minvalue in here, //would like to pass the actual maxvalue in here); } .. $row具有相同的元素数,我就不会看到两个循环的原因。您的原始代码将进行四次迭代,我不确定您想要的是什么。 (根据我的不确定性和反对票数,你应该尽快更新你的问题。)

代码:(Demo

$ar

输出:

$arr=['11','12'];
$ar=['1','2','3','4','5'];

// shuffle and index $data subarrays
foreach($arr as $v){
    shuffle($ar); // just shuffle before the next loop
    $data[]=['stdid'=>$v,"tid"=>$ar];
}
var_export($data);

$data=[];  // clear the array
echo "\n\n---\n\n";

// or shuffle and use stdid as subarray keys for $data
foreach($arr as $v){
    shuffle($ar); // just shuffle before the next loop
    $data[$v]=["tid"=>$ar];
}
var_export($data);