【问题标题】:PHP looping though array of object and selecting a fieldPHP循环遍历对象数组并选择一个字段
【发布时间】:2017-08-02 04:10:22
【问题描述】:

所以我有点卡在这里我有以下对象数组

Array
(
    [0] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => 9852c71f-e367-1ee0-ae05-ec5d1ee59661
            [client_sku] => 6215
            [client_description] => iSeamless Delivery and Hookup Add-On
            [ord_skus_guid] => 9828a5b5-8ea2-73a9-7f6e-622608f353c0
            [Current_Line_Item_Status] => 9828a5b5-8ea2-73a9-7f6e-622608f353c0
            [Product_Or_Service] => false
        )

    [1] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => 48bdc72d-2d79-01dd-8991-1ff4d1d36021
            [client_sku] => 9410101
            [client_description] => HAULAWAY AND RECYCLING
            [ord_skus_guid] => dc58110d-afc4-b2b2-f02f-85c66700778a
            [Current_Line_Item_Status] => dc58110d-afc4-b2b2-f02f-85c66700778a
            [Product_Or_Service] => false
        )

    [2] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => 09577d07-74df-6196-f050-3c08a1bcf77e
            [client_sku] => 3006
            [client_description] => Product Return
            [ord_skus_guid] => 3e9d1b73-566c-c4d3-1dfa-4bef21cdc2ad
            [Current_Line_Item_Status] => 3e9d1b73-566c-c4d3-1dfa-4bef21cdc2ad
            [Product_Or_Service] => true
        )

    [3] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => ff7a9a7f-5120-a089-ac0e-67d83c10533e
            [client_sku] => 3005
            [client_description] => Product Delivery
            [ord_skus_guid] => 8142eee9-fe83-74d8-958e-97439d6dd6ca
            [Current_Line_Item_Status] => 8142eee9-fe83-74d8-958e-97439d6dd6ca
            [Product_Or_Service] => true
        )

    [4] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => 5d64cd87-6ccd-adf7-1015-a6e21710541c
            [client_sku] => 4001225
            [client_description] => INSTALL COOK TOP
            [ord_skus_guid] => 2b60db24-8130-14c4-fb3e-5b42033d1909
            [Current_Line_Item_Status] => 2b60db24-8130-14c4-fb3e-5b42033d1909
            [Product_Or_Service] => false
        )

    [5] => stdClass Object
        (
            [ord_obj_guid] => b0c6836a-be91-e3b9-6083-685862edf6a0
            [job_type_obj_guid] => 48bdc72d-2d79-01dd-8991-1ff4d1d36021
            [client_sku] => 9410101
            [client_description] => HAULAWAY AND RECYCLING
            [ord_skus_guid] => 4e884ae1-eb59-4d4b-4d4c-211b23d56f89
            [Current_Line_Item_Status] => 4e884ae1-eb59-4d4b-4d4c-211b23d56f89
            [Product_Or_Service] => false
        )

)

我想循环它,然后如果 Product 或 Service 字段为真,我想调用一个函数,我认为我需要一个 foreach 循环,但我对如何设置 foreach 循环有点迷茫,任何帮助都会非常有用

【问题讨论】:

  • 你调用的函数是真的,是返回一些东西吗??
  • 你已经展示了你的输入数据,这是第一步。第 2 步和第 3 步显示您的编码尝试,并显示您想要的结果。
  • 谁曾为这个问题点赞——意识到这是“没有经过充分研究的问题”,不值得点赞。

标签: php arrays loops foreach


【解决方案1】:

试试看:

<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
   //$param = array();
$param = array(
    array("ord_obj_guid"=>80,"job_type_obj_guid"=>89,"client_sku"=>79,"client_description"=>80,"ord_skus_guid"=>79,"Current_Line_Item_Status"=>79,"Product_Or_Service"=>false),
    array("ord_obj_guid"=>80,"job_type_obj_guid"=>89,"client_sku"=>79,"client_description"=>80,"ord_skus_guid"=>79,"Current_Line_Item_Status"=>79,"Product_Or_Service"=>true),
    array("ord_obj_guid"=>80,"job_type_obj_guid"=>89,"client_sku"=>79,"client_description"=>80,"ord_skus_guid"=>79,"Current_Line_Item_Status"=>79,"Product_Or_Service"=>false),
  array("ord_obj_guid"=>80,"job_type_obj_guid"=>89,"client_sku"=>79,"client_description"=>80,"ord_skus_guid"=>79,"Current_Line_Item_Status"=>79,"Product_Or_Service"=>false),
);

for($i=0;$i<count($param);$i++){
  if($param[$i]['Product_Or_Service']=='true'){
      echo "Done";
    }
}

?>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2016-09-07
    相关资源
    最近更新 更多