0

I have one flat array that I use to generate a tree later on:

[0] => array
    [0] => 1
    [1] => 3
[1] => array
    [0] => 3
    [1] => 5
[2] => array
    [0] => 8
    [1] => 12
[3] => array
    [0] => 4
    [1] => 7

The values in this array are plain id's, I want to convert them into full names. I can get those names from the database, with corresponding id's, so that's my sample output array:

[0] => array
    ['id'] => 1
    ['name'] => 'sample name'
[1] => array
    ['id'] => 2
    ['name'] => 'foo'
[2] => array
    ['id'] => 3
    ['name'] => 'bar'

So now I have to iterate over the first array, and compare each value to the value from the second array...How can I do that without using foreach loop in every iteration of the external loop?

Thanks!

1
  • Means u want to compare all the element of second array with each iterated value of first array? Commented Oct 12, 2014 at 11:55

1 Answer 1

1

Rebuild the 2nd array so you can reference it in the first.

$newarr = array();
foreach ($secondval as $val )
{
    $newarr[ $val->id ] = $val->name;
}

Then use this in the first loop.

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.