【问题标题】:Rearranging Array of WP_Post Objects and Arrays重新排列 WP_Post 对象和数组的数组
【发布时间】:2013-07-20 15:10:37
【问题描述】:

经过很长时间,我创建了一个函数,可以输出来自 WP 多站点网络中任何站点的最近帖子数组。

现在,我无法以某种格式获取此数组以轻松写出帖子。

SOURCE ARRAY(还有额外的 WP_Post 对象元素,为简洁起见我省略了)。

Array
(
   [0] => Array
       (
           [2013-07-15 04:19:59] => WP_Post Object
               (
                   [ID] => 1008
                   [post_author] => 52
                   [post_date] => 2013-07-15 04:19:59
                   [post_content] => This evening, we witnessed yet another travesty of justice in the ‘not guilty’ verdict on all counts against George Zimmerman the vigilante murderer of Trayvon Martin. 
                   [post_title] => Initial Statement on Verdict of George  Zimmerman Trial From Occu-Evolve
                   [guid] => http://occuevolve.web.net/?p=1
               )

           [2013-07-11 12:14:59] => WP_Post Object
               (
                   [ID] => 889
                   [post_author] => 1
                   [post_date] => 2013-07-11 12:14:59
                   [post_content] => Adam Weissman of OWS TradeJustice will be giving a talk on TPP (The Trans Pacific Partnership) on Sunday from 2-3 at Columbia.
                   [post_title] => This Sunday, Come to hear a talk on TPP – The Bankster’s Secret Plan to Escape Regulation and Kill Public Banks
                   [guid] => http://alternativebanking.web.net/?p=889
               )
           [2013-07-09 22:29:41] => WP_Post Object
               (
                   [ID] => 883
                   [post_author] => 1
                   [post_date] => 2013-07-09 22:29:41
                   [post_content] => It was just publicly and formally acknowledged, five years after AIG cost the US taxpayers billions of dollars that AIG is still a major problem.
                   [post_title] => AIG is still a threat to financial stability
                   [guid] => http://alternativebanking.web.net/?p=883
               )

           [2013-07-05 18:01:42] => WP_Post Object
               (
                   [ID] => 561
                   [post_author] => 12
                   [post_date] => 2013-07-05 18:01:42
                   [post_content] => 
                   [post_title] => The Good, The Bad, & The Ugly - Week of 7/1/2013
                   [guid] => http://occupythesec.web.net/?p=561
               )

           [2013-07-01 13:21:40] => WP_Post Object
               (
                   [ID] => 965
                   [post_author] => 51
                   [post_date] => 2013-07-01 13:21:40
                   [post_content] => Crossposted from mathbabe.org. Opinions expressed are those of Cathy O'Neil.
                   [post_title] => Payroll cards: "It costs too much to get my money" (#OWS)
                   [guid] => http://alternativebanking.web.net/?p=880
               )

           [2013-06-28 18:36:27] => WP_Post Object
               (
                   [ID] => 553
                   [post_author] => 50
                   [post_date] => 2013-06-28 18:36:27
                   [post_content] => 
                   [post_title] => The Good, The Bad, & The Ugly - Week of 6/24/2013
                   [guid] => http://occupythesec.web.net/?p=553
               )

           [2013-06-21 15:12:45] => WP_Post Object
               (
                   [ID] => 543
                   [post_author] => 50
                   [post_date] => 2013-06-21 15:12:45
                   [post_content] => 
                   [guid] => http://occupythesec.web.net/?p=543

               )

       )

   [1] => Array
       (
           [http://occuevolve.web.net/?p=1] => http://localhost/groups.occupy.net/web/occuevolve/2013/07/15/initial-statement-on-verdict-of-george-zimmerman-trial/
           [http://occupythesec.web.net/?p=561] => http://localhost/groups.occupy.net/web/occupythesec/2013/07/05/the-good-the-bad-the-ugly-week-of-712013/
           [http://occupythesec.web.net/?p=553] => http://localhost/groups.occupy.net/web/occupythesec/2013/06/28/the-good-the-bad-the-ugly-week-of-6242013/
           [http://alternativebanking.web.net/?p=889] => http://localhost/groups.occupy.net/web/altbanking/2013/07/this-sunday-come-to-hear-a-talk-on-tpp-the-banksters-secret-plan-to-escape-regulation-and-kill-public-banks/
           [http://alternativebanking.web.net/?p=883] => http://localhost/groups.occupy.net/web/altbanking/2013/07/aig-is-still-a-threat-to-financial-stability/
           [http://alternativebanking.web.net/?p=880] => http://localhost/groups.occupy.net/web/altbanking/2013/07/payroll-cards-it-costs-too-much-to-get-my-money-ows/
           [http://occupythesec.web.net/?p=543] => http://localhost/groups.occupy.net/web/occupythesec/2013/06/21/the-good-the-bad-the-ugly-week-of-61713/
       )

   [2] => Array
       (
           [http://occuevolve.web.net/?p=1] => 4
           [http://occupythesec.web.net/?p=561] => 3
           [http://occupythesec.web.net/?p=553] => 3
           [http://alternativebanking.web.net/?p=889] => 2
           [http://alternativebanking.web.net/?p=883] => 2
           [http://alternativebanking.web.net/?p=880] => 2
           [http://occupythesec.web.net/?p=543] => 3
       )

)

期望的结果

Array
(
   [http://occuevolve.web.net/?p=1] => Array
       (
        [ID] => 1008
        [post_author] => 52
        [post_date] => 2013-07-15 04:19:59
            [post_content] => This evening, we witnessed yet another travesty of justice in the ‘not guilty’ verdict on all counts against George Zimmerman the vigilante murderer of Trayvon Martin.
        [post_title] => Initial Statement on Verdict of George  Zimmerman Trial From Occu-Evolve
        [post_url] => http://localhost/groups.occupy.net/web/occuevolve/2013/07/15/initial-statement-on-verdict-of-george-zimmerman-trial/
        [blog_id] => 4
       )

   [http://alternativebanking.web.net/?p=889] => Array
       (
        [ID] => 889
        [post_author] => 1
        [post_date] => 2013-07-11 12:14:59
            [post_content] => Adam Weissman of OWS TradeJustice will be giving a talk on TPP (The Trans Pacific Partnership) on Sunday from 2-3 at Columbia.
        [post_title] => This Sunday, Come to hear a talk on TPP – The Bankster’s Secret Plan to Escape Regulation and Kill Public Banks
        [post_url] => http://localhost/groups.occupy.net/web/occupythesec/2013/07/05/the-good-the-bad-the-ugly-week-of-712013/
        [blog_id] => 3
       )

   [http://alternativebanking.web.net/?p=883] => Array
       (
        [ID] => 883
        [post_author] => 1
        [post_date] => 2013-07-09 22:29:41
            [post_content] => It was just publicly and formally acknowledged, five years after AIG cost the US taxpayers billions of dollars that AIG is still a major problem.
        [post_title] => AIG is still a threat to financial stability
        [post_url] => http://localhost/groups.occupy.net/web/occupythesec/2013/06/28/the-good-the-bad-the-ugly-week-of-6242013/
        [blog_id] => 3
       )
 etc.....
)

注意:

  • $array[1] 包含 post_url
  • $array[2] 包含 blog_id

【问题讨论】:

    标签: php multidimensional-array wordpress


    【解决方案1】:

    我认为这对你有用,但我还没有测试过。

    foreach ($array[0] as $wp_post){
      $desired_array[$wp_post->guid] = array(
         'ID' => $wp_post->ID,
         'post_author' => $wp_post->post_author,
         'post_date' => $wp_post->post_date,
         'post_content' => $wp_post->post_content,
         'post_title' => $wp_post->post_title,
         'post_url' => $array[1][$wp_post->guid],
         'blog_id' => $array[2][$wp_post->guid]
      );
    }
    

    更新:如果您想保留该对象,那么只需执行此操作(假设该对象具有属性 post_urlblog_id):

    foreach ($array[0] as $wp_post){
      $wp_post->post_url = $array[1][$wp_post->guid];
      $wp_post->blog_id= $array[2][$wp_post->guid]; 
      $desired_array[$wp_post->guid] = $wp_post;
    }
    

    【讨论】:

    • 非常感谢您的快速回复!您的解决方案确实有效!但是,它消除了对象,这可能对我的任务有所帮助。一位朋友帮助我解决了这个问题并提出了这个解决方案(见下文)。 (我会赞成你的回复,但我还没有足够高的代表)。
    • 不用担心,但您想要的结果表明您想要一个数组。我会更新我的答案。
    • 你说得对,我并不清楚。您的解决方案有效;它给出了相同的结果,但要小得多。再次感谢!
    【解决方案2】:

    一位朋友帮助我解决了这个问题。

    $blog_posts = array();
    foreach ($array[0] as $key => $value) {
        foreach ($array[1] as $guid => $post_url) {
            if ($value->guid == $guid) {
                $value->post_url = $post_url;
            }
        }
        foreach ($array[2] as $guid => $blog_id) {
            if ($value->guid == $guid) {
                $value->blog_id = $blog_id;
            }
        }
        $blog_posts[$value->guid] = $value;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2015-07-12
      • 2016-08-28
      相关资源
      最近更新 更多