2

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

1
  • 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 Commented Jul 7, 2014 at 7:18

3 Answers 3

4

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

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

Demo

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

Comments

1

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; });

Comments

1

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();
?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.