Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to create an array like below

array(2) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(0)
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(0)
  }
}

Here first element of the inner array will be incremental and second element will always be 0. The outer array length should be 30. I spent a lot of of time on it but couldn't solve it by my one. Can any one of you help me ? Thanks

share|improve this question
    
no not an assignment.I am not a college student. I am reading php functions and tried to achieve this through array_fill function. Just of curiosity @kevinabelita –  ehp Jul 7 at 7:18
add comment

3 Answers

up vote 3 down vote accepted

You could do it using array_map() and range():

$o = array_map(function($a) { return array($a, 0); }, range(1, 30));

Demo

share|improve this answer
add comment

The array_fill() function creates an array where all elements are identical. You're asking for an array where the elements aren't all identical, so it's not something you can create simply by using array_fill()....

$array = array_fill(0, 2, array_fill(0, 2, 0)); 
array_walk($array, function(&$value, $key) { $value[0] = $key+1; });
share|improve this answer
add comment

Maybe you want something like this?

<?php
function initArray() {
    $array = array();
    for ($i = 1; $i <= 30; $i++) {
        $array[] = array($i, 0);
    }

    return $array;
}

// now call the initArray() function somewhere you need it
$myFancyArray = initArray();
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.