Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串数组转换为对象数组_Php_Arrays_Object - Fatal编程技术网

Php 将字符串数组转换为对象数组

Php 将字符串数组转换为对象数组,php,arrays,object,Php,Arrays,Object,我有一个空数组和数据。通过一个函数,我可以将数据放入数组,这就是函数: foreach($values as $key => $value){ $name = $slugify->slugify($value->getColonne()->getName()); if(!isset($array[$value->getPosition()])){ $array[$value->getPosition()] = array(); }

我有一个空数组和数据。通过一个函数,我可以将数据放入数组,这就是函数:

foreach($values as $key => $value){
  $name = $slugify->slugify($value->getColonne()->getName());

  if(!isset($array[$value->getPosition()])){
       $array[$value->getPosition()] = array();
  }

  array_push($array[$value->getPosition()],  $name . ":" . $value->getValue());
}
有了这个,我在结尾有:
[[“nom:andraud”],[“nom:andro”,“prenom:clement”]

但我希望有这样的东西:
[{nom:andraud},{nom:andro”,“prenom:clement}]


我需要一个对象数组,而不是字符串数组。

好的,因此您需要一个对象数组,而不是字符串数组

在这种情况下,您需要执行以下操作:

foreach ($values as $key => $value) {
   $name = $slugify->slugify($value->getColonne()->getName());

   if (!isset($array[$value->getPosition()])) {
       $array[$value->getPosition()] = new stdClass();
   }
   $array[$value->getPosition()]->$name = $value->getValue();
}
注意

在PHP>=5.3严格模式下中,如果需要在对象内部创建属性而不生成错误,则可以:

$foo = new StdClass();
$foo->bar = '1234';
您需要将特性创建为关联阵列中的位置,然后将阵列投射回对象。你可以这样做:

$foo = array('bar' => '1234');
$foo = (object)$foo;
因此,在这种情况下,您的代码需要如下所示:

foreach ($values as $key => $value) {
   $name = $slugify->slugify($value->getColonne()->getName());

   // cast the object as an array to create the new property,
   // or create a new array if empty
   if (isset($array[$value->getPosition()])) {
       $array[$value->getPosition()] = (array)$array[$value->getPosition()];
   } else {
       $array[$value->getPosition()] = array();
   }
   // create the new position
   $array[$value->getPosition()][$name] = $value->getValue();

   // now simply cast the array back into an object :)
   $array[$value->getPosition()] = (object)$array[$value->getPosition()];
}
你可以用

通过打字

$object = (object)$array;
或者您可以在
foreach

foreach($values as $key => $value){
    $name = $slugify->slugify($value->getColonne()->getName());

    if(!isset($array[$value->getPosition()])){
        $array[$value->getPosition()] = new stdClass();
    }

    $array[$value->getPosition()]->$name = $value->getValue();
}

是的,但不是同样的你的问题告诉我你必须先学习PHP。阅读教程,浏览,阅读你对SO问题的评论和答案。不完全是这样,如果你看到我的最后一个示例,第二个对象有两个元素,它不是数组中的两个对象,而是一个包含两个元素的对象。@ClémentAndraud,是的,我刚刚编辑过。那么,
$value->getPosition()
的作用是什么呢?它是数组中的键?但是为什么在您的示例中没有显示它呢?位置是对象在数组中的位置。在我的示例中,第一个元素的位置为0,第二个和第三个元素的位置为01@Cl埃门坦德劳德:好的,我已经重新考虑过了,现在试试。谢谢,但根本不是这个;)<代码>$array[$value->getPosition()]=new stdClass()会在每次迭代
$value->getPosition()
时创建新对象是的,我终于得到了他想要的:)@ClémentAndraud看看我的代码,看看是否有用:)