首页 > 解决方案 > 如何在 HTML 文件中传递数组?

问题描述

我的 HTML 文件中有一个标签,我的 PHP 文件中有“myfunc”函数。如何将 href 中的数组传递给我的“myfunc”函数?

<a class="btn btn-danger" href="<?php echo myfunc("this is arg1 witch is string", ['id']); ?>">delete</a>

这是我的 PHP 函数:

function myfunc($str,$param=[])
{ 
    return true;
}

标签: phphtmlcodeigniter

解决方案


a中不能有数组,URL如果一定要encoded格式的话,我写了一个demo代码,看看能不能解决你的问题↓↓

查看

<?php 
    $id = array('1', '2');

    // convert the array to JSON objects and then convert the string(object) to query part of the URL.
    $id = urlencode(json_encode($id));
?>
<a class="btn btn-danger" href="<?php echo base_url("home/form/this is arg1 witch is string/{$id}"); ?>">delete</a>

控制器

function form($str, $param){

    echo urldecode($str); // convert the URL string back into its original form 
    echo '<br>';
    print_r(json_decode(urldecode($param)));  // Convert the URL string into encoded form and then convert it back to an array.
}

输出

this is arg1 which is string
Array ( [0] => 1 [1] => 2 )

阅读更多关于json_encodeurlencodeurldecodejson_decode


推荐阅读