删除数组中不必要的嵌套索引(由 Cake PHP 生成)

发布于 2025-01-06 12:38:22 字数 838 浏览 0 评论 0 原文

下面的问题可以通过改变我在 Cake PHP 中使用 find 方法或使用一些 PHP 函数来解决。我更愿意使用 Cake 来解决它,但这并不重要。我有这样的数组:

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

        )

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

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

我只想删除 Model 索引并仅使用数字索引。以下函数生成了此数组:

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

我可能需要更改调用的 'contain' 部分。基本上,除了每个 Model 索引下出现的数据之外,我还有第二个和第三个模型,并且 contain = false 只是限制 Cake 从当前模型获取数据模型(模型)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风柔一江水 2025-01-13 12:38:22

如果我正确理解你的问题,我认为 CakePHP 的 Set::combine 函数会有所帮助 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');

这将导致:

 Array
 (
     [14] => Array
         (
             [foo] => bar
         )
     [15] => Array
         (
             [foo] => something
         )
 )

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
         )
 )
护你周全 2025-01-13 12:38:22

您必须编写自己的代码来修改您的数组,这里有一个函数可以执行您想要的操作(您可以改进它)

function reformArray($array,$modelName)
{

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

    return $newArr;
}

您必须将数组和模型名称传递给此函数并将返回结果

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

黯然 2025-01-13 12:38:22
$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'
            )
        )
    )
);

运行此命令以删除模型名称用户

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

将返回此命令

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

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

        )
    );
迷离° 2025-01-13 12:38:22

在 CakePHP 2 中:

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

您也可以通过 foreach 实现此结果

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

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']
}
2025-01-13 12:38:22

使用Object 会容易得多。将Array 更改为Object

实际上,我在嵌套数组方面也遇到了同样的问题,并且我找到了Set::map($array);

如果您不希望 model_name 作为嵌套数组,您可以更喜欢这个解决方案。

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

    )

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
            )

    )
好倦 2025-01-13 12:38:22

你可以这样做:

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

$arr = $tmp['ModelName'];

you may do this :

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

$arr = $tmp['ModelName'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文