【问题标题】:php two arrays into stringphp 将两个数组转换成字符串
【发布时间】:2017-12-01 13:21:22
【问题描述】:

使用 php print_r 我得到了最终结果:

Array ( [0] => 31 [1] => 21 ) Array ( [0] => 33 [1] => 27 )

我希望结果类似于 31、21、33、27。

我被困住了。请指导我。

谢谢, 维克拉姆

**请用冗长的东西裸露。 **

我有 2 个主要类别,需要在主页上显示 3 个四级子级。类别如下:

类别 > 服装。
分类 > 服装 > 男装。
分类 >> 服装 > 男装 > 上衣。
类别 > 服装 > 男装 > 上衣 > 牛仔裤。

代码

<div class="container products" data-aos="fade-down">
   <div class="page-titles" data-aos="fade-up">
      Products
      <hr />
   </div>
   <!--page-titles-->   
   <?php
      $homepage_products=get_post_meta($post->ID, 'homepage_products', TRUE);
      $homepage_parent=explode(',', $homepage_products);

      global $parent_one;
      global $x;
      $x=1;
      foreach($homepage_parent as $parent_one)
      {

        $parent_one_title = get_the_title( $parent_one );
        $content_post = get_post($parent_one);
        $content = $content_post->post_content;
        $content = apply_filters('the_content', $content);

      ?>
   <div class="row clothing">
      <div id="<?php echo $x++; ?>" class="col-xs-12 col-sm-12 col-md-4 col-lg-4
         <?php if($x % 2)
            {
                echo 'col-md-push-8 col-lg-push-8';
            }
            ?>
         ">
         <?php echo get_the_post_thumbnail($parent_one,  'medium_large', array('class' => 'img-'.$x)); ?>
      </div>
      <!--col-4 img-->
      <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8
         <?php if($x % 2)
            {
                echo 'col-md-pull-4 col-lg-pull-4';
            }
            ?>
         ">
         <h2><?php echo $parent_one_title; ?></h2>
         <?php echo $content; ?>
         <!-- child pages of <?php echo strtolower($parent_title); ?> starts-->
         <div class="row">
            <?php
               $parent_two=get_pages( array('parent'=>$parent_one,'child_of'=>$parent_one) );


               foreach($parent_two as $parent_two_ids)
               {

                $parent_two_id=$parent_two_ids->ID;
                $parent_two_id.=",";

                $parent_three=get_pages( array('parent'=>$parent_two_id) );                                 

                $str_id = array(); 
                foreach($parent_three as $parent_three_ids)
                {
                    $parent_three_id=$parent_three_ids->ID;
                    $parent_three_id.=',';

                    $parent_four=get_pages( array('parent'=>$parent_three_id) );

                    $four_ids=array();
                    foreach($parent_four as $parent_four_ids)
                    {
                        $four_id=$parent_four_ids->ID;
                        $four_ids[]=$four_id;
                    }//parent_four_ids                                              



                        // HERE IS MY ISSUE //                              
                        echo implode(',', array_merge($four_ids) );
                        // HERE IS MY ISSUE //                              



                        //$ids=implode(',',$four_ids);
                        //$idx=$ids;
                        //$idv=preg_replace('#\s+#',',',trim($idx));
                        //echo $idv;
                        //$str_id[]=$idx;   
                        //$str_idx=implode(',',$str_id);    
                        //echo $str_idx;            






                            $content=apply_filters('the_content', $parent_four_ids->post_content);

                            $c_length = 80;
                            if (strlen($content) > $c_length)
                                {
                                    $content = wordwrap($content, 80);
                                    $i = strpos($content, "\n");
                                        if ($i) {
                                            $content = substr($content, 0, $i);
                                        }
                                }                                           


                    ?>
            <!--  <div class="col-xs-12 col-sm-12 col-md-3 col-lg-3 product">
               < ?php echo get_the_post_thumbnail($parent_four_ids,  'medium_large'); ?>
                    <a href="< ?php echo get_permalink($parent_four_ids);?>">
                    <h4>< ?php echo $parent_four_ids->post_title; ?></h4>
                    </a>
                < ?php echo $content; ?>
               </div><!--col 3 #< ?php echo $parent_four_ids->ID; ?>-->
            <?php   
               }//parent_three_ids

               } // parent_two_ids

               ?>
         </div>
         <!-- row category - slider- child pages of <?php echo strtolower($parent_one_title); ?> ends-->
      </div>
      <!-- col-6 content - child pages-->
   </div>
   <!--row <?php echo strtolower($parent_title); ?>-->
   <? } //home_parent ?>
</div>
<!--container products-->

【问题讨论】:

  • array_merge + implode
  • @iainn : 内爆给我的结果是 31,21 33,27。 2个数组之间的空格:( ...
  • 如果您不发布任何代码,我们将无法帮助您
  • @iainn :我同意 - 实际上我正在使用 wordpress,总共有 4 个循环。所以我有点想知道如何在这里发布这么大的东西。将尝试发布代码。

标签: php arrays


【解决方案1】:

正如 iainn 上面所说的:

php > echo implode(',', array_merge([31, 21], [33, 27]));
31,21,33,27

如果您想要单个数组而不是字符串,请删除 implode() 调用。

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 2018-01-29
    • 2013-08-12
    • 2021-12-27
    • 1970-01-01
    • 2013-05-12
    • 2010-10-15
    相关资源
    最近更新 更多