0

I have an complicated array that looks like this:

$input=array(
    (int) 0 => array(
        'XXX' => array(
            'id' => '7',
            'p_id' => '1',
            'address' => '9463',
            'arrival_time' => '2014-05-01 03:30:00'
        ),
        'YYY' => array(
            'id' => '1',
            'iden' => '1111',
            'name' => 'Tom'
        )
    ),
    (int) 1 => array(
        'XXX' => array(
            'id' => '9',
            'p_id' => '2',
            'address' => '9469',
            'arrival_time' => '2014-05-27 16:43:58'
        ),
        'YYY' => array(
            'id' => '2',
            'iden' => '2222',
            'name' => 'Sam'
        )
    ),
    (int) 2 => array(
        'XXX' => array(
            'id' => '3',
            'p_id' => '3',
            'address' => '9462',
            'arrival_time' => '2014-04-21 14:05:00'
        ),
        'YYY' => array(
            'id' => '3',
            'iden' => '3333',
            'name' => 'James'
        )
    )
)

I would like to convert it such that it looks like this;

$output=array(
    (int) 0 => array(
        'name' => 'Tom',
        'iden' => '1111',
        'address' => '9463'
    ),
    (int) 1 => array(
        'name' => 'Sam',
        'iden' => '2222',
        'address' => '9469'
    ),
    (int) 2 => array(
        'name' => 'James',
        'iden' => '3333',
        'address' => '9462'
    )

I wrote some code to solve this problem:

foreach ( $input as $key => $value)
{
    $output['name']=$input[$key]['YYY']['name'];
    $output['iden']=$input[$key]['YYY']['iden'];
    $output['address']=$input[$key]['XXX']['address'];
}

Unfortunately, it retrieves only the last element of the input array.

Can someone more experienced help?

Thank you very much.

1
  • To define an array it is not necessary to use (int) 0 => Commented May 27, 2014 at 10:10

3 Answers 3

3

You are overwriting the values in each iteration, as you always write to $output['name'] etc.

foreach ( $input as $key => $value)
{
    $output[$key] = array(
        'name' => $value['YYY']['name'],
        'iden' => $value['YYY']['iden'],
        'address' => $value['XXX']['address']
    );
}

The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.

Also $input[$key] and $value are equivalent, so I used the shorter variant ;)

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

Comments

1

Try this in your foreach loop :-

foreach ( $input as $key=>$value)
{
    $output[$key]['name']=$value['YYY']['name'];
    $output[$key]['iden']=$value['YYY']['iden'];
    $output[$key]['address']=$value['XXX']['address'];
}

Comments

0

You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

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.