根据一些特定的键重新排列数组 - PHP

时间:2014-07-17 22:34:19

标签: php arrays

我是PHP的新手,所以请指导我更好地练习。基本上,我有这个数组$content,我想根据一些密钥的存在来改变它。这是我的数组:

Array
(
    [civilite] => 3
    [first_name] => first_name
    [last_name] => last_name
    [telephone] => xxxxxxxxxxx
    [fax] => xxxxxxxxx
    [telephone_portable] => xxxxxxxxxxxx
    [mail] => example@example.com
    [naissance] => xx/xx/xxx
    [profession] => xxxxxxxxx
    [activite] => 1
    [cb_moto] => 1
    [adresse_moto] => moto 1
    [cp_moto] => xxxxxx 1
    [ville_moto] => xxxxxxxxx
    [moto_permis_1] => A1
    [moto_permis_2] => B1
    [moto_permis_3] => A1
    [moto_marque] => xxxx
    [cb_auto] => 1
    [adresse_auto] => auto 2
    [cp_auto] => xxxxxxxx 2
    [ville_auto] => xxxxxxx
    [auto_permis_1] => A 2
    [auto_permis_2] => B 2
    [auto_permis_3] => A2
    [auto_marque] => xxxx
);

以下是我要将其更改为:

Array
(
    [general] => Array
                ( 
                [civilite] => 3
                [first_name] => first_name
                [last_name] => last_name
                [telephone] => xxxxxxxxxxx
                [fax] => xxxxxxxxx
                [telephone_portable] => xxxxxxxxxxxx
                [mail] => example@example.com
                [naissance] => xx/xx/xxx
                [profession] => xxxxxxxxx
                [activite] => 1
                )

    [moto] => Array
                ( 
                [cb_moto] => 1
                [adresse_moto] => moto 1
                [cp_moto] => xxxxxx 1
                [ville_moto] => xxxxxxxxx
                [moto_permis_1] => A1
                [moto_permis_2] => B1
                [moto_permis_3] => A1
                [moto_marque] => xxxx
                )

    [auto] => Array
                ( 
                [cb_auto] => 1
                [adresse_auto] => auto 2
                [cp_auto] => xxxxxxxx 2
                [ville_auto] => xxxxxxx
                [auto_permis_1] => A 2
                [auto_permis_2] => B 2
                [auto_permis_3] => A2
                [auto_marque] => xxxx
                )
  );

这个想法是,如果密钥cb_moto存在,则创建一个其中包含密钥moto的子数组,直到出现密钥cb_auto并创建另一个子数组键auto等等。
这是我到目前为止所尝试的内容:

foreach ($content as $key => $value) {
    $content ['general'][] = $value;

    if(isset($_content['cb_moto'])){
        $content ['moto'][] = $value;
    }
}

对此有何帮助?非常感谢。

3 个答案:

答案 0 :(得分:1)

如果密钥等于您要查找的密钥名称cb_motocb_auto,我们将变量设置为true或false以指示并将其数据保存在其子数组中

不是更好的方式来实现它,但易于理解和阅读:

<?php
$data = array (
    'civilite' => 3,
    'first_name' => 'first_name',
    'last_name' => 'last_name',
    'telephone' => 'xxxxxxxxxxx',
    'fax' => 'xxxxxxxxx',
    'telephone_portable' => 'xxxxxxxxxxxx',
    'mail' => 'example@example.com',
    'naissance' => 'xx/xx/xxx',
    'profession' => 'xxxxxxxxx',
    'activite' => 1,
    'cb_moto' => 1,
    'adresse_moto' => 'moto 1',
    'cp_moto' => 'xxxxxx 1',
    'ville_moto' => 'xxxxxxxxx',
    'moto_permis_1' => 'A1',
    'moto_permis_2' => 'B1',
    'moto_permis_3' => 'A1',
    'moto_marque' => 'xxxx',
    'cb_auto' => '1',
    'adresse_auto' => 'auto 2',
    'cp_auto' => 'xxxxxxxx 2',
    'ville_auto' => 'xxxxxxx',
    'auto_permis_1' => 'A 2',
    'auto_permis_2' => 'B 2',
    'auto_permis_3' => 'A2',
    'auto_marque' => 'xxxx'
);

$result = array();

$cb_moto = false;
$cb_auto = false;
foreach ($data as $key => $value)
{
    if($key == 'cb_moto')
    {
        $cb_auto = false;
        $cb_moto = true;
    }

    if($key == 'cb_auto')
    {
        $cb_moto = false;
        $cb_auto = true;
    }

    if ($cb_moto)
    {
        $result['moto'][$key] = $value;
    }
    elseif($cb_auto)
    {
        $result['auto'][$key] = $value;
    }
    else
    {
        $result['general'][$key] = $value;
    }
}

print_r($result);

Live DEMO

使用strpos代替变量检查:

