0

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
                    (
                        [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 : (

2 Answers 2

2

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')
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Daniel, thanks for the idea. The merge and foreach seem to have assisted greatly, but the main issue I am facing is using a non-numeric index as it is now. I am trying to achieve an associative array format with no numbers like: [Tree] => Array ( [id] => 2 [name] => Fur )
I don't understand. Can you edit your question with a pastebin of an exemplar array you're working with?
Hi Daniel, I apologize for being unclear. Here is a pastebin of the array I am trying to format. Basically the index should not be numeric and only associative. Link: pastebin.com/SuSJENR5
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.