Answers for "php key_exists"

PHP
7

php key exists

// Here's our fruity array
$fruits = ['apple', 'pear', 'banana'];

// Use it in an `if` statement
if (array_key_exists("banana", $fruits)) {
 // Do stuff because `banana` exists
}

// Store it for later use
$exists = array_key_exists("peach", $fruits);
Posted by: Guest on May-14-2020
1

array key exists in php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php
    if (array_key_exists('btn1', $_POST)) {
        button1();
    }
    if (array_key_exists('btn2', $_POST)) {
        button2();
    }
    function button1()
    {
        echo 'btn1 is clicked boom!';
    }
    function button2()
    {
        echo 'btn2 is clicked wohoo!';
    }
    ?>
    <br><br>

    <form action="" method="post">
        <button name="btn1">click here</button>
        <button name="btn2">click here</button>
    </form>
</body>
</html>
Posted by: Guest on September-10-2021
0

keys of array exist in array

function findKey($array, $keySearch)
{
    foreach ($array as $key => $item) {
        if ($key == $keySearch) {
            echo 'yes, it exists';
            return true;
        } elseif (is_array($item) && findKey($item, $keySearch)) {
            return true;
        }
    }
    return false;
}
Posted by: Guest on October-02-2020
0

php key_exists

PHP function key_exists($key, array $array) bool
------------------------------------------------
Checks if the given key or index exists in the array. The name of this function is array_key_exists() in PHP > 4.0.6.

Parameters:
int|string--$key--Value to check.
array--$array--An array with keys to check.
  
Returns:true on success or false on failure.
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language