1

How can I use Array1 and Array2 to get the Wanted Result?

Array1

array(
    (int) 0 => '37',
    (int) 1 => '38'
)

Array2

array(
    (int) 0 => array(
        'ParentKey' => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 1 => array(
        'ParentKey' => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
)

Wanted Result

array(
    (int) 0 => array(
        'ParentKey' => array(
            'Array1Key' => 37,
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 1 => array(
        'ParentKey' => array(
            'Array1Key' => 37,
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
    (int) 2 => array(
        'ParentKey' => array(
            'Array1Key' => 38,
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    ),
    (int) 3 => array(
        'ParentKey' => array(
            'Array1Key' => 38,
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
)

I have tried several things but this got me the closest...

PHP

$data = array();
foreach($Array1 as $id)
{
    foreach($Array2 as $Array1Result)
    {
        $data[]['ParentKey'] = array(
            'Array1Key' => $id,
            $Array1Result['ParentKey']
        );
    }
}

Result

array(
  (int) 0 => array(
    'ParentKey' => array(
        'Array1Key' => '37',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    )
  ),
  (int) 1 => array(
    'ParentKey' => array(
        'Array1Key' => '37',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
  ),
  (int) 2 => array(
    'ParentKey' => array(
        'Array1Key' => '38',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '2'
        )
    )
  ),
  (int) 3 => array(
    'ParentKey' => array(
        'Array1Key' => '38',
        (int) 0 => array(
            'ChildKey1' => '1',
            'ChildKey2' => '1'
        )
    )
  )
)
5
  • 2
    and the question is... Commented Nov 1, 2016 at 18:47
  • Replace array('Array1Key' => $id, $Array1Result['ParentKey']) with array_merge(array('Array1Key' => $id), $Array1Result['ParentKey']) Commented Nov 1, 2016 at 18:52
  • @CharlotteDunois That pretty much did it. Thanks! I just had to take out the array() after $data[]['ParentKey'] = Commented Nov 1, 2016 at 18:57
  • 2
    @CharlotteDunois: Why comment and not answer? Commented Nov 1, 2016 at 18:58
  • @AbraCadaver Sorry, haven't gotten to it until now :) Commented Nov 1, 2016 at 19:29

1 Answer 1

2

What you get is exactly what you do. You put an array inside an array. But you need to merge two arrays together. The first one being the Array1Key and the second one the old array. You can achieve this with array_merge.

$data = array();
foreach($Array1 as $id)
{
    foreach($Array2 as $Array1Result)
    {
        $data[]['ParentKey'] = array_merge(
            array('Array1Key' => $id),
            $Array1Result['ParentKey']
        );
    }
}
Sign up to request clarification or add additional context in comments.

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.