Php将连续数组更改为包含数组

Php将连续数组更改为包含数组,php,arrays,string,foreach,inclusion,Php,Arrays,String,Foreach,Inclusion,我在一个拉丁语网站上工作,该网站以字符串的方式保存一些信息: 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'=>arr

我在一个拉丁语网站上工作,该网站以字符串的方式保存一些信息:

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;
我当前的代码如下所示

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;
我卡住了。救命啊

编辑:谢谢大家的快速回复! 我更喜欢仙人掌的反应。我修改了它以增加灵活性,但它是最有用的

对于感兴趣的人,以下是新代码:

$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;

简单一点的东西行吗

函数multiexplode$分隔符,$string{

$ready = str_replace($delimiters, $delimiters[0], $string);
$launch = explode($delimiters[0], $ready);
return  $launch; }
$input=0:us,a,um;属格:0:i;与格,消格:1:is

$spolded=multiexplodearray,,:,;,$input

$test=数组 '主格'=>array0=>array$spreated[1],$spreated[2],$spreated[3], $SPORDED[4]=>array0=>$SPORDED[6], $spolded[7]=>array1=>$spolded[10], $spolded[8]=>array1=>$spolded[10]

打印测试


只是另一种选择:

$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);

您当前代码的结果是什么?