【问题标题】:How To Assign Value On Each Element In Array?如何为数组中的每个元素赋值?
【发布时间】:2021-01-12 08:10:23
【问题描述】:

所以,基本上我有这个数组数据:

$data = ['one', 'two', 'three'];

现在,我想为该数组的每个元素分配一个值,如下所示:

$val = 'new';

// 'new' value is get from $val
$data = [
 'one' => 'new',
 'two' => 'new',
 'three' => 'new'
];

我是怎么做到的?谢谢。

【问题讨论】:

  • @AnuragSrivastava - 这更适合带有'one' => 'one',的数组

标签: php arrays


【解决方案1】:
$data = ['one', 'two', 'three'];
$val = 'new';
$newArray = array_fill_keys($data, $val);
print_r($newArray);

小提琴here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多