PHP创建键和值相同的数组

时间:2021-04-03 21:36:47

I am using the range() function to create an array. However, I want the keys to be the same as the value. This is ok when i do range(0, 10) as the index starts from 0, however if i do range(1, 11), the index will still start from 0, so it ends up 0=>1 when i want it to be 1=>1

我使用range()函数创建一个数组。但是,我希望键与值相同。当我做区间(0,10)时,这是可以的,当索引从0开始时,但是如果我做区间(1,11),索引仍然从0开始,所以当我想让它为1=>1时,它最后还是0=>1

How can I use range() to create an array where the key is the same as the value?

如何使用range()创建键与值相同的数组?

6 个解决方案

#1


123  

How about array_combine?

合二为一呢?

$b = array_combine(range(1,10), range(1,10));

#2


5  

Or you did it this way:

或者你是这样做的:

$b = array_slice(range(0,10), 1, NULL, TRUE);

Find the output here: http://codepad.org/gx9QH7ES

在这里找到输出:http://codepad.org/gx9QH7ES

#3


1  

There is no out of the box solution for this. You will have to create the array yourself, like so:

这个问题没有现成的解决方案。您必须自己创建数组,如下所示:

$temp = array();
foreach(range(1, 11) as $n) {
   $temp[$n] = $n;
}

But, more importantly, why do you need this? You can just use the value itself?

但是,更重要的是,为什么你需要这个?你可以只使用值本身?

#4


0  

<?php
function createArray($start, $end){
  $arr = array();
  foreach(range($start, $end) as $number){
    $arr[$number] = $number;
  }
  return $arr;
}

print_r(createArray(1, 10));
?>

See output here: http://codepad.org/Z4lFSyMy

看到输出:http://codepad.org/Z4lFSyMy

#5


0  

<?php

$array = array();
foreach (range(1,11) as $r)
  $array[$r] = $r;

print_r($array);

?>

#6


0  

Create a function to make this:

创建一个函数来实现以下目标:

if (! function_exists('sequence_equal'))
{
    function sequence_equal($low, $hight, $step = 1)
    {
        return array_combine($range = range($low, $hight, $step), $range);
    }
}

Using:

使用:

print_r(sequence_equal(1, 10, 2));

Output:

输出:

array (
  1 => 1,
  3 => 3,
  5 => 5,
  7 => 7,
  9 => 9,
)

In PHP 5.5 >= you can use Generator to make this:

在PHP 5.5 >=中,您可以使用生成器来实现:

function sequence_equal($low, $hight, $step = 1)
{
    for ($i = $low; $i < $hight; $i += $step) {

        yield $i => $i;
    }
}

#1


123  

How about array_combine?

合二为一呢?

$b = array_combine(range(1,10), range(1,10));

#2


5  

Or you did it this way:

或者你是这样做的:

$b = array_slice(range(0,10), 1, NULL, TRUE);

Find the output here: http://codepad.org/gx9QH7ES

在这里找到输出:http://codepad.org/gx9QH7ES

#3


1  

There is no out of the box solution for this. You will have to create the array yourself, like so:

这个问题没有现成的解决方案。您必须自己创建数组,如下所示:

$temp = array();
foreach(range(1, 11) as $n) {
   $temp[$n] = $n;
}

But, more importantly, why do you need this? You can just use the value itself?

但是,更重要的是,为什么你需要这个?你可以只使用值本身?

#4


0  

<?php
function createArray($start, $end){
  $arr = array();
  foreach(range($start, $end) as $number){
    $arr[$number] = $number;
  }
  return $arr;
}

print_r(createArray(1, 10));
?>

See output here: http://codepad.org/Z4lFSyMy

看到输出:http://codepad.org/Z4lFSyMy

#5


0  

<?php

$array = array();
foreach (range(1,11) as $r)
  $array[$r] = $r;

print_r($array);

?>

#6


0  

Create a function to make this:

创建一个函数来实现以下目标:

if (! function_exists('sequence_equal'))
{
    function sequence_equal($low, $hight, $step = 1)
    {
        return array_combine($range = range($low, $hight, $step), $range);
    }
}

Using:

使用:

print_r(sequence_equal(1, 10, 2));

Output:

输出:

array (
  1 => 1,
  3 => 3,
  5 => 5,
  7 => 7,
  9 => 9,
)

In PHP 5.5 >= you can use Generator to make this:

在PHP 5.5 >=中,您可以使用生成器来实现:

function sequence_equal($low, $hight, $step = 1)
{
    for ($i = $low; $i < $hight; $i += $step) {

        yield $i => $i;
    }
}