PHP:修改多维数组最简单,最干净的方法吗?

时间:2017-01-14 17:55:04

标签: php arrays multidimensional-array

我有一个动态的多维数组,可以是任何维度,这里是一个样本..


$array_serial = 'a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:27:"products.php?la=electronics";s:6:"otitle";s:11:"Electronics";s:6:"utitle";s:11:"Electronics";s:4:"ttip";s:11:"Electronics";}s:8:"children";a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:51:"products.php?la=electronics&lb=Computer-Peripherals";s:6:"otitle";s:20:"Computer Peripherals";s:6:"utitle";s:20:"Computer Peripherals";s:4:"ttip";s:34:"Electronics : Computer Peripherals";}s:8:"children";a:1:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:63:"products.php?la=electronics&lb=Computer-Peripherals&lc=Printers";s:6:"otitle";s:8:"Printers";s:6:"utitle";s:8:"Printers";s:4:"ttip";s:45:"Electronics : Computer Peripherals : Printers";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:90:"products.php?la=electronics&lb=Computer-Peripherals&lc=Printers&ld=Single-Function-Printer";s:6:"otitle";s:23:"Single Function Printer";s:6:"utitle";s:23:"Single Function Printer";s:4:"ttip";s:71:"Electronics : Computer Peripherals : Printers : Single Function Printer";}}}}}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:38:"products.php?la=electronics&lb=Mobiles";s:6:"otitle";s:7:"Mobiles";s:6:"utitle";s:7:"Mobiles";s:4:"ttip";s:21:"Electronics : Mobiles";}}}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:31:"products.php?la=homenfurnitures";s:6:"otitle";s:19:"Home And Furnitures";s:6:"utitle";s:19:"Home And Furnitures";s:4:"ttip";s:19:"Home And Furnitures";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:9:"about.php";s:6:"otitle";s:8:"About Us";s:6:"utitle";s:8:"About Us";s:4:"ttip";s:8:"About US";}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:40:"products.php?la=homenfurnitures&lb=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:27:"Home And Furnitures : Other";}s:8:"children";a:1:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:51:"products.php?la=homenfurnitures&lb=Lights-AnD-Lamps";s:6:"otitle";s:14:"Lights & Lamps";s:6:"utitle";s:14:"Lights & Lamps";s:4:"ttip";s:36:"Home And Furnitures : Lights & Lamps";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:12:"contacts.php";s:6:"otitle";s:10:"Contact Us";s:6:"utitle";s:10:"Contact Us";s:4:"ttip";s:10:"Contact US";}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:58:"products.php?la=homenfurnitures&lb=Dinnerware-AnD-Crockery";s:6:"otitle";s:21:"Dinnerware & Crockery";s:6:"utitle";s:21:"Dinnerware & Crockery";s:4:"ttip";s:43:"Home And Furnitures : Dinnerware & Crockery";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:78:"products.php?la=homenfurnitures&lb=Dinnerware-AnD-Crockery&lc=Cups-AnD-Saucers";s:6:"otitle";s:14:"Cups & Saucers";s:6:"utitle";s:14:"Cups & Saucers";s:4:"ttip";s:60:"Home And Furnitures : Dinnerware & Crockery : Cups & Saucers";}}}}}}}}}}}';

此数组的所有索引“otitle”将以输入类型文本的形式显示,用户可以根据需要修改此标题。我怎样才能以干净的方式实现这一目标?

这只是数组的一部分,实际的数组可能要大得多。

这是我的code to view "otitle" in a input type

