想要用逗号分隔的数组中的值组合

时间:2014-07-26 09:32:28

标签: php

我在每个键下面有一个数组,其值以逗号分隔。是否可以将所有这些组合使用相同的索引?例如,它应该如下所示:

string = 'kashif','kashif1','kashif2'

这是我的代码:

  <?php
    $array = array(
        'prop41' => "'kashif' ,'tarique','nilofer'",
        'prop42' => "'kashif1' ,'tarique1','nilofer1'",
        'prop43' => "'kashif2' ,'tarique','nilofer2'",
    );
    print_r($array);
    for($i=1;$i<=3;$i++){
        for($j=1;$j<=1;$j++){
            echo "<br>prop".($j+3).$i;
        }
    }
?>  

请指导我实现目标的正确方法。

1 个答案:

答案 0 :(得分:0)

  

结果是(&#39; kashif&#39;,kashif1,..),(&#39; tarique&#39;,&#39; tarique1&#39;,...),&#39; nilofer&#39;,&#39;&#39;,&#39; nilofer2&#39;),...等等

   $array = array(
           'prop41' => "'kashif' ,'tarinq','nilofer','dissiq'",
           'prop42' => "'kashif1' ,'tarinq1'",
           'prop43' => "'kashif2' ,'tarinq2','nilofer2'",
           'prop44' => "'kashif3' ,'tarinq3','nilofer3'",
           'prop45' => "'kashif4' ,'tarinq4','nilofer4'",
           'prop46' => "'kashif5' ,'tarinq5','nilofer5'",
   );
    $array2= array();
    $counter = 0;
    $max = 0 ;
    foreach($array as $key=>$value){
        $array2[$counter] = explode(',',$value);
        if(count($array2[$counter]) > $max)
            $max = count($array2[$counter]);
        $counter++;
    }
    echo "counter = ".$counter;
    $string = '';
    for($i=0;$i<$max;$i++){
        $string .= "(";
        for($j=0;$j<$counter;$j++){
            if(!isset($array2[$j][$i]))
            {
                $string .= "''".",";
                continue;
            }
            else{
                $string .= $array2[$j][$i].",";    
            }

        }
        $string = rtrim($string, ' ,');
        $string .= "),";
    }
      print_r(rtrim($string,',('));