将数组转换为新数组以满足模板

Converting an array to a new array to meet template

我有一个旧文件。我想把它整合到我买的新模板中。

我有一个链接列表。我将它们存储在 Session 数组中。 该数组有一个标题和一个 url 每个


<?php session_start();
//in my old file i write this

foreach ($_SESSION[links] as $value){
?>
<a href="<?php print $value[linkurl]?>"><?php print $value[linktitle]?></a ><?php
}

//  array style i need to match in the new file

//  "name" => array(
//      "title" => "Display Title",
//      "url" => "http://yoururl.com"
//      )

// what i tried

//  foreach ($_SESSION[links] as $value){
//      "$value[linktitle]" => array(
//          "title" => "$value[linktitle]",
//          "url" => "$value[linkurl]"
//      )
//  }

//obviously doesn't work
?>

这是完整的模板。我正在尝试更换智能 UI 子(旋转木马、标签...)

<?php

//CONFIGURATION for SmartAdmin UI

//ribbon breadcrumbs config
//array("Display Name" => "URL");
$breadcrumbs = array(
    "Home" => APP_URL
);

/*navigation array config

ex:
"dashboard" => array(
    "title" => "Display Title",
    "url" => "http://yoururl.com",
    "url_target" => "_self",
    "icon" => "fa-home",
    "label_htm" => "<span>Add your custom label/badge html here</span>",
    "sub" => array() //contains array of sub items with the same format as the parent
)

*/
$page_nav = array(
    "dashboard" => array(
        "title" => "Dashboard",
        "url" => APP_URL,
    ),
    "smartui" => array(
        "title" => "Smart UI",
        "icon" => "fa-code",
        "sub" => array(

            "carousel" => array(
                "title" => "Carousel",
                "url" => APP_URL.'/smartui-carousel.php'
            ),
            "tab" => array(
                "title" => "Tab",
                "url" => APP_URL.'/smartui-tab.php'
            ),
            "accordion" => array(
                "title" => "Accordion",
                "url" => APP_URL.'/smartui-accordion.php'
            ),
            "widget" => array(
                'title' => "Widget",
                'url' => APP_URL."/smartui-widget.php"
            ),
            "datatable" => array(
                "title" => "DataTable",
                "url" => APP_URL."/smartui-datatable.php"
            ),
            "button" => array(
                "title" => "Button",
                "url" => APP_URL."/smartui-button.php"
            ),
            'smartform' => array(
                'title' => 'Smart Form',
                'url' => APP_URL.'/smartui-form.php'
            )
        )
    )

);

//configuration variables
$page_title = "";
$page_css = array();
$no_main_header = false; //set true for lock.php and login.php
$page_body_prop = array(); //optional properties for <body>
$page_html_prop = array(); //optional properties for <html>
?>

var_dump:

array(6) { 
    [0]=> array(4) { ["linktitle"]=> string(13) "Claims Center" ["linkurl"]=> string(79) "Claims.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" } 
    [1]=> array(4) { ["linktitle"]=> string(16) "Expense Allocate" ["linkurl"]=> string(81) "Expense.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" } 
    [2]=> array(4) { ["linktitle"]=> string(13) "Freight Rater" ["linkurl"]=> string(105) "User.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" } 
    [3]=> array(4) { ["linktitle"]=> string(14) "Invoice Center" ["linkurl"]=> string(71) "Online.php" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(11) "Application" } 
    [4]=> array(4) { ["linktitle"]=> string(4) "KPIs" ["linkurl"]=> string(4) "KPIs" ["Sectionlinktitle"]=> string(10) "Dashboards" ["linkdes"]=> string(9) "Dashboard" } 
    [5]=> array(4) { ["linktitle"]=> string(16) "Multi-Mode Rater" ["linkurl"]=> string(0) "M" ["Sectionlinktitle"]=> string(12) "Applications" ["linkdes"]=> string(16) "Multi-Mode Rater" }
            }

您需要创建一个新数组:

$page_nav = array();
foreach ($_SESSION[links] as $value) {
    $page_nav[][$value['linktitle']] = array(
        'title' => $value['linktitle'],
        'url'   => $value['linkurl']
    )
}

现在您应该在新数组 $var 中拥有所有内容;

您想将$_SESSION['links']中的导航添加到新模板中!

注释掉模板中的 $page_nav 变量 (/* $page_nav = array( ... ); */)。这将生成要插入模板的代码(剪切和粘贴):

$a = $_SESSION['links'];
echo '<pre>$page_nav = array(' . "\n";
foreach( $a as $k=>$v ) {
    echo "\t'" . strtolower(str_replace(' ','',$v['linktitle'])) . "' => array(\n"
        . "\t\t'title' => '" . $v['linktitle'] . "',\n"
        . "\t\t'url' => '" . $v['linkurl'] . "'\n"
        . "\t),\n";
}
echo ');</pre>';

如果您想将代码保留在 $_SESSION 变量中(确实不是一个好主意),请将这段代码放在它的位置:

$page_nav = array();
foreach ( $_SESSION['links'] as $k=>$v ) {
    $page_nav[strtolower(str_replace(' ','',$v['linktitle']))] = array(
        'title' => $v['linktitle'],
        'url'   => $v['linkurl']
    );
}

我认为这应该可行!我没有考虑任何子菜单,因为 none 在你的 var_dump!