PHP Single Array to Multi Array Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcafeedan
    New Member
    • Jun 2009
    • 3

    #1

    PHP Single Array to Multi Array Problem

    I have exploded a file line into a single dimensioned array. I want to take the contents of the single deminsioned array and separate it out into a multi-dimensioned array.

    In the for loop I have code like this:

    $arr1[0][$j] = $arr2[$j];

    where the j index is changing. The result is only the first character of the arr2 is being deposited into arr1 for each element of the array. arr2 is still intact with complete field values.

    What is causing this and is there something I should be doing (such as a cast...tried casting arr2 with (string) and that didn't work) to insinuate a complete field transfer?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try
    Code:
    $arr1[0] = $arr2;
    what does that give?

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      If Dormilich's solution doesn't work, post the entire loop here. Maybe there is something wrong with the loop itself.

      Comment

      • mcafeedan
        New Member
        • Jun 2009
        • 3

        #4
        Not what I need...

        I need to extract the field to the second part of the multi-field array (arr1[0][$j]).

        Further info:

        I tried putting the information into a variable and then loading the array from the variable and the destination array (arr1) still is truncating all but the first character.

        $holdingCell = $arr2[$j];
        $arr1[0][$j] = $holdingCell;

        echo $holdingCell;

        what print_r shows is the first character of each field....

        Is there something within PHP that needs the second array of a multi-array dimensioned?

        I set up $arr1 = array(array());

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by mcafeedan
          I need to extract the field to the second part of the multi-field array (arr1[0][$j]).
          that's what the code is supposed to do. I just didn't bother to copy the elements one by one when I can pass over the whole array.

          Originally posted by mcafeedan
          I set up $arr1 = array(array());
          then try
          Code:
          $arr1 = array($arr2);

          Comment

          • mcafeedan
            New Member
            • Jun 2009
            • 3

            #6
            Solution figured out...

            Well, I was able to figure out the solution by playing around with the indexes and coming to the conclusion that the second index was not necessary. As soon as I dropped that index PHP moved everything as desired. Solution looked like this:

            arr1[0] = arr2[j];

            Suggesting that PHP add additional items to a multi-dimensioned array the same way it does in single dimensioned arrays where not specifying an index means to add to the array list...in this case the undefined index.

            Thanks for your input....

            Comment

            Working...