【问题标题】:How to read array data using where condition [closed]如何使用 where 条件读取数组数据 [关闭]
【发布时间】:2014-11-04 13:58:22
【问题描述】:

我已将一个查询结果存储在一个 php 数组中

 Array
 (
    [0] => stdClass Object
        (
            [AccountNumber] => 1000000
            [FirstName] => james
            [LastName] => James Administrator
        )

[1] => stdClass Object
    (
        [AccountNumber] => 1000001
        [FirstName] => george
        [LastName] => James
    )

[2] => stdClass Object
    (
        [AccountNumber] => 1000002
        [FirstName] => Clara
        [LastName] => Pradeep 
    )

[3] => stdClass Object
    (
        [AccountNumber] => 1000003
        [FirstName] => Dona
        [LastName] => Kodi
    )

)

我需要打印特定 AccountNumber 的 FirstName 和 LastName

例外的输出是:

名字:克拉拉

姓:普拉迪普

其中 AccountNumber 是 1000002

提前致谢

【问题讨论】:

  • 请与我们分享您的尝试。
  • 您表示这是来自数据库。为什么不直接使用 db 的“where 子句”功能呢?然后你最终只得到匹配的结果。另外,它会更快,因为您从数据库传输的数据更少,并且不必提取结果,然后循环它们。

标签: php arrays codeigniter


【解决方案1】:

在数组中搜索你想要的账号,然后输出:

$object_array = {your array}; // whatever variable you have to print out what you posted
foreach ($object_array as $account) {
    if ($account->AccountNumber == "1000002") { // change the number to a variable if required
        echo "<p>FirstName: " . $account->FirstName . "</p>";
        echo "<p>:LastName: " . $account->LastName . "</p>";
    }
}

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 2011-12-14
    • 1970-01-01
    • 2016-05-25
    • 2022-07-21
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多