Create an arrayed array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phub11
    New Member
    • Feb 2008
    • 127

    Create an arrayed array

    Hi all,

    I have two pre-defined arrays;

    array1 = car, 1, tony;
    array2 = bus, 3, mike;

    I would like to make these into an arrayed array such that arrayFinal[0][2]=tony.

    I've looked at array_merge and array_combine, but they don't appear to do what I want. All the examples I can only find multidimensiona l array examples that use scripted input rather than using pre-existing arrays. Any ideas ?

    Thanks in advance!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Not too sure what your question is, can you clarify a little more?

    Code:
    $array1 = array(1, 2, 3);
    $array2 = array(4, 5, 6);
    $array_final = array($array1, $array2);
    print_r($array_final);

    Comment

    • phub11
      New Member
      • Feb 2008
      • 127

      #3
      Thanks for the reply!

      To clarify - I have a loop, and for each loop a new array get appended to the arrayed array; much like array_push, but instead of appending a new element to an array, appending a new array to the multidimensiona l array.

      while ( $selCounter < $nSelections )
      {
      $arrayedArr = ?SOMETHING? ${'finalArr' . ($selCounter)}
      $selCounter = $selCounter + 1;
      }

      Thanks!

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        A two-dimensional array is actually an array of arrays in php.
        Works slightly different in other languages such as C.
        You will found that the array functions are virtualy useless with 2d arrays.

        You have to write your own procedures using loops.

        Now to your problem.
        My understanding is you want to collect arrays into an array.
        That is an array of arrays (see line one above).

        So you have
        Code:
        $array1 = array('car', 1, 'tony');
        $array2 = array('bus', 3, 'mike');
        To collect these into a 2d.
        Code:
        $arrayofarrays = array(); #Good practice to define arrays
        //Load arrays into 2d
        $arrayofarrays[] = $array1 ;
        $arrayofarrays[] = $array2 ;
        So 2d array will have format
        Code:
        arrayofarrays(0=>array(0=>'car',1=>1...),1=>array(0=>'bus', 1=>3,2=> 'mike'))

        Comment

        • phub11
          New Member
          • Feb 2008
          • 127

          #5
          Thanks!

          Works now; I knew it was something simple.

          Comment

          Working...