douhuibo5635 2017-01-17 20:16
浏览 106
已采纳

Php:修改多维数组值

<?php

//Here is my array in serialized form:

$array_serial = 'a:4:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:19:"products.php?la=men";s:6:"otitle";s:3:"Men";s:6:"utitle";s:3:"Men";s:4:"ttip";s:3:"Men";}s:8:"children";a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:35:"products.php?la=men&lb=All-Clothing";s:6:"otitle";s:12:"All Clothing";s:6:"utitle";s:12:"All Clothing";s:4:"ttip";s:18:"Men : All Clothing";}s:8:"children";a:5:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:63:"products.php?la=men&lb=All-Clothing&lc=Winter-AnD-Seasonal-Wear";s:6:"otitle";s:22:"Winter & Seasonal Wear";s:6:"utitle";s:22:"Winter & Seasonal Wear";s:4:"ttip";s:43:"Men : All Clothing : Winter & Seasonal Wear";}s:8:"children";a:3:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:74:"products.php?la=men&lb=All-Clothing&lc=Winter-AnD-Seasonal-Wear&ld=Jackets";s:6:"otitle";s:7:"Jackets";s:6:"utitle";s:7:"Jackets";s:4:"ttip";s:53:"Men : All Clothing : Winter & Seasonal Wear : Jackets";}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:80:"products.php?la=men&lb=All-Clothing&lc=Winter-AnD-Seasonal-Wear&ld=Thermal-Pants";s:6:"otitle";s:13:"Thermal Pants";s:6:"utitle";s:13:"Thermal Pants";s:4:"ttip";s:59:"Men : All Clothing : Winter & Seasonal Wear : Thermal Pants";}}i:2;a:2:{s:2:"id";a:4:{s:3:"url";s:84:"products.php?la=men&lb=All-Clothing&lc=Winter-AnD-Seasonal-Wear&ld=Suits-AnD-Blazers";s:6:"otitle";s:15:"Suits & Blazers";s:6:"utitle";s:15:"Suits & Blazers";s:4:"ttip";s:61:"Men : All Clothing : Winter & Seasonal Wear : Suits & Blazers";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:95:"products.php?la=men&lb=All-Clothing&lc=Winter-AnD-Seasonal-Wear&ld=Suits-AnD-Blazers&le=Blazers";s:6:"otitle";s:7:"Blazers";s:6:"utitle";s:7:"Blazers";s:4:"ttip";s:71:"Men : All Clothing : Winter & Seasonal Wear : Suits & Blazers : Blazers";}}}}}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:44:"products.php?la=men&lb=All-Clothing&lc=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:26:"Men : All Clothing : Other";}}i:2;a:2:{s:2:"id";a:4:{s:3:"url";s:45:"products.php?la=men&lb=All-Clothing&lc=Shirts";s:6:"otitle";s:6:"Shirts";s:6:"utitle";s:6:"Shirts";s:4:"ttip";s:27:"Men : All Clothing : Shirts";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:62:"products.php?la=men&lb=All-Clothing&lc=Shirts&ld=Formal-Shirts";s:6:"otitle";s:13:"Formal Shirts";s:6:"utitle";s:13:"Formal Shirts";s:4:"ttip";s:43:"Men : All Clothing : Shirts : Formal Shirts";}}}}i:3;a:2:{s:2:"id";a:4:{s:3:"url";s:50:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear";s:6:"otitle";s:11:"Sports Wear";s:6:"utitle";s:11:"Sports Wear";s:4:"ttip";s:32:"Men : All Clothing : Sports Wear";}s:8:"children";a:4:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:65:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=Track-Pants";s:6:"otitle";s:11:"Track Pants";s:6:"utitle";s:11:"Track Pants";s:4:"ttip";s:46:"Men : All Clothing : Sports Wear : Track Pants";}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:60:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=Shorts";s:6:"otitle";s:6:"Shorts";s:6:"utitle";s:6:"Shorts";s:4:"ttip";s:41:"Men : All Clothing : Sports Wear : Shorts";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:70:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=Shorts&le=Sports";s:6:"otitle";s:6:"Sports";s:6:"utitle";s:6:"Sports";s:4:"ttip";s:50:"Men : All Clothing : Sports Wear : Shorts : Sports";}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:69:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=Shorts&le=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:49:"Men : All Clothing : Sports Wear : Shorts : Other";}}}}i:2;a:1:{s:2:"id";a:4:{s:3:"url";s:65:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=TDaShShirts";s:6:"otitle";s:8:"T-Shirts";s:6:"utitle";s:8:"T-Shirts";s:4:"ttip";s:43:"Men : All Clothing : Sports Wear : T-Shirts";}}i:3;a:1:{s:2:"id";a:4:{s:3:"url";s:59:"products.php?la=men&lb=All-Clothing&lc=Sports-Wear&ld=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:40:"Men : All Clothing : Sports Wear : Other";}}}}i:4;a:1:{s:2:"id";a:4:{s:3:"url";s:44:"products.php?la=men&lb=All-Clothing&lc=Jeans";s:6:"otitle";s:5:"Jeans";s:6:"utitle";s:5:"Jeans";s:4:"ttip";s:26:"Men : All Clothing : Jeans";}}}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:31:"products.php?la=men&lb=Footwear";s:6:"otitle";s:8:"Footwear";s:6:"utitle";s:8:"Footwear";s:4:"ttip";s:14:"Men : Footwear";}s:8:"children";a:4:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:47:"products.php?la=men&lb=Footwear&lc=Casual-Shoes";s:6:"otitle";s:12:"Casual Shoes";s:6:"utitle";s:12:"Casual Shoes";s:4:"ttip";s:29:"Men : Footwear : Casual Shoes";}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:41:"products.php?la=men&lb=Footwear&lc=Sports";s:6:"otitle";s:6:"Sports";s:6:"utitle";s:6:"Sports";s:4:"ttip";s:23:"Men : Footwear : Sports";}}i:2;a:1:{s:2:"id";a:4:{s:3:"url";s:55:"products.php?la=men&lb=Footwear&lc=Sandals-and-Floaters";s:6:"otitle";s:20:"Sandals and Floaters";s:6:"utitle";s:20:"Sandals and Floaters";s:4:"ttip";s:37:"Men : Footwear : Sandals and Floaters";}}i:3;a:1:{s:2:"id";a:4:{s:3:"url";s:56:"products.php?la=men&lb=Footwear&lc=Slippers-and-FlipFlop";s:6:"otitle";s:21:"Slippers and FlipFlop";s:6:"utitle";s:21:"Slippers and FlipFlop";s:4:"ttip";s:38:"Men : Footwear : Slippers and FlipFlop";}}}}}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:21:"products.php?la=women";s:6:"otitle";s:5:"Women";s:6:"utitle";s:5:"Women";s:4:"ttip";s:5:"Women";}s:8:"children";a:1:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:36:"products.php?la=women&lb=Ethnic-Wear";s:6:"otitle";s:11:"Ethnic Wear";s:6:"utitle";s:11:"Ethnic Wear";s:4:"ttip";s:19:"Women : Ethnic Wear";}s:8:"children";a:3:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:58:"products.php?la=women&lb=Ethnic-Wear&lc=Kurtas-AnD-Kurties";s:6:"otitle";s:16:"Kurtas & Kurties";s:6:"utitle";s:16:"Kurtas & Kurties";s:4:"ttip";s:38:"Women : Ethnic Wear : Kurtas & Kurties";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:69:"products.php?la=women&lb=Ethnic-Wear&lc=Kurtas-AnD-Kurties&ld=Kurties";s:6:"otitle";s:7:"Kurties";s:6:"utitle";s:7:"Kurties";s:4:"ttip";s:48:"Women : Ethnic Wear : Kurtas & Kurties : Kurties";}}}}i:1;a:2:{s:2:"id";a:4:{s:3:"url";s:54:"products.php?la=women&lb=Ethnic-Wear&lc=Ethnic-Bottoms";s:6:"otitle";s:14:"Ethnic Bottoms";s:6:"utitle";s:14:"Ethnic Bottoms";s:4:"ttip";s:36:"Women : Ethnic Wear : Ethnic Bottoms";}s:8:"children";a:2:{i:0;a:2:{s:2:"id";a:4:{s:3:"url";s:78:"products.php?la=women&lb=Ethnic-Wear&lc=Ethnic-Bottoms&ld=Patialas-AnD-Salwars";s:6:"otitle";s:18:"Patialas & Salwars";s:6:"utitle";s:18:"Patialas & Salwars";s:4:"ttip";s:57:"Women : Ethnic Wear : Ethnic Bottoms : Patialas & Salwars";}s:8:"children";a:2:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:90:"products.php?la=women&lb=Ethnic-Wear&lc=Ethnic-Bottoms&ld=Patialas-AnD-Salwars&le=Patialas";s:6:"otitle";s:8:"Patialas";s:6:"utitle";s:8:"Patialas";s:4:"ttip";s:68:"Women : Ethnic Wear : Ethnic Bottoms : Patialas & Salwars : Patialas";}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:89:"products.php?la=women&lb=Ethnic-Wear&lc=Ethnic-Bottoms&ld=Patialas-AnD-Salwars&le=Salwars";s:6:"otitle";s:7:"Salwars";s:6:"utitle";s:7:"Salwars";s:4:"ttip";s:67:"Women : Ethnic Wear : Ethnic Bottoms : Patialas & Salwars : Salwars";}}}}i:1;a:1:{s:2:"id";a:4:{s:3:"url";s:69:"products.php?la=women&lb=Ethnic-Wear&lc=Ethnic-Bottoms&ld=Harem-Pants";s:6:"otitle";s:11:"Harem Pants";s:6:"utitle";s:11:"Harem Pants";s:4:"ttip";s:50:"Women : Ethnic Wear : Ethnic Bottoms : Harem Pants";}}}}i:2;a:1:{s:2:"id";a:4:{s:3:"url";s:45:"products.php?la=women&lb=Ethnic-Wear&lc=Other";s:6:"otitle";s:5:"Other";s:6:"utitle";s:5:"Other";s:4:"ttip";s:27:"Women : Ethnic Wear : Other";}}}}}}i:2;a:2:{s:2:"id";a:4:{s:3:"url";s:23:"products.php?la=eyewear";s:6:"otitle";s:7:"Eyewear";s:6:"utitle";s:7:"Eyewear";s:4:"ttip";s:7:"Eyewear";}s:8:"children";a:1:{i:0;a:1:{s:2:"id";a:4:{s:3:"url";s:42:"products.php?la=eyewear&lb=Spectacle-Frame";s:6:"otitle";s:15:"Spectacle Frame";s:6:"utitle";s:15:"Spectacle Frame";s:4:"ttip";s:25:"Eyewear : Spectacle Frame";}}}}i:3;a:1:{s:2:"id";a:4:{s:3:"url";s:23:"products.php?la=watches";s:6:"otitle";s:7:"Watches";s:6:"utitle";s:7:"Watches";s:4:"ttip";s:7:"Watches";}}}';




