【问题标题】:PHP - Search through PHP array through input field?PHP - 通过输入字段搜索 PHP 数组?
【发布时间】:2018-03-24 08:46:24
【问题描述】:
<?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 声明有关?

非常感谢

【问题讨论】:

    标签: php arrays post arraylist get


    【解决方案1】:

    $allBookings = [ ... ]之后

    尝试添加类似的内容:

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

    【讨论】:

      【解决方案2】:

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

      试试:

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

      打印内容:

      print_r( $allBookings[$_POST['bookingReference']] );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-24
        • 2011-09-16
        • 1970-01-01
        • 2016-02-28
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        • 2019-11-16
        相关资源
        最近更新 更多