froggomad
froggomad

Reputation: 1915

php array_walk find key for value

First let me start off saying I'm brand new to php and winging it. I'm open to alternate methods for achieving the result and any criticism.

I've got a form with a list of country codes. The list is formatted so the country code is the value and the country is the text.

<option value="AF">Afghanistan</option>

I'm wanting the country rather than the country code to be input into the database without having to change the html.

In the php file processing the form, I'm using htmlspecialchars to prevent XSS

$countryCode = htmlspecialchars($_POST["user_country"], ENT_QUOTES, 'UTF-8');

I put the country codes into an array as keys, and the name as values.

$countryArr = array('AF' => 'Afghanistan');

I created a variable to hold the name of the country

$country = '';

I created a function to find the value and write the key to $country

function countryName($value, $key) {
    if ($countryCode == $value) {
        $country = $key;
    } else {
        $country = 'not found';
    }
}    

I walk the array using the above function

array_walk($countryArr, 'countryName');

The output I'm expecting is the country name or 'not found' but what I'm getting is an empty string.

What I've got now works in a playground, but not live, presumably because of htmlspecialchars - but I don't know how to handle that. I'm using htmlspecialchars as a way to escape the string, bypassing common XSS attacks

Works in playground

<?php
  $countryCode = 'AF';
  $countryArr = array('AF' => 'Afghanistan');
  $country = '';
  function countryName($value, $key) {
    global $countryCode, $country;
    if ($countryCode == $key) {
      $country = $value;
    } else {
      $country = 'not found';
    }
  }
  array_walk($countryArr, 'countryName');
  echo('Country:<br>');
  echo("&nbsp;&nbsp;&nbsp;&nbsp;$country");
?>

Upvotes: 0

Views: 513

Answers (3)

Progrock
Progrock

Reputation: 7485

<?php
$languages =
[
    'fr' => 'French',
    'es' => 'Spanish'
];

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if (
        isset($_POST['lang'])
        && is_string($_POST['lang'])
        && array_key_exists($_POST['lang'], $languages)
    )
    {
        $language = $languages[$lang];
    }
    else 
    {
        throw new Exception('Invalid language code.');
    }

    echo $language;
}

?>
<form method='post'>
    <select name='lang'>
        <?php foreach($languages as $k => $v) { ?>
            <option value='<?=$k?>'><?=$v?></option>
        <?php } ?>
    </select>
    <input type='submit'>
</form>

Upvotes: 0

Jeto
Jeto

Reputation: 14927

According to comments, you're simply looking for an array value given a key:

$countryCode = 'AF';
$countryArr = array('AF' => 'Afghanistan');

$countryName = $countryArr[$countryCode] ?? null;

The ?? null part (requires PHP7) ensures $countryName will be null even if the country code doesn't exist as a key in the array.

Note that this is extremely basic PHP, I would recommend reading up on the subject of arrays.

Upvotes: 1

Sitepose
Sitepose

Reputation: 322

If you need to return key from value, You don't need to create a function for that or use array_walk. Instead this array_search will also work.

$key = array_search ('country_name', $countryArr);

Upvotes: 1

Related Questions