7

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.

2
  • 1
    What is $input? Where is it defined? Commented Jan 14, 2012 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. Commented Jan 14, 2012 at 6:41

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Note Ending code is public function setInput($assoc) { $this->assoc= array_map(function($item){return new Input($item);}, $assoc); }
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.
6

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"
  }
}

3 Comments

I am looking for similar output into a Specific object.
I am trying: foreach ($assoc as $to_obj) { $this->assoc[] = new Obj($to_obj); } But am getting invalid argument supplied for foreach
array_map is considered "voodoo"?
1
$len = count($assoc);
for($i=0;$i<$len; $i++){
    $assoc[$i] = (Object)$assoc[$i];
}

Comments

0

You have an indexed array of associative arrays. If you convert it to json then back to an iterable state with the default behavior of json_decode(), the top level (indexed array) will be cast as array-type while the subarrays will become object-type.

Note that this will conversion will permeate all the way through subsequent levels of data (in case researchers might have deeper data structures). Effectively, indexed arrays remain indexed arrays and associative arrays become objects.

This is such a basic call that I am not sure that creating a wrapper for it is necessary.

Code: (Demo)

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

var_export(
    json_decode(json_encode($assoc))
);

Output:

array (
  0 => 
  (object) array(
     'prop1' => 'val1',
     'prop2' => 'val2',
  ),
  1 => 
  (object) array(
     'prop1' => 'val1',
     'prop2' => 'val2',
  ),
)

Comments

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.