0

私は、この方法で文字列にいくつかの情報を保持するラテン語の Web サイトに取り組んでいます。

nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is

この情報をこのような配列に変換したいと思います

array(
    'nominative'=>array(
        0=>array('us','a','um')
    ),
    'genitive'=>array(
        0=>'i'
    )
    'dative'=>array(
        1=>'is'
    )
    'ablative'=>array(
        1=>'is'
    )
)

私の現在のコードは次のようになります

//turn into array for each specification
$specs=explode(';',$specificities);
foreach($specs as $spec):
    //turn each specification into a consecutive array
    $parts=explode(':',$spec);
    //turn multiple value into array
    foreach($parts as &$value)
        if(strpos($value,',')!==false)
            $value=explode(',',$value);
    //imbricate into one other
    while(count($parts)!==1):
        $val=array_pop($parts);
        $key=array_pop($parts);
        if(is_array($key)):
            foreach($key as $k)
                $parts[$k]=$val;
        else:
            $parts[$key]=$val;
        endif;
    endwhile;
endforeach;

私は立ち往生しています。ヘルプ。


編集:皆さん、迅速な対応に感謝します!私が好んだ応答は、CaCtus からのものでした。柔軟性を追加するために修正しましたが、これが最も便利でした。

興味のある方のために、新しいコードは次のとおりです。

$parts=explode(';','nominative:0:er,ra,rum;genitive:0:i;dative,ablative:1:is;root:r;');
if(!empty($parts)&&!empty($parts[0])):
    foreach($parts as $part):
        $subpart=explode(':',$part);
        $keys=explode(',',$subpart[0]);
        foreach($keys as $key):
            @$vals=(strpos($subpart[2],',')!==false)
                ?explode(',',$subpart[2])
                :$subpart[2];
            $final=(is_null($vals))
                ?$subpart[1]
                :array($subpart[1]=>$vals);
        endforeach;          
    endforeach;
endif;
4

3 に答える 3

0

もう少し単純なものは機能しますか?

関数 multiexplode ($delimiters,$string) {

$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return  $launch; }

$input = "0:us,a,um;属格:0:i;与格、奪格:1:is";

$exploded = multiexplode(array(",",":",";"),$input);

$test = array( 'nominative'=>array(0=>array($exploded[1],$exploded[2],$exploded[3])), $exploded[4]=>array(0=>$ Exploded[6]), $exploded[7]=>array(1=>$exploded[10]), $exploded[8]=>array(1=>$exploded[10]) );

print_r($テスト);

于 2014-07-20T18:18:03.030 に答える
0

別の代替手段:

$specificities = "nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is";

$specificities = trim($specificities, ";") . ";"; //mandatory a trailing ;

$pattern = "/(?<types>[a-z\,]+):(?<ids>\d):(?<values>.*?);/";

preg_match_all($pattern, $specificities, $matches, PREG_SET_ORDER);


$result = array();

array_map(function($item) use (&$result)
{
    $types = explode("," , $item['types']);

    if(count($types) == 1)
    {
        $result[$item['types']] = array(
            $item['ids'] => explode("," , $item['values'])
        );
    }
    else 
    {
        foreach ($types as $type) 
        {
            $result[$type] = array(
                $item['ids'] => explode("," , $item['values'])
            );
        }
    }    
}, $matches);

var_dump($result);
于 2014-07-20T18:54:36.297 に答える
0
$string = 'nominative:0:us,a,um;genitive:0:i;dative,ablative:1:is';
$finalArray = array();

// Separate each specification
$parts = explode(';', $string);
foreach($parts as $part) {
    // Get values : specification name, index and value
    $subpart = explode(':', $part);

    // Get different specification names
    $keys = explode(',', $subpart[0]);

    foreach($keys as $key) {            
        if (preg_match('#,#', $subpart[2])) {
        // Several values
            $finalValues =  explode(',', $subpart[2]);
        } else {
        // Only one value
            $finalValues = $subpart[2];
        }

        $finalArray[$key] = array(
            $subpart[1] => $finalValues
        );
    }
}
于 2014-07-20T17:58:20.153 に答える