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 8356185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:06:34+00:00 2026-06-09T10:06:34+00:00

I am stuck in a array manipulation task. Scenario: I have an array of

  • 0

I am stuck in a array manipulation task.

Scenario:
I have an array of Objects. Sample:

Array(
    [0] => stdClass Object(
        ['foo'] => 'foo1',
        ['bar'] => 'bar1',
        ['baz'] => 'baz1'
        ),
    [1] => stdClass Object(
        ['foo'] => 'foo2',
        ['bar'] => 'bar2',
        ['baz'] => 'baz2'
        ),
    [2] => stdClass Object(
        ['foo'] => 'foo3',
        ['bar'] => 'bar3',
        ['baz'] => 'baz3'
        )
)

I have to convert it into this form:

Array(
    ['foo1'] => Array('bar1', 'baz1'),
    ['foo2'] => Array('bar2', 'baz2'),
    ['foo3'] => Array('bar3', 'baz3'),
)

Here is the function that i create to achieve this:

function createAttributeString($attributes)
{
    $finalString = '';
    $string = array();
    $column = array();

    foreach( $attributes as $attrib )
    {
        if( $attrib->primary_key == '1' )
            $column[$attrib->name] = array("'$attrib->type'", "'$attrib->max_length'", '\'pk\'');
        else
            $column[$attrib->name] = array("'$attrib->type'", "'$attrib->max_length'");

        $string[$attrib->name] = 'array('.implode(',', $column[$attrib->name]).')';
    }

    $finalString = 'array('.implode(',', $string).')';
    return $finalString;
}

But the output of this function is:

Array(
    [0] => Array('bar1', 'baz1'),
    [1] => Array('bar2', 'baz2')
    [2] => Array('bar3', 'baz3')
)

I know this is because of the last implode function that i have used in $finalString, but i dont understand what should i use instead to get my desired output.

Any help or suggestion would be highly appreciated.

UPDATE:
The final array should be return in a string format.

  • 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-06-09T10:06:36+00:00Added an answer on June 9, 2026 at 10:06 am

    UPDATE 2

    In addition to the code below I previously posted, you can use the following code to “pretty-print” your array in your desired form:

    $s = "Array(" . "<br />";
    foreach ($output as $key => $array) {
        $s .= "['" . $key . "'] => Array('";
        $s .= implode("', '", $array);
        $s .= "')," . "<br />";
    }
    $s .= ")";
    print $s;
    

    will output:

    Array(
    ['foo1'] => Array('bar1', 'baz1'),
    ['foo2'] => Array('bar2', 'baz2'),
    ['foo3'] => Array('bar3', 'baz3'),
    )
    

    Hope it helps!


    UPDATE

    After you updated the question, and mentioned that the array is consisting of stdClass objects, I have updated the code that works with stdClass and then stores the output in a string variable which can be printed later:

    <?php
    
    // Basic setup of test variables
    $arr = array(
        array("foo" => "foo1", "bar" => "bar1", "baz" => "baz1", "car" => "car1"), 
        array("foo" => "foo2", "bar" => "bar2", "baz" => "baz2", "car" => "car2"), 
        array("foo" => "foo3", "bar" => "bar3", "baz" => "baz3", "car" => "car3")
           );
    
    // array of objects
    $arrObject = array();
    
    // convert above array to an array of stdClass objects
    foreach ($arr as $array) {
        $object = new stdClass();
        foreach ($array as $key => $value) {
            $object->$key = $value;
        }
        $arrObject[] = $object;
    }   
    
    // print to see if the array structure is the right one
    print "<pre>";
    print_r($arrObject);
    print "</pre>";
    
    // now the real code starts, assuming $arrObject is the input array you specified in your question
    $output = array();
    foreach ($arrObject as $a) {
      $a = (array) $a;
      $key = key($a);
      next($a);
      $output[$a[$key]] = array();
      while (($key2 = key($a)) !== null) {
        next($a);
        $output[$a[$key]][] = $a[$key2];
      }
    }
    
    $str = print_r($output, true);
    print "<pre>";
    print $str;
    print "</pre>";
    

    The above code will print the following output on screen:

    [input]
    Array
    (
        [0] => stdClass Object
            (
                [foo] => foo1
                [bar] => bar1
                [baz] => baz1
                [car] => car1
            )
    
        [1] => stdClass Object
            (
                [foo] => foo2
                [bar] => bar2
                [baz] => baz2
                [car] => car2
            )
    
        [2] => stdClass Object
            (
                [foo] => foo3
                [bar] => bar3
                [baz] => baz3
                [car] => car3
            )
    
    )
    
    [output]
    Array
    (
        [foo1] => Array
            (
                [0] => bar1
                [1] => baz1
                [2] => car1
            )
    
        [foo2] => Array
            (
                [0] => bar2
                [1] => baz2
                [2] => car2
            )
    
        [foo3] => Array
            (
                [0] => bar3
                [1] => baz3
                [2] => car3
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array in C#, and my logic is stuck and it can only
I am stuck in allocating data in array. for example i have 100 words
Im stuck on a sorting problem, I have an array with 10 numbers (1-10)
Doing homework and I'm stuck. Let's say I have an array colors: [blue, orange,
in C code I'm stuck to pass an array of struct to a function,
I have a struck with an array of pthread pointers. Each thread is meant
I have a structure which contains character array on C side stuct s {
I have lots of details. I want to stack them within an array, can
Kinda stuck here... I have an application with lets say 5000 rows of data
I am a little stuck on how to create this array. My data: category_id

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.