1

Basically I am trying to create the following array.

$foo = array(
    1 => array(
            'id' => '1',
            'value'  => '1',
    ),
     2 => array(
            'id' => '2',
            'value'  => '2',
     )
 );

I am currently doing this like so

for($i = $rangeMin; $i <= $rangeMax; $i++) {
    array_push($foo, array('id'=> $i, 'value' => $i));
}

Although this works, is there any way I can improve upon this code? or is there any pre-built functionality in PHP to already do this?

4
  • Well i don't see much sense in this code, but what would you want to improve? Note that your 'id' part of the array is usually useless because you can just get the key with foreach($array as $key => $value) { $id = $key + $rangeMin; } or something. If you want better advice give a specific scenario. Commented Jan 28, 2014 at 14:08
  • that's about it. you can play code golf and reduce things a little bit, but that's about as good as you can make it. of course, if all 3 values in each sub-array are always the same, why bother producing the subarray at all? $foo = range($rangeMin, $rangeMax) would give you the same values in much less space. Commented Jan 28, 2014 at 14:08
  • first index will be == 0 ,not 1 Commented Jan 28, 2014 at 14:09
  • why would you ever do that? Commented Jan 28, 2014 at 14:13

9 Answers 9

3

If you love one-liners, you may go for:

$foo = array_map("array_fill_keys", array_fill(0, $rangeMax-$rangeMin+1, array("id", "value")), range($rangeMin, $rangeMax));

Apart from this, I can’t think of any pre-built functionality in PHP that makes your task easier.

Sign up to request clarification or add additional context in comments.

Comments

1

may be like this.. it is two line code

while($i<10)
    $arr[++$i]=array('id'=>++$k,'value'=>++$j);

Comments

0

You can try with:

for ($i = $rangeMin; $i <= $rangeMax; $i++) {
    $foo[] = array('id'=> $i, 'value' => $i);
}

We are replacing array_push with $foo[] which can be much clear.

Comments

0

Im not quite sure why you push them. Every new entry should be at the end of the Array. And they iterate with simple integers. So why not simply adding them with $foo[] = array('id' => 1, 'value' => 1);

Comments

0
$foo = array();
for($i = $rangeMin; $i <= $rangeMax; $i++) {
    $foo[] = array('id'=> $i, 'value' => $i);
}

Or if your key will always be the index of the pair in the array just do this:

$foo = array();
for($i = $rangeMin; $i <= $rangeMax; $i++) {
    $foo[$i] = $i;
}

Comments

0

Using

$foo[] = array('id'=> $i, 'value' => $i);

Inside the for loop will cause no overhead of calling a function, thus, will be more efficient.

Comments

0

Well, if you think that the key number of the array is important, maybe you can write like this:

$foo = array();

for($i = $min; $i <= $max; $i++)
{
    $foo[$i] = array(
        'id'    => $i,
        'value' => $i
    );
}

Comments

0

If you don't want to use function, try with

for($i = $rangeMin; $i <= $rangeMax; $i++) {
    $foo[] = array('id'=> $i, 'value' => $i);
}

Edit:

or if you want to use $i as key

$foo[$i] = array('id'=> $i, 'value' => $i);

Comments

0

You can do also :

for ($foo=$rangeMin; $foo < $rangeMax; $foo++) {
   $bar[] = array("id"=>$foo,"value"=>$foo);
}

If you are using an array of value to built another array more specefic you can try this:

unset($foo);
if (count($array_of_values) > 0) {
   foreach ($array_of_values as $key=>$bar) {
      $foo[$bar["id"]] = $bar["name"];
   }
}

This way, you can assign associative indexes to value and even exclude duplicates of a same index. The foeach will help you pass by EVERY single index of the array. Have a good day Mary!!

1 Comment

I still don't get why you want to use the same value for the every indexes and values.... My brain has rebooted. :-S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.