Combinación de matriz PHP donde el segundo valor coincide con la condición manteniendo el orden

First Array

[0] => Array
    (
        [name] => Car
        [selected] => 1
    )

[1] => Array
    (
        [name] => Boat
        [selected] => 1
    )

[2] => Array
    (
        [name] => Bike

    )

[3] => Array
    (
        [name] => Tank
    )

[4] => Array
    (
        [name] => Aircraft
    )

Segunda matriz:

 [0] => Array
    (
        [name] => Boat
        [code] => PLA


    )

[1] => Array
    (
        [name] => Car
        [code] => GTO


    )

So what I need is to create new array containing all the values of the first array but the 'selected' entries need to be in the order of the second array.

Desired Array:

[0] => Array
        (
            [name] => Boat
            [selected] => 1
        )

    [1] => Array
        (
            [name] => Car
            [selected] => 1
        )

    [2] => Array
        (
            [name] => Boat

        )

    [3] => Array
        (
            [name] => Tank
        )

    [4] => Array
        (
            [name] => Aircraft
        )

preguntado el 16 de mayo de 11 a las 16:05

3 Respuestas

$desiredArray = array();
$checkArray = array();
$arrayToAdd =array();
foreach ($secondArray as $item)
{
    foreach($firstArray as $item2)
    {
        if($item2['name'] == $item['name'] && $item2['selected'])
        {
            $n = count($desiredArray);
            $desiredArray[$n]['name'] = $item['name'];
            $desiredArray[$n]['selected'] = 1;
            $checkArray[] = $item['name'];
        }               
    }
}
foreach($firstArray as $item)
{
    if(!in_array($item['name'], $checkArray))
    {
        $desiredArray[]['name'] = $item['name'];
    }
}

Try this, I can't say i'm proud of it but it might just do what you need. This code assumes that both the First and Second arrays have corresponding name fields. I haven't tested this extensively but please feel free to use it if you need it.

contestado el 17 de mayo de 11 a las 20:05

thanks , I tweaked the above answer to work , but this looks better. - BobFlemming

foreach($second as $k => $v) if(isset($v['selected')) $selected[$v['name']] = $v;
foreach($first as $k => $v){
   $Desired[]['name'] = $v['name'];
   if(isset($selected[$v['name']])) $Desired[]['selected'] = 1;
}

contestado el 17 de mayo de 11 a las 21:05

this created a new value for the [selected] , so [0] [name] 1 [selected] but helped me on my way . Thanks! - BobFlemming

So let me just get this straight so i can actually try to answer your question :

Your goal is to make sure the selected items in your " first " array get sorted the same way as your " second " array. So you'd want the same array values, in a different order, and that order is the order specified in the " second " array?

contestado el 16 de mayo de 11 a las 20:05

This should have been a comment as it's not an answer - R. Barzell

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.