Search code examples
phparraysmultidimensional-arrayarray-push

Assigning indexes to subarrays


I need help figuring out how to assign an index to subarrays. Here is what I have so far:

I start off by setting up the array with the first subarray and some data.

$start=array(array($rand, $_POST['day'], $_POST['time'], $delay));

Then when I need to add another subarray, I use the array_push method to add subarrays to the existing array.

$array = get_option('extend-array');
$push = array($rand, $_POST['day'], $_POST['time'], $delay);
array_push($array, $push);

I'm not sure where to go from here. I want to have a custom index assigned to the subarrays so I can reference them directly if needed.


Solution

  • Just assign to the array elements:

    $array = array(array($rand,$_POST['day'],$_POST['time'],$delay));
    $array[1] = array("New data");
    $array[2] = "Blah";
    $array[9001] = "Doesn't have to be consecutive.";