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

时间:2015-12-02 19:42:06

标签: php arrays smartadmin

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

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

的网址
<?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
?>

这是完整的模板。我试图更换Smart 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" }
            }

2 个答案:

答案 0 :(得分:1)

您需要创建一个新阵列:

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

现在你应该拥有新数组$ var;

中的所有内容

答案 1 :(得分:1)

您想要将$ _SESSION [&#39;链接&#39;]中的导航添加到新模板中!

在模板中注释掉$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']
    );
}

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