function get_menu_list($selected_menus)
{

    if($selected_menus != null){
    foreach($selected_menus as $menu_level_1)
    {

        $title = implode("///",$menu_level_1['id']);

        echo '
            <li class="dd-item">
                <div class="dd-handle">
                    <input type="text" name="otitle[]" value="'.$menu_level_1['id']['otitle'].'" />
                </div>
        ';

        if(isset($menu_level_1['children']))
        {
            echo '<ol class="dd-list">';

            foreach($menu_level_1['children'] as $menu_level_2)
            {

                $title_level_2 = implode("///",$menu_level_2['id']);

                echo '
                    <li class="dd-item">
                        <div class="dd-handle">
                            <input type="text" name="otitle[]" value="'.$menu_level_2['id']['otitle'].'" />
                    </div>
                    ';

                if(isset($menu_level_2['children']))
                {
                    echo '<ol class="dd-list">';

                    foreach($menu_level_2['children'] as $menu_level_3)
                    {

                        $title_level_3 = implode("///",$menu_level_3['id']);

                        echo '
                            <li class="dd-item">
                              <div class="dd-handle">
                                <input type="text" name="otitle[]" value="'.$menu_level_3['id']['otitle'].'" />
                              </div>
                          ';

                        if(isset($menu_level_3['children']))
                        {
                            echo '<ol class="dd-list">';

                            foreach($menu_level_3['children'] as $menu_level_4)
                            {
                                $title_level_4 = implode("///",$menu_level_4['id']);

                                    echo '
                                        <li class="dd-item">
                                          <div class="dd-handle">
                                            <input type="text" name="otitle[]" value="'.$menu_level_4['id']['otitle'].'" />
                                          </div>
                                      ';

                                if(isset($menu_level_4['children']))
                                {
                                    echo '<ol class="dd-list">';

                                    foreach($menu_level_4['children'] as $menu_level_5)
                                    {
                                        $title_level_5 = implode("///",$menu_level_5['id']);

                                        echo '
                                            <li class="dd-item">
                                              <div class="dd-handle">
                                                <input type="text" name="otitle[]" value="'.$menu_level_5['id']['otitle'].'" />
                                              </div>
                                            </li>
                                          ';
                                    }

                                    echo "</ol>";
                                }

                                echo '</li>';
                            }

                            echo "</ol>";

                        }

                        echo '</li>';

                    }

                    echo "</ol>";
                }

                echo '</li>';

            }

            echo '</ol>';
        }

        echo '</li>';

    }

    }//end if
    else
    return null;    
}

$array_unserial = unserialize($array_serial);
echo get_menu_list($array_unserial);

2 个答案:

答案 0 :(得分:0)

基本理念

您正在寻找的想法称为递归。递归通过命名函数调用自身,直到满足各种条件。例如,数学运算符(读作:“Factorial”)是了解递归如何工作的最简单方法之一。通过从您的给定数字开始定义阶乘,比如说5,然后乘以该数字的每个整数,直到达到1。因此,5!计算如下:5*4*3*2*1。代码可能类似于:

function factorial($n){
  if($n<=1) return 1; 
  //If the number being passed is one or lower, no need to call this function again, just return 1.
  return $n*factorial($n-1); 
  //If we got to here, the $n is not 1 or less, so we multiply $n times the answer of factorial($n-1), which will repeat until $n-1 becomes 1.
}

我发现this Quora question我认为这两个答案都有助于以不同的方式解释递归的想法。

答案

现在,针对您的问题。我评论了我能想到的一切,所以如果你什么都不懂,请告诉我。

<style>
    div.menu-item > div.menu-item {
        margin-left:1rem;
    }
</style>
<?php
function get_menu_list($selected_menus,$offset_i=false)
{

    foreach($selected_menus as $i=>$menu): 
  //the : is an alternate syntax in PHP for {}, now instead of trying to find a matching }, PHP will look for the keyword "endforeach;"
    $id_string = ($offset_i===false) ? $i : $offset_i.'-'.$i;
  //If the $offset_i is false, then we are calling this function directly and these are main-level elements.
  //If it is not false, this function is being called from itself.
        ?>
    <div class="menu-item">
        <label for="text-<?php echo $id_string;?>"><?php echo $menu['id']['otitle'];?></label><input type="text" id="text-<?php echo $id_string?>" value="<?php print($menu['id']['utitle']);?>" /> 
<?php
  //The code above just outputs any information you want into a 'menu-item' div. you can change literally every thing here to your liking. This was fastest for me.

  /*
    The code below
      1. Checks if the $menu array as a key named 'children', and if that is true
      2. Checks that the count (or sizeof) the array is greater than 0. 
      3. If it does, call get_menu_list on the children array, supplying the $id_string we created up top so that we don't have overlapping 
         ID names in our HTML.
  */
    if(array_key_exists('children', $menu) && count($menu['children'])>0){
        get_menu_list($menu['children'],$id_string);
    }
    ?></div><?php
  //Ends the HTML div we started up top.
    endforeach;
  //Self explanatory.
}

