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



我正在一个拉丁网站工作,它以这种方式在字符串中保存一些信息:

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

是否可以使用更简单的方法?

函数multiexplosion ($delimiters,$string) {

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

输入美元= " 0:美国,,嗯;属格:0:我;配,烧蚀:1:是";

$ = multiexplode爆炸(数组 (",",":",";"),$

输入);

$test = array("主格"=>阵列(0 =>数组([1]爆炸,爆炸美元[2],[3]爆炸美元)),$[4] =>爆炸数组(0 =>[6]爆炸美元),$[7] =>爆炸数组(1 =>[10]爆炸美元),$[8] =>爆炸数组(1 =>[10]爆炸美元));

print_r(测试)美元;

另一种选择:

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

相关内容

  • 没有找到相关文章

最新更新