开发者

CakePHP set::combine(). Need a numeric array transformed into an associative array

I have been attempting to structure an array using the set::combine method, and I cannot get it working (what am I doing wrong!).

And I will note I need to turn this into an associative array like:

[Tree] => Array
         (
             [id] => 1
             [name] => Pine
          )...

Here is an example of my Array:

Array
(
[1] => Array
    (
        [1] => Array
            (
                [Tree] => Array
                    (
          开发者_Python百科              [id] => 1
                        [name] => Pine
                    )

            )

    )...

And here is my set::combine call:

$combine = Set::combine($this->data,'{n}.Tree.id','{n}.Tree.name');
debug($combine);

And here is the debug output of $combine:

Array
(
    [] => 
)

I can do:

$combine = Set::combine($this->data,'{n}.{n}.Tree.id','{n}.{n}.Tree.name');

But I still get the numeric index!

Array
 (
[Array] => Array
    (
        [0] => Oak nnn
    )

)

I've tried every example and have been over the manual section on combine all evening. Can't get this working : (


I'm not entirely sure I understand what specific format the output array needs to be. If the first dimension of the array is a single row, you should be able to do the following:

array_values(Set::combine(reset($this->data), '/Tree/id', '/Tree'));

If it has multiple rows, you will need to iterate through this first dimension of the array with foreach, and Set::combine each row in turn, using Set::merge to merge the results into your output array:

$outArray = array()
foreach($this->data as $row) {
    $outArray = Set::merge( $outArray, Set::combine($row, '/Tree/id', '/Tree'));
}

Hope this helps.


Update:

Based on the requirements clarified in the comments, you would transform the arrays like this:

$outArray = array('Tree'=>array());
foreach($this->data as $row) {
    $outArray['Tree'] = Set::merge(
        $outArray['Tree'],
        Set::combine($row, '{n}.Tree.id', '{n}.Tree')
    );
}


I understand what specific format the output array needs to be. If the first dimension of the array is a single row, you should be able to remove like following code

$combine = Set::classicExtract($this->data, '{n}.Tree');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