$array_serial = 'a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:27:"products.php?la=electronics";s:6:"otitle";s:11:"Electronics";s:6:"utitle";s:11:"Electronics";s:4:"ttip";s:11:"Electronics";}s:8:"children";a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:51:"products.php?la=electronics&lb=Computer-Peripherals";s:6:"otitle";s:20:"Computer Peripherals";s:6:"utitle";s:20:"Computer Peripherals";s:4:"ttip";s:34:"Electronics : Computer Peripherals";}s:8:"children";a:1:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:63:"products.php?la=electronics&lb=Computer-Peripherals&lc=Printers";s:6:"otitle";s:8:"Printers";s:6:"utitle";s:8:"Printers";s:4:"ttip";s:45:"Electronics : Computer Peripherals : Printers";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:90:"products.php?la=electronics&lb=Computer-Peripherals&lc=Printers&ld=Single-Function-Printer";s:6:"otitle";s:23:"Single Function Printer";s:6:"utitle";s:23:"Single Function Printer";s:4:"ttip";s:71:"Electronics : Computer Peripherals : Printers : Single Function Printer";}}}}}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:38:"products.php?la=electronics&lb=Mobiles";s:6:"otitle";s:7:"Mobiles";s:6:"utitle";s:7:"Mobiles";s:4:"ttip";s:21:"Electronics : Mobiles";}}}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:31:"products.php?la=homenfurnitures";s:6:"otitle";s:19:"Home And Furnitures";s:6:"utitle";s:19:"Home And Furnitures";s:4:"ttip";s:19:"Home And Furnitures";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:9:"about.php";s:6:"otitle";s:8:"About Us";s:6:"utitle";s:8:"About Us";s:4:"ttip";s:8:"About US";}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:40:"products.php?la=homenfurnitures&lb=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:27:"Home And Furnitures : Other";}s:8:"children";a:1:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:51:"products.php?la=homenfurnitures&lb=Lights-AnD-Lamps";s:6:"otitle";s:14:"Lights & Lamps";s:6:"utitle";s:14:"Lights & Lamps";s:4:"ttip";s:36:"Home And Furnitures : Lights & Lamps";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:12:"contacts.php";s:6:"otitle";s:10:"Contact Us";s:6:"utitle";s:10:"Contact Us";s:4:"ttip";s:10:"Contact US";}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:58:"products.php?la=homenfurnitures&lb=Dinnerware-AnD-Crockery";s:6:"otitle";s:21:"Dinnerware & Crockery";s:6:"utitle";s:21:"Dinnerware & Crockery";s:4:"ttip";s:43:"Home And Furnitures : Dinnerware & Crockery";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:78:"products.php?la=homenfurnitures&lb=Dinnerware-AnD-Crockery&lc=Cups-AnD-Saucers";s:6:"otitle";s:14:"Cups & Saucers";s:6:"utitle";s:14:"Cups & Saucers";s:4:"ttip";s:60:"Home And Furnitures : Dinnerware & Crockery : Cups & Saucers";}}}}}}}}}}}';
$array_unserial = unserialize($array_serial);
get_menu_list($array_unserial);
?>

关于序列化

中数据的相关说明

尽可能避免使用序列化字符串。一个非常简单的解决方案是我已经在评论中写到的那个,但是通过创建一个只有几个字段的表categories,您可以获得相同的数据集,而无需使用数组序列化/反序列化。这绝对是这个特定问题的主题之外的问题,但如果我至少没有提到你,我觉得我没有帮助。

希望这有帮助。

答案 1 :(得分:0)

我没有使用Recursion方法,但因为我已经使用foreach loop编写了函数。以下是使用相同功能完成此任务的代码。

if (isset($_REQUEST['u_arr'])) {        
    $array_serial = serialize($_REQUEST['u_arr']);
}
else {

//Here is my code to view "utitle" in a input type
$array_serial = $array_serial; /*You can get $array_serial from the question of this post*/

}


