Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to convert the associative array to an array of objects.

$assoc = array (
array(
    'prop1'=>'val1',
    'prop2'=>'val2',
),

array(
    'prop1'=>'val1',
    'prop2'=>'val2',
),
)

Here Is the code I have so far:

class Assoc {
public function setObject($assoc) {
    $this->assoc[] = new Obj($assoc);
}
}
class Obj {
public function __construct($item) {
    foreach ( $item as $property=>$value ) {
        $this->{$property} = $value;
    }
}
}

$test = New Assoc();
$test->setObject($assoc);

This code will work for a single array but not an array of arrays. If you could help with what I believe to be the loop in the setObject function.

share|improve this question
1  
What is $input? Where is it defined? –  FakeRainBrigand Jan 14 '12 at 6:38
    
Sorry, in trying to generalize the code I missed changing a few vars. They are both $assoc and updated in the post. –  Matthew Sprankle Jan 14 '12 at 6:41

3 Answers 3

up vote 3 down vote accepted

EDIT for specific object:

To adhere to your existing style as close as possible without messing with array_map voodoo:

class Assoc {
  public function setObject($assoc) {
    foreach ($assoc as $arr) {
      $this->assoc[] = new Obj($arr);
    }   
  }
}
class Obj {
   public function __construct($item) {
     foreach ( $item as $property=>$value ) { 
       $this->{$property} = $value;
     }  
   }   
}

$test = New Assoc();
$test->setObject($assoc);

Original:

If you just need generic conversion, and not into specific custom objects (not exactly clear in your post?) you can try this:

$new_array = array();
foreach ($assoc as $to_obj)
{
  $new_array[] = (object)$to_obj;
}

// Print results
var_dump($new_array);

outputs:

array(2) {
  [0]=>
  object(stdClass)#1 (2) {
    ["prop1"]=>
    string(4) "val1"
    ["prop2"]=>
    string(4) "val2"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["prop1"]=>
    string(4) "val1"
    ["prop2"]=>
    string(4) "val2"
  }
}
share|improve this answer
    
I am looking for similar output into a Specific object. –  Matthew Sprankle Jan 14 '12 at 6:56
    
I am trying: foreach ($assoc as $to_obj) { $this->assoc[] = new Obj($to_obj); } But am getting invalid argument supplied for foreach –  Matthew Sprankle Jan 14 '12 at 7:01
    
array_map is considered "voodoo"? –  Joshua K Jan 14 '12 at 8:14
$len = count($assoc);
for($i=0;$i<$len; $i++){
    $assoc[$i] = (Object)$assoc[$i];
}
share|improve this answer

Convert the associative array to an array of objects:

$output = array_map(function($element) {
    return (object) $element;
}, $assoc);

Simple enough.

EDIT: If you need to make objects of a specific class:

$output = array_map(function($element) use ($classType) {
    return new $classType($element);
}, $assoc);

You can generalize it into just about anything, really.

share|improve this answer
    
Note Ending code is public function setInput($assoc) { $this->assoc= array_map(function($item){return new Input($item);}, $assoc); } –  Matthew Sprankle Jan 14 '12 at 7:39
    
You really, really, don't need classes for this. PHP already has two types of arrays: a built-in one and SPL "fixed" arrays. Use the first if you need hash-table semantics, and the second if you need fixed-size arrays. Don't create your own wrapper. –  Joshua K Jan 14 '12 at 7:58
    
.... two downvotes? really? –  Joshua K Jan 22 '12 at 6:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.