2

I'm a newbie in PHP so please guide me to better practice. Basically, I have this array $content, and I would like to change it according to some keys existence. Here's my array :

Array
(
    [civilite] => 3
    [first_name] => first_name
    [last_name] => last_name
    [telephone] => xxxxxxxxxxx
    [fax] => xxxxxxxxx
    [telephone_portable] => xxxxxxxxxxxx
    [mail] => [email protected]
    [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
);

And here's what I want to change it to :

Array
(
    [general] => Array
                ( 
                [civilite] => 3
                [first_name] => first_name
                [last_name] => last_name
                [telephone] => xxxxxxxxxxx
                [fax] => xxxxxxxxx
                [telephone_portable] => xxxxxxxxxxxx
                [mail] => [email protected]
                [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
                )
  );

The idea is if the key cb_moto exists create a sub-array that has the key moto in it until the appearance of the key cb_auto and create another sub-array that has the key auto and so on.
Here's what I've tried so far :

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

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

Any help with this? Much appreciated.

3 Answers 3

1

If the key is equal the key name you're looking for, cb_moto and cb_auto we set a variable to true or false to indicate that and save its data inside its sub-array.

Not the prettier way to achieve it, but easy to follow and readable:

<?php
$data = array (
    'civilite' => 3,
    'first_name' => 'first_name',
    'last_name' => 'last_name',
    'telephone' => 'xxxxxxxxxxx',
    'fax' => 'xxxxxxxxx',
    'telephone_portable' => 'xxxxxxxxxxxx',
    'mail' => '[email protected]',
    '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

Using strpos instead of variable checks:

<?php
$data = array (
    'civilite' => 3,
    'first_name' => 'first_name',
    'last_name' => 'last_name',
    'telephone' => 'xxxxxxxxxxx',
    'fax' => 'xxxxxxxxx',
    'telephone_portable' => 'xxxxxxxxxxxx',
    'mail' => '[email protected]',
    '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

Sample output:

Array
(
    [general] => Array
        (
            [civilite] => 3
            [first_name] => first_name
            [last_name] => last_name
            [telephone] => xxxxxxxxxxx
            [fax] => xxxxxxxxx
            [telephone_portable] => xxxxxxxxxxxx
            [mail] => [email protected]
            [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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

@user3851013 see the second example its a little bit more simplified.
0

Maybe this will help you:

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

1 Comment

The strpos was a nice idea however your sample is wrong needs to be !== FALSE and that would only work if all the data in between the area he wants to gather always have moto as suffix or prefix like on the sample.
0

There is the code i made

$content = array(   'civilite'              => '3',
                    'first_name'            => 'first_name',
                    'last_name'             => 'last_name',
                    'telephone'             => 'xxxxxxxxxxx',
                    'fax'                   => 'xxxxxxxxx',
                    'telephone_portable'    => 'xxxxxxxxxxxx',
                    'mail'                  => '[email protected]',
                    '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);

2 Comments

Why use regex? Why. why. whyyyyyyyyy
ahaha :p i didn't know for str functions ^^ now i know, THX <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.