如何将包含值的对象数组转换为由该值索引的数组?

发布于 2024-12-04 13:19:36 字数 3601 浏览 0 评论 0原文

我的数组结构如下:

Array
(
    [0] => stdClass Object
    (
            [ID] => 277
            [post_author] => 1
            [post_date] => 2011-09-02 08:34:03
            [post_date_gmt] => 2011-09-02 08:34:03
            [post_content] => <div class="sol_topcont">
            [menu_order] => 103
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [1] => stdClass Object
    (
            [ID] => 275
            [post_author] => 1
            [post_date] => 2011-09-02 08:32:36
            [post_date_gmt] => 2011-09-02 08:32:36
            [post_content] => <div class="sol_topcont1">
            [menu_order] => 100
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [2] => stdClass Object
    (
            [ID] => 280
            [post_author] => 1
            [post_date] => 2011-09-02 08:35:24
            [post_date_gmt] => 2011-09-02 08:35:24
            [post_content] => <div class="sol_topcont">
            [menu_order] => 102
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [3] => stdClass Object
    (
            [ID] => 282
            [post_author] => 1
            [post_date] => 2011-09-02 08:36:31
            [post_date_gmt] => 2011-09-02 08:36:31
            [post_content] => <div class="sol_topcont">
            [menu_order] => 101
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

)

我需要使用 ['menu_order'] 键值对该数组进行排序。如何将上面的数组转换为这样的数组:

Array
(

    [100] => stdClass Object
    (
        [ID] => 275
        [post_author] => 1
        [post_date] => 2011-09-02 08:32:36
        [post_date_gmt] => 2011-09-02 08:32:36
        [post_content] => <div class="sol_topcont1">
        [menu_order] => 100
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )



    [101] => stdClass Object
    (
        [ID] => 282
        [post_author] => 1
        [post_date] => 2011-09-02 08:36:31
        [post_date_gmt] => 2011-09-02 08:36:31
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 101
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
    [102] => stdClass Object
    (
        [ID] => 280
        [post_author] => 1
        [post_date] => 2011-09-02 08:35:24
        [post_date_gmt] => 2011-09-02 08:35:24
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 102
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    [103] => stdClass Object
    (
        [ID] => 277
        [post_author] => 1
        [post_date] => 2011-09-02 08:34:03
        [post_date_gmt] => 2011-09-02 08:34:03
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 103
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
)

I have array structured like this:

Array
(
    [0] => stdClass Object
    (
            [ID] => 277
            [post_author] => 1
            [post_date] => 2011-09-02 08:34:03
            [post_date_gmt] => 2011-09-02 08:34:03
            [post_content] => <div class="sol_topcont">
            [menu_order] => 103
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [1] => stdClass Object
    (
            [ID] => 275
            [post_author] => 1
            [post_date] => 2011-09-02 08:32:36
            [post_date_gmt] => 2011-09-02 08:32:36
            [post_content] => <div class="sol_topcont1">
            [menu_order] => 100
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [2] => stdClass Object
    (
            [ID] => 280
            [post_author] => 1
            [post_date] => 2011-09-02 08:35:24
            [post_date_gmt] => 2011-09-02 08:35:24
            [post_content] => <div class="sol_topcont">
            [menu_order] => 102
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

    [3] => stdClass Object
    (
            [ID] => 282
            [post_author] => 1
            [post_date] => 2011-09-02 08:36:31
            [post_date_gmt] => 2011-09-02 08:36:31
            [post_content] => <div class="sol_topcont">
            [menu_order] => 101
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
    )

)

I need to sort this array using the ['menu_order'] key value. How do I convert the above array into one like this:

Array
(

    [100] => stdClass Object
    (
        [ID] => 275
        [post_author] => 1
        [post_date] => 2011-09-02 08:32:36
        [post_date_gmt] => 2011-09-02 08:32:36
        [post_content] => <div class="sol_topcont1">
        [menu_order] => 100
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )



    [101] => stdClass Object
    (
        [ID] => 282
        [post_author] => 1
        [post_date] => 2011-09-02 08:36:31
        [post_date_gmt] => 2011-09-02 08:36:31
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 101
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
    [102] => stdClass Object
    (
        [ID] => 280
        [post_author] => 1
        [post_date] => 2011-09-02 08:35:24
        [post_date_gmt] => 2011-09-02 08:35:24
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 102
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    [103] => stdClass Object
    (
        [ID] => 277
        [post_author] => 1
        [post_date] => 2011-09-02 08:34:03
        [post_date_gmt] => 2011-09-02 08:34:03
        [post_content] => <div class="sol_topcont">
                    [menu_order] => 103
        [post_type] => page
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
)

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

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

发布评论

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

评论(3

只是偏爱你 2024-12-11 13:19:36
  1. 创建一个新的空数组。
  2. 循环遍历原始数组。
  3. 检索循环中当前元素的 menu_order 键。
  4. 将当前元素存储在新数组中,并以 menu_order 为键。
  5. 循环结束后,销毁原数组。

像这样的东西:

<?php

$newArray = array();
foreach($oldArray as $element)
{
    $menuKey = (string) $element->menu_order;
    $newArray[$menuKey] = $element;
}

$oldArray = null;
 // $newArray now has required data 

?>

}

  1. create a new empty array.
  2. traverse your original array in a loop.
  3. retreive menu_order key for current element in loop.
  4. store the current element in the new array with menu_order as key.
  5. after the loop,destroy original array.

Something like :

<?php

$newArray = array();
foreach($oldArray as $element)
{
    $menuKey = (string) $element->menu_order;
    $newArray[$menuKey] = $element;
}

$oldArray = null;
 // $newArray now has required data 

?>

}

々眼睛长脚气 2024-12-11 13:19:36

我得到了答案

    foreach($whyapptivo_sub_pages as $pages)
    {
            $key = $pages->menu_order;
            $final[$key] = $pages;
            ksort($final);
        }
   foreach($final as $page)
    {
             ........
          }

I got an Answer

    foreach($whyapptivo_sub_pages as $pages)
    {
            $key = $pages->menu_order;
            $final[$key] = $pages;
            ksort($final);
        }
   foreach($final as $page)
    {
             ........
          }
那片花海 2024-12-11 13:19:36

PHP 有一个 usort 函数,您可以将自定义比较器传递给其中。您需要创建一个具有两个参数(在本例中是两个对象)的函数,对它们进行比较,并根据比较结果返回 -1、0 或 1。

http://www.php.net/manual/en/function.usort.php

示例

function cmp($a, $b) {
    if ($a->menu_order == $b->menu_order) {
        return 0;
    }
    return ($a->menu_order < $b->menu_order) ? -1 : 1;
}

usort($your_array, 'cmp');

PHP has a usort function you can pass a custom comparator into. You will need to make a function that has two arguments (in this instance two of your objects), compare them, and return -1, 0 or 1 depending on the comparison.

http://www.php.net/manual/en/function.usort.php

Example

function cmp($a, $b) {
    if ($a->menu_order == $b->menu_order) {
        return 0;
    }
    return ($a->menu_order < $b->menu_order) ? -1 : 1;
}

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