php 循环 OBJECT 并获得键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:22:20  来源:igfitidea点击:

Loop OBJECT and obtain both key and value

phparraysobjectloops

提问by Richard M

Ok, here we go. I hope I explain this correctly.

好的,我们开始。我希望我能正确解释这一点。

I have an object that i'd like to loop through to obtain both the key and value. Here is an example of the object I'm receiving. Thanks in advance for any help or ideas.

我有一个对象,我想遍历它以获取键和值。这是我收到的对象的示例。在此先感谢您的任何帮助或想法。

    Array
(
    [0] => stdClass Object
        (
            [id] => 93
            [RecordGUID] => 
            [txtEmplid] => 0134754
            [txtFname] => 
            [txtLname] => 
            [txtMname] => 
            [txtEmail] => 
            [txtSecEmail] => 
            [txtPhoneNo] => 4046565454
            [drpMajor] => 
            [drpStatus] => 
            [regmain] => 
            [chkDental] => 0
            [chkDO] => 
            [chkMD] => 
            [chkMDPHD] => 
            [chkNursin] => 
            [chkOPT] => 
            [chkPA] => 
            [chkPH] => 
            [chkPharm] => 
            [chkPOD] => 
            [chkPostBac] => 
            [chkVet] => 
        )

)

I basically need to loop through the above info getting both the key and value. For example:

我基本上需要遍历上述信息以获取键和值。例如:

id=93
RecordGUID=
txtEmplid=0134754

and so on.

等等。

Again, thanks in advance for any answers.

再次感谢您提供任何答案。

UPDATE for DBF Here is what I get when I use your code snippt:

DBF 的更新这是我使用您的代码片段时得到的信息:

int(0)
object(stdClass)#27 (24) {
  ["id"]=>
  string(2) "93"
  ["RecordGUID"]=>
  NULL
  ["txtEmplid"]=>
  string(7) "0134754"
  ["txtFname"]=>
  string(0) ""
  ["txtLname"]=>
  string(0) ""
  ["txtMname"]=>
  string(0) ""
  ["txtEmail"]=>
  string(0) ""
  ["txtSecEmail"]=>
  string(0) ""
  ["txtPhoneNo"]=>
  string(10) "4045506561"
  ["drpMajor"]=>
  NULL
  ["drpStatus"]=>
  NULL
  ["regmain"]=>
  NULL
  ["chkDental"]=>
  string(1) "0"
  ["chkDO"]=>
  NULL
  ["chkMD"]=>
  NULL
  ["chkMDPHD"]=>
  NULL
  ["chkNursin"]=>
  NULL
  ["chkOPT"]=>
  NULL
  ["chkPA"]=>
  NULL
  ["chkPH"]=>
  NULL
  ["chkPharm"]=>
  NULL
  ["chkPOD"]=>
  NULL
  ["chkPostBac"]=>
  NULL
  ["chkVet"]=>
  NULL
}

回答by dbf

use get_object_vars ( object $object )

get_object_vars ( object $object )

$vars = get_object_vars ( $object );
foreach($vars as $key=>$value) {
  var_dump($key);
  var_dump($value);
}

or just iterate the object itself

或者只是迭代对象本身

foreach($object as $key=>$value) {
  var_dump($key);
  var_dump($value);
}

-- edit 2

-- 编辑 2

Here you'll have the keys and values in one line

在这里,您将拥有一行中的键和值

$string = "";
foreach($regs as $object) {
  foreach($object as $key=>$value) {
    $string += "{$key}={$value} ";
  }
}
echo $string;

if this is not what you need, I'm clueless ..

如果这不是你需要的,我一无所知..

回答by P M

You can loop through object properties with foreach

您可以循环遍历对象属性 foreach

foreach($array as $key => $object)
    foreach($object as $property => $value)
        echo "{$property} : $value" . PHP_EOL;