//Here is my 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);
get_menu_list($array_unserial);
?>

All index "otitle" of this array is being displayed in a form in an input type text.

How can the user modify and save the array with just one submit (or any other way) ??

And the array is dynamic, and can be much bigger.

  • 写回答

1条回答 默认 最新

  • duanjiwang2927 2017-01-17 20:55
    关注

    PhpFiddle.

    This should get you most of the way there. The important part here is to specify the ID in the array you want to update for each child item, so that the structure is recreated on submit. This can be achieved through a otitle[<id1>][children][<id2>][children]...[<id5>][name] style.

    If you submit the form in the fiddle above, the array structure generated by PHP from the form is displayed at the top of the page. From that, you should be able to iterate the array and its children and grab the updated values.

    //Here is my code to view "otitle" in a input type
    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="text" name="otitle['.$id1.'][name]" value="'.$menu_level_1['id']['otitle'].'" />
                    </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="text" name="otitle['.$id1.'][children]['.$id2.'][name]" value="'.$menu_level_2['id']['otitle'].'" />
                            </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="text" name="otitle['.$id1.'][children]['.$id2.'][children]['.$id3.'][name]" value="'.$menu_level_3['id']['otitle'].'" />
                                    </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="text" name="otitle['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][name]" value="'.$menu_level_4['id']['otitle'].'" />
                                            </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="text" name="otitle['.$id1.'][children]['.$id2.'][children]['.$id3.'][children]['.$id4.'][children]['.$id5.'][name]" 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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)