2

I have a list of cities, these cities may exists once or repeated several times, or may not exists at all.

With php I want to check if city is in the foreach, and if exists print only one input out.

Below works perfect, print city only if exist, but repeats the existing:

  **Updated (added afterwards):**
$paradasarray= array(); 
$xml = new SimpleXMLElement($viajes); 
foreach ($xml->parada as $excursion) { 
  $paradasObject = new stdClass(); 
  $paradasObject->localidad = $excursion->localidad; 
  $paradasObject->localidad = str_replace('/<![CDATA[(.*)]]>/', '', 
  $paradasObject->localidad); $paradasarray[] = $paradasObject; 
} 
$paradasarray = json_encode($paradasarray); $paradasarray = 
json_decode($paradasarray); 

     **end updated**


foreach ($paradasarray as $parada) {
    if (strpos($parada->localidad, 'Benalmádena') !== false) {
        echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';
    }
    if (strpos($parada->localidad, 'Estepona') !== false) {
        echo '<option value="Estepona">Estepona</option>';
    }
}

I have tried with break, however or I only get one of the two cities when I should get both, or none of them.

Helenp
  • 143
  • 2
  • 15

3 Answers3

2

You need to remove duplicate value from your array so get localidad column of array using array_column() and remove duplicate values from it using array_unique()

$newParadasarray = array_unique(array_column($paradasarray, "localidad"));

So your code should changed to

$newParadasarray = array_unique(array_column($paradasarray, "localidad"));
foreach ($newParadasarray as $parada) {
    if (strpos($parada, 'Benalmádena') !== false)
        echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';

    if (strpos($parada, 'Estepona') !== false)
        echo '<option value="Estepona">Estepona</option>';
}

Check result in demo


Update:

I see your full code so you can create an array contain only localidad values. and easily remove duplicate values from it using array_unique. Only add bottom code to your first loop (xml loop).

@$newParadasarray[] = $excursion->localidad;

And remove duplicate value after loop like this

$newParadasarray = array_unique($newParadasarray);

And loop through array to printing options

foreach ($newParadasarray as $parada) {
    if (strpos($parada, 'Benalmádena') !== false)
        echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';

    if (strpos($parada, 'Estepona') !== false)
        echo '<option value="Estepona">Estepona</option>';
}
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • Don't work, maybe I should have posted full code. Its from an xml. Before the foreach: $paradasarray= array(); $xml = new SimpleXMLElement($viajes); foreach ($xml->parada as $excursion) { $paradasObject = new stdClass(); $paradasObject->localidad = $excursion->localidad; $paradasObject->localidad = str_replace('/<!\[CDATA\[(.*)\]\]>/', '', $paradasObject->localidad); $paradasarray[] = $paradasObject; } $paradasarray = json_encode($paradasarray); $paradasarray = json_decode($paradasarray); – Helenp Oct 29 '18 at 10:26
  • @Helenp Please write all question details in the question itself. This way the full story is all in the one predictable location for everyone's convenience. – mickmackusa Oct 29 '18 at 10:36
  • Thanks, This works just perfect, however reorder the inputs. Now I need to order them alfabethically – Helenp Oct 29 '18 at 11:05
0

Simply,

$cities = [];

foreach ($paradasarray as $parada) {
    // If the current city is not in our array i.e. if it hasn't been printed
    if (!in_array($parada->localidad, $cities)) {
         // print then push
         array_push($parada->localidad, $cities);
    }
}

This way you are keeping track of all the cities you have already printed.

However, I would actually amend this at a query level (if a query is involved) and filter them from there.

Reading Material

array_push

Script47
  • 14,230
  • 4
  • 45
  • 66
0

You can write a break condition when 2 different matches are stored. The beauty in declaring the $result array keys, is that if you encounter a duplicate qualifying value, it will overwrite the "group" element instead of generating a new element.

Code: (Demo)

$paradasarray = [
    (object)['localidad' => 'Estepona whatever'],
    (object)['localidad' => 'something Benalmádena'],
    (object)['localidad' => 'Benalmádena foo'],
    (object)['localidad' => 'Estepona bar']
];

$result = [];
foreach ($paradasarray as $i => $parada) {
    if (strpos($parada->localidad, 'Benalmádena') !== false) {
        $result['Benalmádena'] = "Benalmádena Costa";
    } elseif (strpos($parada->localidad, 'Estepona') !== false) {
        $result['Estepona'] = 'Estepona';
    }
    if (count($result) == 2) {
        break;
    }
}

// you don't need to declare a value attribute if it matches the text
echo "<select>";
    echo "<option>" , implode("</option><option>", $result) , "</option>";
echo "</select>";

No matter how many elements are matched, you will either get:

0: an empty <option></option> tag in the select.
1: a single, filled option in the select
2: both filled options in the select

mickmackusa
  • 43,625
  • 12
  • 83
  • 136