Marcello Pato
Marcello Pato

Reputation: 504

Find a string inside of an array

I have this array:

0: {nome: "ELIANA CRISTINA GUILHERMITTI RODRIGUES - 49-Sócio-Administrador", eMail: null,…}
1: {nome: "SUZANA MARQUES LOBANCO - 49-Sócio-Administrador", fiador: "1",…}
2: {nome: "AMARILDO APARECIDO RODRIGUES - 49-Sócio-Administrador", eMail: null, cpf: "132.555.555-57",…}
3: {nome: "VALDEMIR FRANCISCO DA COSTA - 22-Sócio", eMail: null, cpf: "132.555.555-58",…}
4: {nome: "ANDRE LUIS LOBANCO - 49-Sócio-Administrador", eMail: null, cpf: "132.555.555-59",…}

The item number 1 has a specific word: fiador: "1". This means that only this user has an email. How can I get only this item->eMail? Only the item that has the word "fiador" has a valid email.

But, it is dynamic, so I´ll never know which one will have the word "fiador".

Upvotes: 0

Views: 59

Answers (1)

peterxz
peterxz

Reputation: 874

Iterate through the array (unless we're talking about thousands of records) and check which has the fiador property set:

foreach($arr as $record) {
    if(isset($record['fiador'])) ...
}

The point is is that you can use isset() to check if the array has a specific index.

Alternatively you could filter the array so that only the records with fiador property set remain in the array:

function hasProperty($item) {
    return isset($item['fiador']);
}
array_filter($arr, "hasProperty");

Upvotes: 2

Related Questions