Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 - Fatal编程技术网

如何在php中为数组分配关联索引

如何在php中为数组分配关联索引,php,arrays,Php,Arrays,我想为演示值分配一个索引“父名”,但无法这样做,请帮助我 Array ( [first_name] => Abc [last_name] => Xyz [address] => xyz [email] => abcxyz@domain.com [contact_no] => 1234567890 [exam_id] => 4 [candidate_id] => abc.xyz [passwo

我想为演示值分配一个索引“父名”,但无法这样做,请帮助我

Array
(
    [first_name] => Abc
    [last_name] => Xyz
    [address] => xyz
    [email] => abcxyz@domain.com
    [contact_no] => 1234567890
    [exam_id] => 4
    [candidate_id] => abc.xyz
    [password] => seXUEz1n7cRRY
    [martial_status] => S
    [children] => 0
    [birth_date] => 1988-11-24
    [remark] => xyz
    [] => demo
)

这通常可以做到:


$array['fathername']='demo'

我猜您的索引
null
中包含demo值,并且
null
将被转换为空字符串,即null键实际上将存储在“”下

$array["Father Name"] = $array[''];
unset($array['']);
试试这个:

array('Father Name'=>'demo');

如果您需要查找并替换该项目,可以执行以下操作

$array = array('a' => 1, 'b' => 2, 'demo');
$index = array_search('demo', $array);

if ($index !== false) {
    $array['Father Name'] = $array[$index];
    unset($array[$index]);
}

var_dump($array);

原始数组中
demo
的索引是什么?
$array[null]
没有意义(因为这
null
作为索引会隐式转换为空字符串,
'
//不是我的dv
$array = array('a' => 1, 'b' => 2, 'demo');
$index = array_search('demo', $array);

if ($index !== false) {
    $array['Father Name'] = $array[$index];
    unset($array[$index]);
}

var_dump($array);