Php:修改多维数组值

时间:2017-01-17 20:16:43

标签: php multidimensional-array

<?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);
?>

所有索引&#34; otitle&#34;此数组的数据以输入类型文本的形式显示。

用户如何通过一次提交(或任何其他方式)修改和保存数组?

阵列是动态的,可以更大。

1 个答案:

答案 0 :(得分:0)

PhpFiddle

这应该可以帮助你完成大部分工作。这里的重要部分是在要为每个子项更新的数组中指定ID,以便在提交时重新创建结构。这可以通过otitle[<id1>][children][<id2>][children]...[<id5>][name]样式实现。

如果您在上面的小提琴中提交表单,PHP从表单生成的数组结构将显示在页面顶部。从那里,您应该能够迭代数组及其子节点并获取更新的值。

//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);
?>