如果数组echo键中存在post数据

I have an input with this form action:

<?php
  $product_add = $_POST['product'];
  $pa = strtolower($product_add);
  $tv= array('Sony'=>'sony tv', 'vizio' => 'vizio tv', 'panasonic' => 'panasonic tv');
  if (in_array($pa, $tv)) {
    foreach ($tv as $key => $item) {
      if ($item === $pa) {echo $item.' is begin with ('.$key.')';}
    }
?>
<div class="tv">
  <form name="addform" action="inc/ap-tv.inc.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="product_type" value="TV">
    <input type="hidden" name="user" value="<?php echo $_SESSION['user_name'] ?>">  
    <label for="brand">Brand</label> <input type="text" name="brand" value="<?php echo $key; ?>"><br>
    <input name="" type="submit" value="Add Television" />
  </form>   
</div>
<?php
}
?>

If someone enters 'Sony TV' into the initial input form I want the word "Sony" to appear as the value in the brand field.

When I echo $key in the foreach loop it works but when I try to echo it as the input value I get a number. how can I make the variable useable outside of the foreach loop? I've also tried using both 'print' and 'print_r()' but neither work. Sorry if this is simple, I'm kind of a beginner with this stuff.

Thanks!

$key only exists inside of your loop. When the loop terminates, the variables declared by it do, too.

All you need to do is use array_search() to get the key based on the value:

if (in_array($pa, $tv)) {
    $brand = array_search(strtolower($pa), $tv);
}

<label for="brand">Brand</label> 
<input type="text" name="brand" value="<?php echo $brand; ?>"><br>

Demo

When you echo in the loop, you echo only when it matches. The loop continues. Either add a break; to your loop or stored the match in a variable not tied to the loop.

You've set the key for 'Sony', but the other two are given implied keys because your syntax is bad.

$tv= array('Sony'=>'sony tv', 'vizio => vizio tv', 'panasonic => panasonic tv');

It should read

$tv= array('Sony'=>'sony tv', 'vizio' => 'vizio tv', 'panasonic' => 'panasonic tv');

You also have a syntax error: The first if statement is not closed (missing })

Also see Jeremy's answer.