function get_menu_list($selected_menus)
{
    if($selected_menus != null){
        foreach($selected_menus as $id1=>$menu_level_1)
        {
            $title = implode("///",$menu_level_1['id']);
            echo '
<li class="dd-item">
<div class="dd-handle">
<input type="hidden" name="u_arr['.$id1.'][id][url]" value="'.$menu_level_1['id']['url'].'" />
<input type="hidden" name="u_arr['.$id1.'][id][otitle]" value="'.$menu_level_1['id']['otitle'].'" />
<input type="text" name="u_arr['.$id1.'][id][utitle]" value="'.$menu_level_1['id']['utitle'].'" />
<input type="hidden" name="u_arr['.$id1.'][id][ttip]" value="'.$menu_level_1['id']['ttip'].'" />
</div>
';
            if(isset($menu_level_1['children']))
            {
                echo '<ol class="dd-list">';
                foreach($menu_level_1['children'] as $id2=>$menu_level_2)
                {
                    $title_level_2 = implode("///",$menu_level_2['id']);
                    echo '
<li class="dd-item">
<div class="dd-handle">
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][id][url]" value="'.$menu_level_2['id']['url'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][id][otitle]" value="'.$menu_level_2['id']['otitle'].'" />
<input type="text" name="u_arr['.$id1.'][children]['.$id2.'][id][utitle]" value="'.$menu_level_2['id']['utitle'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][id][ttip]" value="'.$menu_level_2['id']['ttip'].'" />
</div>
';
                    if(isset($menu_level_2['children']))
                    {
                        echo '<ol class="dd-list">';
                        foreach($menu_level_2['children'] as $id3=>$menu_level_3)
                        {
                            $title_level_3 = implode("///",$menu_level_3['id']);
                            echo '
<li class="dd-item">
<div class="dd-handle">
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][id][url]" value="'.$menu_level_3['id']['url'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][id][otitle]" value="'.$menu_level_3['id']['otitle'].'" />
<input type="text" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][id][utitle]" value="'.$menu_level_3['id']['utitle'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][id][ttip]" value="'.$menu_level_3['id']['ttip'].'" />
</div>
';
                            if(isset($menu_level_3['children']))
                            {
                                echo '<ol class="dd-list">';
                                foreach($menu_level_3['children'] as $id4=>$menu_level_4)
                                {
                                    $title_level_4 = implode("///",$menu_level_4['id']);
                                    echo '
<li class="dd-item">
<div class="dd-handle">
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][id][url]" value="'.$menu_level_4['id']['url'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][id][otitle]" value="'.$menu_level_4['id']['otitle'].'" />
<input type="text" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][id][utitle]" value="'.$menu_level_4['id']['utitle'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][id][ttip]" value="'.$menu_level_4['id']['ttip'].'" />
</div>
';
                                    if(isset($menu_level_4['children']))
                                    {
                                        echo '<ol class="dd-list">';
                                        foreach($menu_level_4['children'] as $id5=>$menu_level_5)
                                        {
                                            $title_level_5 = implode("///",$menu_level_5['id']);
                                            echo '
<li class="dd-item">
<div class="dd-handle">
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][children]['.$id5.'][id][url]" value="'.$menu_level_5['id']['url'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][children]['.$id5.'][id][otitle]" value="'.$menu_level_5['id']['otitle'].'" />
<input type="text" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][children]['.$id5.'][id][utitle]" value="'.$menu_level_5['id']['utitle'].'" />
<input type="hidden" name="u_arr['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][children]['.$id5.'][id][ttip]" value="'.$menu_level_5['id']['ttip'].'" />
</div>
</li>
';
                                        }
                                        echo "</ol>";
                                    }
                                    echo '</li>';
                                }
                                echo "</ol>";
                            }
                            echo '</li>';
                        }
                        echo "</ol>";
                    }
                    echo '</li>';
                }
                echo '</ol>';
            }
            echo '</li>';
        }
    }//end if
    else
        return null;    
}
$array_unserial = unserialize($array_serial);
?>
<form method="post" action="">
    <?php get_menu_list($array_unserial) ?>
    <button type="submit">Submit</button>
</form>