Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Single Array to Multi Array Problem

Newbie
 
Join Date: Jun 2009
Posts: 3
#1: Jun 11 '09
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's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Jun 11 '09

re: PHP Single Array to Multi Array Problem


try
Expand|Select|Wrap|Line Numbers
  1. $arr1[0] = $arr2;
what does that give?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#3: Jun 11 '09

re: PHP Single Array to Multi Array Problem


If Dormilich's solution doesn't work, post the entire loop here. Maybe there is something wrong with the loop itself.
Newbie
 
Join Date: Jun 2009
Posts: 3
#4: Jun 11 '09

re: PHP Single Array to Multi Array Problem


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());
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#5: Jun 11 '09

re: PHP Single Array to Multi Array Problem


Quote:

Originally Posted by mcafeedan View Post

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.

Quote:

Originally Posted by mcafeedan View Post

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

then try
Expand|Select|Wrap|Line Numbers
  1. $arr1 = array($arr2);
Newbie
 
Join Date: Jun 2009
Posts: 3
#6: Jun 15 '09

re: PHP Single Array to Multi Array Problem


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....
Reply