Re-arrange a php array?

997

I'm not even sure if we called transform, but, let's say we have an array like this:

A)

Array
(
    [id] => 532b436477936
    [name] => Record #1
    [created] => 20-03-2014
    [regions] => Array
    (
        [0] => Array
        (
            [id] => 532b3fcbf2353
            [name] => Region #2
            [created_at] => 1395343322
        )
        [1] => Array
        (
            [id] => 532b3fcbf2354
            [name] => Region #3
            [created_at] => 1395343323
        )
    )
)

And we wish to transform it like this:

B)

Array
(
    [0] => Array
    (
        [id] => 532b436477936
        [name] => Record #1
        [created] => 20-03-2014                
        [regions.id] => 532b3fcbf2353
        [regions.name] => Region #2
        [regions.created_at] => 1395343322
    )
    [1] => Array
    (
        [id] => 532b436477936
        [name] => Record #1
        [created] => 20-03-2014    
        [regions.id] => 532b3fcbf2354
        [regions.name] => Region #3
        [regions.created_at] => 1395343323
    )
)

Can I have some help with the code please?

$actual = array(
            'id' => '532b436477936',
            'name' => 'Record #1',
            'created' => '20-03-2014',
            'regions' => array(
                            array('id' => '532b3fcbf2353', 'name' => 'Region #2'), 
                            array('id' => '532b3fcbf2354', 'name' => 'Region #3')
                        )

        );

var_dump($actual);

$desired = array(
            array(
                'id' => '532b436477936',
                'name' => 'Record #1',
                'created' => '20-03-2014',
                'regions.id' => '532b3fcbf2353',
                'regions.name' => 'Region #2',              
                ),
            array(
                'id' => '532b436477936',
                'name' => 'Record #1',
                'created' => '20-03-2014',
                'regions.id' => '532b3fcbf2354',
                'regions.name' => 'Region #3',  
            )           
);

var_dump($desired);

Can I have some lights on were to look at in order to transform A, into B?

862

Answer

Solution:

$desired = array();
foreach($actual['regions'] as $region)
  {
    $desired[] = array(
            'id' => '532b436477936',
            'name' => 'Record #1',
            'created' => '20-03-2014',
            'region.id' => $region['id'],
            'region.name' => $region['name']);
  } 
408

Answer

Solution:

$actual = array(
        'id' => '532b436477936',
        'name' => 'Record #1',
        'created' => '20-03-2014',
        'regions' => array(
                        array('id' => '532b3fcbf2353', 'name' => 'Region #2'), 
                        array('id' => '532b3fcbf2354', 'name' => 'Region #3')
                    )

    );


$desired = array();
foreach($actual as $actual_dummy => $a)
{
    foreach($a['regions'] as $Region_dummy => $r)
    {
        $desired[]=array("id" => $a['id'], 
                        "name" => $a['name'], 
                        "created" => $a['created'], 
                        "regions.id" => $r['id'], 
                        "regions.name" => $r['name']);
    }
}
654

Answer

Solution:

Try this:

$desired = array();
$basic = $actual;
unset($basic['regions']);
foreach($actual['regions'] as $key=>$val){
    foreach($val as $key2=>$val2) {
        $basic["$key.$key2"] = $val2;
    }
    $desired[] = $basic;
}

People are also looking for solutions to the problem: php - Amazon EC2 Connects to RDS from command line but not from web server

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.