简体   繁体   中英

How to assign multiple values to single index in array in PHP

 $def_field = array( "def_value" => 'Display Name', "def_value" => 'Database Value' );

Here i have a array in that i need to assign both Display Name and Database Value to def_value key. how to do that can anyone please help me..

Your way is incorrect as the second index will override the first index value because they have the same name. So You will have only this in your array:

$def_field = array(
    "def_value" => 'Database Value'
);

You can do it these ways

$def_field = array(
  "def_value" => array('Display Name','Database Value')
);

Or like this:

$def_field = [];
$def_field['def_value'][] = 'Display Name';
$def_field['def_value'][] = 'Database Value';

The second exemple kind of explains everything. You will create an array with index $def_value inside array $def_field with values 'Display Name' having index 0 and 'Database Value' having index 1 .

You will probably need to add it as a nested array like this:

$def_field = array(
    "def_value" => array('Display Name','Database Value')
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM