PHP - 通过输入字段搜索PHP数组?

时间:2018-03-24 08:46:25

标签: php arrays post arraylist get

<?php
$allBookings = [
    "IMD002" => [
        "client" => "James Holden",
        "paid"   => "yes",
        "email"  => "james.holden@gmail.com"
    ],
    "IMD003" => [
        "client" => "Harold P. Redman",
        "paid"   => "yes",
        "email"  => "HaroldPRedman@dayrep.com"
    ],
    "IMD004" => [
        "client" => "Marcus C. Nelson",
        "paid"   => "no",
        "email"  => "MarcusCNelson@armyspy.com"
    ]
];


// DONT EDIT ANYTHING BELOW HERE
if(!empty($_POST)) {
    $clientName = $booking['client'];
}
else {
    $clientName = null;
};
?>

<!-- DONT EDIT ANYTHING BELOW HERE -->
<div class="myApp">
    <form method="post">
        <input type="text" name="bookingReference" placeholder="Booking Reference">
        <input type="submit" value="Find Client">
    </form>
    
    <h2 id="result_name">Client name: <em><?php echo $clientName; ?></em></h2>
    
</div>

当我输入输入字段(预订参考),例如“IMD002”时,如何显示凭据?因为它应该显示在“IMD002”下的php中列出的数组。我假设它与$_POST语句有关?

非常感谢

2 个答案:

答案 0 :(得分:1)

$allBookings = [ ... ]之后

尝试添加以下内容:

if(isset($_POST['bookingReference']) &&
   isset($allBookings[$_POST['bookingReference']])) {
    $booking = $allBookings[$_POST['bookingReference']];
}
else {
    $booking = ['client' => 'Invalid Ref/Not found'];
};

答案 1 :(得分:0)

要搜索,请使用 $ _ POST ['bookingReference'] 作为 $ allBookings 的索引,数据数组;

<强>尝试:

$allBookings[$_POST['bookingReference']];

打印内容:

print_r( $allBookings[$_POST['bookingReference']] );
相关问题