Complex Arrays PHP


Arrays can get complicated pretty quickly. Here’s a quick lowdown on how they are made, and how they are accessed.

<!DOCTYPE html>
<html>
<body>

<?php
// Standard Array
$my_keys = [ 'key1', 'key2' ];
$my_colors_array = [ 'blue' => 'sky', 'green' => 'salad', 'yellow' => 'pee' ];

// Adding an associative array to each key.
foreach ( $my_keys as $my_key ) {
	$my_complex_array[$my_key] = array(
	    // You can use a var as the key and the value.
		'product_name'   => 'MyProduct',
		// This just sets up this key with an empty array.
		'product_colors' => array(),
		'product_sizes'  => array(),
	);
}

// Adding values to the array.
foreach ( $my_keys as $my_key ) {
    $my_complex_array[$my_key]['product_colors'][] = 'some new value prob use a var';
    $my_complex_array[$my_key]['product_colors'][] = 'some OTHER one';
    // You can concatinate into a value with .=
    $my_complex_array[$my_key]['product_colors'][1] .= ' - concatinated!';
    
    // You can add as many other arrays as you want as a value
    $my_complex_array[$my_key]['product_sizes'][] = $my_colors_array;
    // You can also declare the key 
    $my_complex_array[$my_key]['product_sizes']['rad_key'] = $my_colors_array;
    // Add to the child array
    $my_complex_array[$my_key]['product_sizes']['rad_key']['pink'] = 'heart';
    
}

print("<pre>".print_r($my_complex_array,true)."</pre>");
?>

</body>
</html>

The output

Array
(
    [key1] => Array
        (
            [product_name] => MyProduct
            [product_colors] => Array
                (
                    [0] => some new value prob use a var
                    [1] => some OTHER one - concatinated!
                )

            [product_sizes] => Array
                (
                    [0] => Array
                        (
                            [blue] => sky
                            [green] => salad
                            [yellow] => pee
                        )

                    [rad_key] => Array
                        (
                            [blue] => sky
                            [green] => salad
                            [yellow] => pee
                            [pink] => heart
                        )

                )

        )

    [key2] => Array
        (
            [product_name] => MyProduct
            [product_colors] => Array
                (
                    [0] => some new value prob use a var
                    [1] => some OTHER one - concatinated!
                )

            [product_sizes] => Array
                (
                    [0] => Array
                        (
                            [blue] => sky
                            [green] => salad
                            [yellow] => pee
                        )

                    [rad_key] => Array
                        (
                            [blue] => sky
                            [green] => salad
                            [yellow] => pee
                            [pink] => heart
                        )

                )

        )

)