Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中命名数组元素?_Php_Arrays_Codeigniter - Fatal编程技术网

如何在PHP中命名数组元素?

如何在PHP中命名数组元素?,php,arrays,codeigniter,Php,Arrays,Codeigniter,我从数据库中取出一条记录 因为我有一个数组 $new_val = explode(',',$param->arg_2); 当我把它扔掉时,我得到了这个: 0 => string 'Profile1' (length=8) 1 => string 'Profile2' (length=8) 2 => string 'Profile3' (length=8) 如何在var_dump中获取此信息: Profile1 => string 'Profile1' (leng

我从数据库中取出一条记录

因为我有一个数组

$new_val = explode(',',$param->arg_2);
当我把它扔掉时,我得到了这个:

0 => string 'Profile1' (length=8)
1 => string 'Profile2' (length=8)
2 => string 'Profile3' (length=8)
如何在var_dump中获取此信息:

Profile1 => string 'Profile1' (length=8)
Profile2 => string 'Profile2' (length=8)
Profile3 => string 'Profile3' (length=8)
试试这个

$new_array=array();
    foreach($new_val as $nv)
    {
    $new_array[$nv]=$nv;
    }
    var_dump($new_array);
试试这个

$new_array=array();
    foreach($new_val as $nv)
    {
    $new_array[$nv]=$nv;
    }
    var_dump($new_array);
在代码之后:

$new_val = explode(',',$param->arg_2);
加:

在代码之后:

$new_val = explode(',',$param->arg_2);
加:

试试这个:

结果是:

array(3) {
  ["Profile 1"]=>
  string(9) "Profile 1"
  ["Profile 2"]=>
  string(9) "Profile 2"
  ["Profile 3"]=>
  string(9) "Profile 3"
}
试试这个:

结果是:

array(3) {
  ["Profile 1"]=>
  string(9) "Profile 1"
  ["Profile 2"]=>
  string(9) "Profile 2"
  ["Profile 3"]=>
  string(9) "Profile 3"
}

array\u combine
-使用一个数组作为键,另一个数组作为值来创建数组

试试这个

$array = explode(',',$param->arg_2);
$names = array_combine($array, $array);
var_dump($names);

array\u combine
-使用一个数组作为键,另一个数组作为值来创建数组

试试这个

$array = explode(',',$param->arg_2);
$names = array_combine($array, $array);
var_dump($names);
应该输出

array(3) { ["Profile1"]=> string(8) "Profile1" ["Profile2"]=> string(8) "Profile2" ["Profile3"]=> string(8) "Profile3" }
应该输出

array(3) { ["Profile1"]=> string(8) "Profile1" ["Profile2"]=> string(8) "Profile2" ["Profile3"]=> string(8) "Profile3" }

在搜索thorught PHP指南的过程中,我发现了以下内容:

eval("\$new_val = array (".$param->arg_2.");");

在搜索thorught PHP指南的过程中,我发现了以下内容:

eval("\$new_val = array (".$param->arg_2.");");

这意味着您必须从逗号分隔的字符串生成数组?是@sanjaychaudharible@sanjaychaudharible但如果某些索引具有相同的值会发生什么情况?它们将被覆盖。所以这并不总是好的做法。@aslawin您是对的,但根据我的要求,我必须得到这一点,这意味着您必须从逗号分隔的字符串生成数组?是@Sanjaychaudharible但如果某些索引具有相同的值,会发生什么?它们将被覆盖。所以这并不总是好的做法。@aslawin你是对的,但根据我的要求我必须得到这个否,它给了我这个0=>字符串“Profile1”=>“Profile A”(长度=23)1=>字符串“Profile2”=>“Profile B”(长度=23)2=>字符串“Profile3”=>“Profile C”(长度=23)不,它给了我这个0=>字符串“Profile1”=>“Profile A”(长度=23)1=>字符串“Profile2”=>“Profile B”(长度=23)2=>字符串“Profile3”=>“Profile C”(长度=23)这是非常感谢的Mant这是非常感谢的人