1

The following question can either be solved by probably changing my use of the find method in Cake PHP OR using some PHP function. I would prefer to solve it using Cake but it doesn't matter too much. I have any array like this:

Array
(
    [0] => Array
        (
            [Model] => Array
                (
                    [id] => 14
                    [foo] => bar
                )

        )

    [1] => Array
        (
            [Model] => Array
                (
                    [id] => 15
                    [foo] => something
                )

        ) .............

I just want to remove the Model index and just use the numeric one. The following function generated this array:

$arr = $this->Model->find('all', array('contain' => false ) );

I probably need to change the 'contain' part of the call. Basically, in addition to the data that appears under each Model index, I also have a second and third model and the contain = false just restricts Cake from getting data from the current model (Model).

0

6 Answers 6

4

If I understand your question correctly, I think CakePHP's Set::combine function will help http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine :

$result = Set::combine($your_array, '{n}.Model.id', '{n}.Model.data');

This will result in:

 Array
 (
     [14] => Array
         (
             [foo] => bar
         )
     [15] => Array
         (
             [foo] => something
         )
 )
Sign up to request clarification or add additional context in comments.

Comments

3

you have to write your own piece of code to modify your array , here is a function that will do what you want (you can improve it)

function reformArray($array,$modelName)
{

    foreach($arr as $key => $value)
    {
        $newArr[] = $arr[$key][$modelName];
    }

    return $newArr;
}

you have to pass your array and the Model Name to this function and will return you the result

Comments

2
$a = array(
    array(
        'User' => array(
            'id' => 2,
            'group_id' => 1,
            'Data' => array(
                'user' => 'mariano.iglesias',
                'name' => 'Mariano Iglesias'
            )
        )
    ),
    array(
        'User' => array(
            'id' => 14,
            'group_id' => 2,
            'Data' => array(
                'user' => 'phpnut',
                'name' => 'Larry E. Masters'
            )
        )
    ),
    array(
        'User' => array(
            'id' => 25,
            'group_id' => 1,
            'Data' => array(
                'user' => 'gwoo',
                'name' => 'The Gwoo'
            )
        )
    )
);

RUN THIS TO REMOVE THE MODEL NAME USER

Set::extract($a, '{n}.User');

WILL RETURN THIS

$a = array(
        array(

                'id' => 2,
                'group_id' => 1,
                'Data' => array(
                    'user' => 'mariano.iglesias',
                    'name' => 'Mariano Iglesias'
                )

        ),
        array(

                'id' => 14,
                'group_id' => 2,
                'Data' => array(
                    'user' => 'phpnut',
                    'name' => 'Larry E. Masters'
                )

        ),
        array(

                'id' => 25,
                'group_id' => 1,
                'Data' => array(
                    'user' => 'gwoo',
                    'name' => 'The Gwoo'
                )

        )
    );

Comments

0

In CakePHP 2:

$data = $this->Model->find('all');
$data = Set::extract('/Model/.', $data );

You can achieve this result by foreach also

foreach ($data as $k => &$t) {
    $t = $t['Model']
}

Comments

0

It will be much easier with Object. Change the Array with Object.

Actually I had faced the same problem with nested array And I found the solution with Set::map($array);

If you don't want the model_name as nested array, You can prefer this solution.

$data = array(
    array(
        "IndexedPage" => array(
            "id" => 1,
            "url" => 'http://blah.com/',
            'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
            'get_vars' => '',
            'redirect' => '',
            'created' => "1195055503",
            'updated' => "1195055503",
        )
    ),
    array(
        "IndexedPage" => array(
            "id" => 2,
            "url" => 'http://blah.com/',
            'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
            'get_vars' => '',
            'redirect' => '',
            'created' => "1195055503",
            'updated' => "1195055503",
        ),
    )
);

$mapped = Set::map($data);

/* $mapped now looks like: */

    Array
    (
        [0] => stdClass Object
            (
                [_name_] => IndexedPage
                [id] => 1
                [url] => http://blah.com/
                [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
                [get_vars] =>
                [redirect] =>
                [created] => 1195055503
                [updated] => 1195055503
            )

        [1] => stdClass Object
            (
                [_name_] => IndexedPage
                [id] => 2
                [url] => http://blah.com/
                [hash] => 68a9f053b19526d08e36c6a9ad150737933816a5
                [get_vars] =>
                [redirect] =>
                [created] => 1195055503
                [updated] => 1195055503
            )

    )

Comments

-1

you may do this :

$tmp = $this->Model->find('all', array('contain' => false ) );

$arr = $tmp['ModelName'];

3 Comments

you don't have to add th 'contain' to the query !
I always use contain = array() - but either way he has to add it if the default level of recursive is greater -1!
No, the array structure isn't like that.

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.