Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Array collection of files

I'm trying to make an array of files which will hold a list of files, per remittance, so in my example I have 2 remitances both of 2 pages, so in simple terms Im trying to set my array up as:-
	$remitanceCollection = array();

	array_push($remitanceCollection, $remitanceCollection[0]["files"],"Page0");
	array_push($remitanceCollection, $remitanceCollection[0]["files"],"Page1");

	array_push($remitanceCollection, $remitanceCollection[1]["files"],"Page2");
	array_push($remitanceCollection, $remitanceCollection[1]["files"],"Page3");

Open in new window


The code runs a lot more lines to get this information, so I cant use, but will get what I want.
	$remitanceCollection = array("0"=>array("Files"=>array("Page1","Page2")),"1"=>array("Files"=>array("Page3","Page4")));

Open in new window


So, my issue is 3 part:-
1 - I dont understand why I get the error - E_NOTICE : type 8 -- Undefined offset: 0 -- at line 4

2 - I dont understand the Warnings - E_WARNING : type 2 -- Illegal string offset 'files' -- at line 7 and E_WARNING : type 2 -- Illegal string offset 'files' -- at line 8

3 - The returned array after a print_r, isnt what I would have expected, where did the "P" come from? :-S:-
Array ( [0] => [1] => Page0 [2] => [3] => Page1 [4] => P [5] => Page2 [6] => P [7] => Page3 )

Any ideas what Im doing wrong in what looks like a simple task :-S

Thank you
ASKER CERTIFIED SOLUTION
Avatar of tonelm54
tonelm54

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi
array_push() accept as first parameter an array and as second parameters typically a string or another array. In your code you're passing an elment of the array itself ($remitanceCollection[0]["files"] - and this just doesn't make sense to me) and then a string ("Page0"). In addition, you're trying to create an array which has always the same index "Files" and this is not allowed: using always the same index the last inserted value will overwrite the previous one.
I think you should rethink your array structure depending on how you retrieve the file list and the pages for each file...
Anyway, in order to get an array similar the one you have shown above you should write something like this
$remitanceCollection = array();
	array_push($remitanceCollection, array("File01"=>array("Page0", "Page1")));
	array_push($remitanceCollection, array("File02"=>array("Page2", "Page3")));

Open in new window

Actually, I would need to see more code and how you get files and pages to give you a better help, but this should give you an idea. Probably the array $remitanceCollection  should be filled within a loop through another array which stores files and the number of pages per file...
Avatar of tonelm54
tonelm54

ASKER

Good morning Marco Gasi,
I have actually closed this, and opened a new ticket under 28929112, as I still cant get this working :-S

The problem with your solution is I cannot add the pages to the array in one line as in:-
array_push($remitanceCollection, array("File01"=>array("Page0", "Page1")));

Open in new window


I need to add them in sepertatly, like:-
array_push($remitanceCollection, array("File01"=>array("Page0")));
array_push($remitanceCollection, array("File01"=>array("Page1")));

Open in new window


Because Im reading each page and then assigning the pages to the correct remitance file, so at the end I will have an array of remitances, each with an array of what pages they should contain. Then I can patch them together.

Thank you