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

Accessing the property name of the first property in an stdClass in PHP

在 json 字符串上执行 json_decode 后,我有一个包含许多 stdClasses 的数组。

看起来像这样:

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>Product 1|PropertyAbc>Product 5|PropertyXyz>Product 2|PropertyXyz>Product 9|PropertyXyz>Product 10|PropertyEfg>Product 3|PropertyEfg>Product 12

这是我尝试这样做的方法,但无法弄清楚如何在遍历 stdClasses 时获取第一个 属性 的值和名称(注意:总是只有一个 属性 对于每个 stdClass):

foreach ($json->products as $product) {
    // Put all products in an array
    $arr = explode('|', $NEED-VALUE-OF-FIRST-PROP);

    // Loop through array and combine values
    foreach ($arr as $key => $value) {
        $arr[$key] = $NEED-NAME-OF-FIRST-PROP . ">" . $value;
    }
}

您可以使用反射来获取对象的属性(参见http://php.net/manual/de/reflectionclass.getproperties.php):

class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
    print $prop->getName() . "\n";
}

var_dump($props);

结果:

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
}

只需将其转换为数组

$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;

?>

get_object_vars 对于将对象属性作为数组获取并从那里开始工作很有用。

 $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));

字符串(155) "PropertyAbc>Product 1|PropertyAbc>Product 5|PropertyXyz>Product 2|PropertyXyz>Product 9|PropertyXyz>Product 10|PropertyEfg>Product 3|PropertyEfg>Product 12"

您也可以将 get_object_vars 方法设为单行:

$obj->{array_keys(get_object_vars($obj))[0]};