<?php
$data = array (
    'civilite' => 3,
    'first_name' => 'first_name',
    'last_name' => 'last_name',
    'telephone' => 'xxxxxxxxxxx',
    'fax' => 'xxxxxxxxx',
    'telephone_portable' => 'xxxxxxxxxxxx',
    'mail' => 'example@example.com',
    'naissance' => 'xx/xx/xxx',
    'profession' => 'xxxxxxxxx',
    'activite' => 1,
    'cb_moto' => 1,
    'adresse_moto' => 'moto 1',
    'cp_moto' => 'xxxxxx 1',
    'ville_moto' => 'xxxxxxxxx',
    'moto_permis_1' => 'A1',
    'moto_permis_2' => 'B1',
    'moto_permis_3' => 'A1',
    'moto_marque' => 'xxxx',
    'cb_auto' => '1',
    'adresse_auto' => 'auto 2',
    'cp_auto' => 'xxxxxxxx 2',
    'ville_auto' => 'xxxxxxx',
    'auto_permis_1' => 'A 2',
    'auto_permis_2' => 'B 2',
    'auto_permis_3' => 'A2',
    'auto_marque' => 'xxxx'
);

$result = array();
foreach ($data as $key => $value)
{
    if (strpos($key, 'moto') !== FALSE)
    {
        $result['moto'][$key] = $value;
    }
    elseif(strpos($key, 'auto') !== FALSE)
    {
        $result['auto'][$key] = $value;
    }
    else
    {
        $result['general'][$key] = $value;
    }
}

print_r($result);

Live DEMO

示例输出:

Array
(
    [general] => Array
        (
            [civilite] => 3
            [first_name] => first_name
            [last_name] => last_name
            [telephone] => xxxxxxxxxxx
            [fax] => xxxxxxxxx
            [telephone_portable] => xxxxxxxxxxxx
            [mail] => example@example.com
            [naissance] => xx/xx/xxx
            [profession] => xxxxxxxxx
            [activite] => 1
        )

    [moto] => Array
        (
            [cb_moto] => 1
            [adresse_moto] => moto 1
            [cp_moto] => xxxxxx 1
            [ville_moto] => xxxxxxxxx
            [moto_permis_1] => A1
            [moto_permis_2] => B1
            [moto_permis_3] => A1
            [moto_marque] => xxxx
        )

    [auto] => Array
        (
            [cb_auto] => 1
            [adresse_auto] => auto 2
            [cp_auto] => xxxxxxxx 2
            [ville_auto] => xxxxxxx
            [auto_permis_1] => A 2
            [auto_permis_2] => B 2
            [auto_permis_3] => A2
            [auto_marque] => xxxx
        )

)

答案 1 :(得分:0)

也许这会对你有所帮助:

foreach (new RecursiveIteratorIterator (new RecursiveArrayIterator ($content), RecursiveIteratorIterator::SELF_FIRST) as $key => $value)
{
    if (strpos ($key, 'moto') === 0)
    {
        $content ['moto'][] = $value;
    } else {
        $content ['general'][] = $value;
    }
}

答案 2 :(得分:0)

我有代码

$content = array(   'civilite'              => '3',
                    'first_name'            => 'first_name',
                    'last_name'             => 'last_name',
                    'telephone'             => 'xxxxxxxxxxx',
                    'fax'                   => 'xxxxxxxxx',
                    'telephone_portable'    => 'xxxxxxxxxxxx',
                    'mail'                  => 'example@example.com',
                    'naissance'             => 'xx/xx/xxx',
                    'profession'            => 'xxxxxxxxx',
                    'activite'              => '1',
                    'cb_moto'               => '1',
                    'adresse_moto'          => 'moto 1',
                    'cp_moto'               => 'xxxxxx 1',
                    'ville_moto'            => 'xxxxxxxxx',
                    'moto_permis_1'         => 'A1',
                    'moto_permis_2'         => 'B1',
                    'moto_permis_3'         => 'A1',
                    'moto_marque'           => 'xxxx',
                    'cb_auto'               => '1',
                    'adresse_auto'          => 'auto 2',
                    'cp_auto'               => 'xxxxxxxx 2',
                    'ville_auto'            => 'xxxxxxx',
                    'auto_permis_1'         => 'A 2',
                    'auto_permis_2'         => 'B 2',
                    'auto_permis_3'         => 'A2',
                    'auto_marque'           => 'xxxx'
                );

$moto = array();
$auto = array();
$gene = array();

foreach( $content as $k => $v ){

    if( preg_match('/auto/', $k) ){

        $auto[$k] = $v;
    }
    elseif( preg_match('/moto/', $k) ){

        $moto[$k] = $v;
    }
    else{

        $gene[$k] = $v;
    }
}

$content = array();

if( !empty($gene) ){
    $content['general'] = $gene;
}
if( !empty($auto) && isset($auto['cb_auto']) ){
    $content['auto'] = $auto;
}
if( !empty($moto) && isset($moto['cb_moto']) ){
    $content['moto'] = $moto;
}

var_dump($content);