Friday, January 13, 2012

How do I build a complex array in PHP?


How can I build an array like this in PHP?



It should have unique keys each of whose corresponding values are arrays, and those arrays have multiple elements for each key.




Array (
42 => Array ( 56, 86, 97 )
51 => Array ( 64, 52 )
)

4 comments:

  1. $main = array();
    for ($i=0; $i<10; $i++) {
    // $i makes numeric keys
    $main[$i] = array();

    // Or instead, you could make a truly unique key name for each:
    $main[uniqid()] = array(...somevalues...);

    }


    Creates something like:

    Array
    (
    [4f105f361cdd1] => Array
    (
    )

    [4f105f361cf24] => Array
    (
    )

    [4f105f361cf33] => Array
    (
    )

    [4f105f361cf3c] => Array
    (
    )

    [4f105f361cf44] => Array
    (
    )

    )

    ReplyDelete
  2. $arr = array(
    42 => array(56, 86, 97),
    51 => array(64, 52)
    );

    var_dump($arr);

    ReplyDelete
  3. $array = array(42=>array(56,86,97),51=>array(64,52));

    ReplyDelete
  4. $arraytest = array(
    42 => array(56, 86, 97),
    51 => array(64, 52)
    );

    var_dump($arraytest);

    ReplyDelete