在PHP中访问stdClass中第一个属性的属性名

在PHP中访问stdClass中第一个属性的属性名,php,php-5.6,Php,Php 5.6,在对json字符串进行json_解码后,我有一个由许多stdclass组成的数组 看起来是这样的: product: array(3) 0: stdClass PropertyAbc: "Product 1|Product 5" 1: stdClass PropertyXyz: "Product 2|Product 9|Product 10" 2: stdClass PropertyEfg: "Product 3|Product 12" 我需要将其转换为以下格式的所有值的管道

在对json字符串进行json_解码后,我有一个由许多stdclass组成的数组

看起来是这样的:

product: array(3)
0: stdClass
   PropertyAbc: "Product 1|Product 5"
1: stdClass
   PropertyXyz: "Product 2|Product 9|Product 10"
2: stdClass
   PropertyEfg: "Product 3|Product 12"
我需要将其转换为以下格式的所有值的管道分隔字符串:PropertyName>Value作为最终结果:

PropertyAbc>产品1 | PropertyAbc>产品5 | PropertyXyz>产品 2 | PropertyXyz>产品9 | PropertyXyz>产品10 | propertyfg>产品 3 | PropertyEfg>产品12

下面是我尝试执行此操作的方式,但在循环通过stdClass时,无法确定如何获取第一个属性的值和名称(注意:每个stdClass始终只有一个属性):


可以使用反射来获取对象的属性(请参见):

结果:

foo
bar
array(2) {
  [0]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "foo"
    ["class"]=>
    string(3) "Foo"
  }
  [1]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "Foo"
  }
}
其余的拆分和连接应该是直接进行的

更新: 还有一些澄清。获得属性名称后,可以使用动态访问器获取属性值:

$class = <stdClassObject>;
$reflectionClass = new ReflectionClass($class);
$properties = $reflectionClass->getProperties();

foreach($properties as $p){
  $value = $class->$p;

  // do some concatination here
}
$class=;
$reflectionClass=新的reflectionClass($class);
$properties=$reflectionClass->getProperties();
foreach($p){
$value=$class->$p;
//在这里做一些浓缩
}

只需通过

$products=json_decode(json_encode($json), true);
然后你可以像操作一个简单的数组一样操作它

实施:

<?php
$p1 = new StdClass();
$p1->PropertyAbc = "Product 1|Product 5";

$p2 = new StdClass();
$p2->PropertyXyz = "Product 2|Product 9|Product 10";

$p3 = new StdClass();
$p3->PropertyEfg = "Product 3|Product 12";

$products_orig = [ $p1, $p2, $p3 ];
$products=json_decode(json_encode($products_orig), true);

?>
<pre><?= print_r($products, true) ?></pre>
<?php
$s='';
foreach($products as $a){
    foreach($a as $key=>$b){
        $c=explode('|', $b);
        foreach($c as $d){
            $s.=(($s==='')?'':'|').$key.'>'.$d;
        }
    }
}
echo $s;

?>

对于将对象属性作为数组获取并从中进行操作非常有用


字符串(155)“PropertyAbc>产品1 | PropertyAbc>产品5 | PropertyXyz>产品2 | PropertyXyz>产品9 | PropertyXyz>产品10 | PropertyEfg>产品3 | PropertyEfg>产品12”

您还可以使
获取对象变量
接近一行:


当使用
foreach($arr as$key=>$value)
在对象上迭代时,
$key
将保留属性名。仅当类是json_可序列化的,没有边时才起作用effects@Kancholliev我照你说的做了,现在我有了一个包含3个数组的数组,而不是3个stdclass。这对我有什么帮助?只是好奇它的优势是什么。如何获得属性名称?@Kancholliev我想说的是,我现在有了类似于数组(1)PropertyXyz的东西:“产品1 |产品2”。如何在代码中获取它的PropertyXyz部分?Key最后是0,value最后是整个字符串:PropertyXyz:“Product 1 | Product 2b顺便说一句,我想你的问题是因为你犯了错误,错过了json的第二个参数(true)谢谢你超越并给我一个解决方案。它工作得很好。
$products=json_decode(json_encode($json), true);
<?php
$p1 = new StdClass();
$p1->PropertyAbc = "Product 1|Product 5";

$p2 = new StdClass();
$p2->PropertyXyz = "Product 2|Product 9|Product 10";

$p3 = new StdClass();
$p3->PropertyEfg = "Product 3|Product 12";

$products_orig = [ $p1, $p2, $p3 ];
$products=json_decode(json_encode($products_orig), true);

?>
<pre><?= print_r($products, true) ?></pre>
<?php
$s='';
foreach($products as $a){
    foreach($a as $key=>$b){
        $c=explode('|', $b);
        foreach($c as $d){
            $s.=(($s==='')?'':'|').$key.'>'.$d;
        }
    }
}
echo $s;

?>
 $p1 = new StdClass();
 $p1->PropertyAbc = "Product 1|Product 5";

 $p2 = new StdClass();
 $p2->PropertyXyz = "Product 2|Product 9|Product 10";

 $p3 = new StdClass();
 $p3->PropertyEfg = "Product 3|Product 12";

 $products = [ $p1, $p2, $p3 ];
 foreach ($products as $product) {
    $productArray = get_object_vars($product);
    $productPropName = array_keys($productArray)[0];
    $productPropsValues = explode('|', array_values($productArray)[0]);
    foreach ($productPropsValues as $productPropsValue) {
        $result[] = $productPropName . '>' . $productPropsValue;
    }
}

var_dump(implode('|', $result));
$obj->{array_keys(get_object_vars($obj))[0]};