在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>产品1 | PropertyAbc>产品5 | PropertyXyz>产品 2 | PropertyXyz>产品9 | PropertyXyz>产品10 | PropertyEfg>产品 3 | PropertyEfg>产品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;
}
}
答案 0 :(得分:2)
您可以使用反射来获取对象的属性(请参阅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
}
答案 1 :(得分:2)
只需将其转换为数组
即可$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;
?>
答案 2 :(得分:1)
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));
string(155)&#34; PropertyAbc&gt; Product 1 | PropertyAbc&gt; Product 5 | PropertyXyz&gt; Product 2 | PropertyXyz&gt; Product 9 | PropertyXyz&gt; Product 10 | PropertyEfg&gt; Product 3 | PropertyEfg&gt; Product 12&#34;
答案 3 :(得分:0)
您还可以使get_object_vars
方法变得单一:
$obj->{array_keys(get_object_vars($obj))[0]};