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.
See Question&Answers more detail:
os