Dan Sebastian
Dan Sebastian

Reputation: 539

Populate multidimensional array with data

While renown scientists are looking into other dimensions I'm trying to figure out how to populate multidimensional arrays with dynamic data.

I need to add an undetermined number of values that are calculated from a random function.

function my_random ($min, $max) {

        $random_var = rand($min, $max);
        return $random_var;
    }

I would like my array to look like this:

$array_example = [
                'id' => $id, 
                'value1' => $value1,
                'value2' => $value2

            ];

...or maybe like this:

$array[$i] = [
                'id' => $id, array(
                    'value1' => $value1,
                    'value2' => $value2
                )   
        ];

I figured a simple for-loop would do the trick and so I tried this:

for ($i = 1; $i <= $amount; $i++) {

    $array[$i] = $i;
    $array[$i] = [
            'id' => $i, array(
                'value1' => $value1,
                'value2' => $value2
            )
        ];
}

...but it comes out all wrong (from console):

string(103) "{"id":2,"value1":[14,{"1":{"id":1,"0":{"value1":[14],"value2":[11]}}},13],"value2":[11,19]}"

The for-loop seems to nest them. I tried different functions, hoping to get it right: range() to get the id and then populate it with data, array_push() and I even tried to combine and merge.

This thread makes it look simple:

$array[] = "item";
$array[$key] = "item";
array_push($array, "item", "another item");

But this solution will only work to create the index.

How do I insert those values into each index dynamically? What I ultimately need is to be able to access the array and its values like this:

$array[0]["value1"].

Thanks.

Upvotes: 2

Views: 3040

Answers (1)

Fred Gandt
Fred Gandt

Reputation: 4312

Indexed array of associative arrays

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

The following function takes one optional integer argument, or defaults to 10, and initiates a for loop to create an indexed Array of associative array()s.

<!DOCTYPE html><html><head></head><body>
<?php 
    function createArrayOf( $quantity = 10 ) {
        $arr = [];
        for ( $i = 0; $i < $quantity; ++$i ) {
            $arr[ $i ] = array( 
                "value_1" => "some value",
                "value_2" => "another value"
            );
        }
        return $arr;
    }
?>
<pre><?php var_export( createArrayOf( 5 ) ); ?></pre>
</body></html>

Output using var_export() which:

gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.

array (
    0 => 
    array (
        'value_1' => 'some value',
        'value_2' => 'another value',
    ),
    1 => 
    array (
        'value_1' => 'some value',
        'value_2' => 'another value',
    ),
    2 => 
    array (
        'value_1' => 'some value',
        'value_2' => 'another value',
    ),
    3 => 
    array (
        'value_1' => 'some value',
        'value_2' => 'another value',
    ),
    4 => 
    array (
        'value_1' => 'some value',
        'value_2' => 'another value',
    ),
)

Upvotes: 2

Related Questions