-1

我陷入了数组操作任务。

场景:
我有一个对象数组。样本:

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'
        )
)

我必须将其转换为这种形式:

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

这是我为实现此目的而创建的功能:

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

但是这个函数的输出是:

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

我知道这是因为我在 $finalString 中使用的最后一个内爆函数,但我不明白应该使用什么来获得我想要的输出。

任何帮助或建议将不胜感激。

更新:
最终数组应以字符串格式返回。

4

1 回答 1

1

更新 2

除了我之前发布的以下代码之外,您还可以使用以下代码以所需的形式“漂亮地打印”您的数组:

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

将输出:

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

希望能帮助到你!


更新

在您更新问题并提到该数组由 stdClass 对象组成后,我更新了与 stdClass 一起使用的代码,然后将输出存储在一个字符串变量中,稍后可以打印:

<?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>";

上面的代码将在屏幕上打印以下输出:

[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
        )

)
于 2012-08-08T16:11:59.327 回答