Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:02:42+00:00 2026-05-28T05:02:42+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:02:42+00:00Added an answer on May 28, 2026 at 5:02 am

    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"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert an xml document into an associative array. I want
Hi I'm trying to convert the XML file into associative array by using the
I am trying to modify an associative array eg. array( 'key1' => 'val1', 'key2'
I have a code in vb, and I'm trying convert it to c#. _nextContactDate.ToShortDateString
How to do an update in LINQ to Objects. Trying convert SQL to Linq
Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
I'm trying to convert some code that worked great in VB, but I can't
How to extract and save array from string parameter? I'm trying convert string beafore_create
Trying to convert a two dimensional array to a two dimensional JSON.Net array. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.