0

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?

7
  • php.net/foreach would be a good place to start... Commented Feb 3, 2015 at 20:35
  • Loop through array A and just reassign to a new array, which will become B? I don't see how you can't achieve this... Commented Feb 3, 2015 at 20:35
  • 1
    It's not so much a transform you're looking for but a flatten stackoverflow.com/questions/9546181/… Commented Feb 3, 2015 at 20:35
  • Thank you. @DaveGoten - I didn't know about the term flatten. Thank you. Commented Feb 3, 2015 at 20:37
  • Foreach region in A create an array with the same properties in array B + add root properties of A? Commented Feb 3, 2015 at 20:38

3 Answers 3

0
$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']);
  } 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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

Comments

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.