0

たとえば、文字列があります

$somestring = '11;11;11;12;13';

と配列

    $somearray = array(
  11 => array(
    prop => array(0=>11),
    uid => 1,
  ),
  21 => array(
    prop => array(0=>12),
    uid => 2,
  ),
  32 => array(
    prop => array(0=>11),
    uid => 1,
  ),  
  34 => array(
    prop => array(0=>11),
    uid => 2,
  ),  
 35 => array(
    prop => array(0=>13),
    uid => 1,
  ),  
  36 => array(
    prop => array(0=>13),
    uid => 3,
  ),  
  37 => array(
    prop => array(0=>12),
    uid => 1,
  ),  
  38 => array(
    prop => array(0=>12),
    uid => 3,
  ),  
  39 => array(
    prop => array(0=>12),
    uid => 1,
  ),
);

[prop]の値が$somestringの同じ値であるという条件で、$ somearray [uid]から取得し、次のように塗りつぶされた行で出力する方法:

row1:11(uid:1);11(uid:1);11(uid:2);12(uid:2);13(uid:1)
row2:11(uid:);11(uid:);11(uid:);12(uid:1);13(uid:3)
row3:11(uid:);11(uid:);11(uid:);12(uid:3);13(uid:)
row4:11(uid:);11(uid:);11(uid:);12(uid:1);13(uid:)

私はここhttp://eval.in/6816で少し寛大な解決策を見つけましたが、それは繰り返される値を扱っていません

4

2 に答える 2

2

コピペして実行するだけで、

<?php
error_reporting(0);
$somestring = '11;11;11;12;13';

$somearray = array(
  11 => array(
    prop => array(0=>11),
    uid => 1,
  ),
  21 => array(
    prop => array(0=>12),
    uid => 2,
  ),
  32 => array(
    prop => array(0=>11),
    uid => 1,
  ),  
  34 => array(
    prop => array(0=>11),
    uid => 2,
  ),  
 35 => array(
    prop => array(0=>13),
    uid => 1,
  ),  
  36 => array(
    prop => array(0=>13),
    uid => 3,
  ),  
  37 => array(
    prop => array(0=>12),
    uid => 1,
  ),  
  38 => array(
    prop => array(0=>12),
    uid => 3,
  ),  
  39 => array(
    prop => array(0=>12),
    uid => 1,
  ),
);

$sme_array    = explode(";",$somestring);
$somestrarray = array_unique($sme_array);
$res          = array();
foreach($somearray as $key=>$val){

    foreach($somestrarray as $value){
        if($val['prop'][0] == $value){
           $res[$value][]   = $val['uid'];
        }       
    }
}

$cnt_values  = array_count_values($somestrarray);

$num_loop    = 1;
foreach($cnt_values as $key=>$val){
  $num_loop  = (count($res[$key])/$val) > $num_loop?(count($res[$key])/$val):$num_loop;
}

for($i=0; $i<$num_loop; $i++){
    echo "row".($i+1).":";
    foreach($sme_array as $vals){
      if(array_key_exists($vals,$res)){
      echo $vals."(uid:".$res[$vals][0].");";
      array_shift($res[$vals]);
      }
    }
    echo "<br>";
}


?>
于 2013-01-16T10:08:16.503 に答える
0
<?php

$str_array=explode(';',$somestring);
$final=array();

foreach($str_array as $key => $value1)
{
 foreach($somearray as $value2)
 {
  if($value2['prop'][0]==$value1)
  {
    $final[$key][]='row'.$key.':'.$value1.'(uid:'.$value2['prop'][0].')'; // something like this
  }
 }
}
   //print_r($final);

foreach($final as $keyy => $farr)
{
 $myarr[$keyy]=implode(';',$farr);
}

//print_r($myarr);
   echo implode('<br/>',$myarr);
于 2013-01-16T09:00:43.953 に答える