Go through an array and separate the names to make the appropriate changes

-1

I need to loop through an array that has the following elements:

$MeuArray = array("PEDRO" ,"PEDRO" ,"PEDRO" ,"PEDRO" ,"JOAO" ,"JOAO", "JOAO", "JOAO");

I need to separate by the same names, for example: I want to get the names that are equal to "PEDRO' to make the modifications, then continue with the rest of the names.

How can I do this?

    
asked by anonymous 19.04.2016 / 15:43

1 answer

0

Well, if I understand correctly, I believe that's it, you can use a combination of for and if to know when to make the changes. Using your example:

$MeuArray = array("PEDRO" ,"PEDRO" ,"PEDRO" ,"PEDRO" ,"JOAO" ,"JOAO", "JOAO", "JOAO");

for ($i=0; $i <count($MeuArray) ; $i++) { 

    if($MeuArray[$i] == 'PEDRO')
    {
        // Faça aqui as devidas alterações para PEDRO
        // Podes criar um novvo array se desejar
        // Para utilizar o array atual $MeuArray[$i]['PEDRO'];
            echo "Teste";
        }
    }

And to get all the names, you would have to have an array with all the names, like the example you posted in $MeuArray .

Having this array, you can use array_unique . For example:

$MeuArray = array("PEDRO" ,"PEDRO" ,"PEDRO" ,"PEDRO" ,"JOAO" ,"JOAO", "JOAO", "JOAO");
$result = array_unique($MeuArray);

The example will print:

Array
(
    [0] => PEDRO
    [1] => JOAO

)

Anything but talk.

Att;

    
19.04.2016 / 16:01