2

I have an empty array and data. With a function I can put data into my array, this is the function :

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());
}

With this, i have at the end : [["nom:andraud"], ["nom:andro", "prenom:clement"]]

But I expected to have something like : [{nom:"andraud"}, {nom:"andro", "prenom:clement"}]

I need an array of object, not an array of strings.

2
  • yeah, but not the same Commented Apr 28, 2015 at 14:03
  • Your questions tell me you have to learn PHP first. Read a tutorial, browse the documentation, read the comments and answers you get on your SO questions. Commented Apr 28, 2015 at 14:07

2 Answers 2

3

You can use stdClass

By typecasting

$object = (object)$array;

Or you can create object in 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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Not exactly, if you see my last example, the second object has two elements, it's not two objects in an array but one object with two elements.
@ClémentAndraud, yeah, I've just edited. And what for $value->getPosition()? It's key in array? But why it's not presented in your example?
Position is the position of the object in the array. In my example the first element has positon 0 and the second and third has 1
@ClémentAndraud ok, I have recfactored, Try now.
0

Ok, so instead of an array of arrays of strings, you need an array of ojects.

In this case, you need to do something like the following:

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();
}

NOTE

In PHP >= 5.3 strict mode, if you need to create a property inside an object, without generating an error, instead of:

$foo = new StdClass();
$foo->bar = '1234';

You need to create the property as a position in an associative array, and then cast the array back to an object. You'd do something like:

$foo = array('bar' => '1234');
$foo = (object)$foo;

So, in that case your code will need to like like this:

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()];
}

3 Comments

Thanks, but it's not this at all ;)
$array[$value->getPosition()] = new stdClass(); would create new object every iteration of $value->getPosition()
Yeah, I finally got what he wanted :) @ClémentAndraud take a look at my code, and see if it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.