I have a csv file that is structured in this way. It gives me the number of pagesviews by hour
"00","ARTICLE 1",100
"00","ARTICLE 2",50
"00","ARTICLE 3",30
"01","ARTICLE 1",40
"01","ARTICLE 2",100
"01","ARTICLE 4",200
"02","ARTICLE 1",30
"02","ARTICLE 2",40
"02","ARTICLE 3",30
"03","ARTICLE 5",30
I import the csv in my php code and i insert the contents in a mutidimensional array. When i var_dump my array this is what i get
Array
(
[00] => Array
(
[ARTICLE 1] => 100
[ARTICLE 2] => 50
[ARTICLE 3] => 30
)
[01] => Array
(
[ARTICLE 1] => 40
[ARTICLE 2] => 100
[ARTICLE 4] => 200
)
[02] => Array
(
[ARTICLE 1] => 30
[ARTICLE 2] => 40
[ARTICLE 3] => 30
)
[03] => Array
(
[ARTICLE 5] => 30
)
)
So my issue is "ARTICLE 4" was only viewed at "01" but i would like it to appear in the keys "00","02","03" with a default value of 0. The same for "ARTICLE 5" which only appears at key "03". I would like it to appear in keys "00","01","02","03","04"
AT the end, i would like to my array to appeear like this
Array
(
[00] => Array
(
[ARTICLE 1] => 100
[ARTICLE 2] => 50
[ARTICLE 3] => 30
[ARTICLE 4] => 0
[ARTICLE 5] => 0
)
[01] => Array
(
[ARTICLE 1] => 40
[ARTICLE 2] => 100
[ARTICLE 3] => 0
[ARTICLE 4] => 200
[ARTICLE 5] => 0
)
[02] => Array
(
[ARTICLE 1] => 30
[ARTICLE 2] => 40
[ARTICLE 3] => 30
[ARTICLE 4] => 0
[ARTICLE 5] => 0
)
[03] => Array
(
[ARTICLE 1] => 0
[ARTICLE 2] => 0
[ARTICLE 3] => 0
[ARTICLE 4] => 0
[ARTICLE 5] => 30
)
)
I have tried doing something like this but it's not working
foreach ($orig as $item) {
$new[] = $item;
$new[] = $item;
}
Can some one please help ?
UPDATE
I tried something like this
foreach ($articles as $article)
{
$pageviews2[$article] = $pageviews;
}
Where $pageviews2 contains the contents of my csv file and the array articles is structured in this way
$articles = Array("00", "01", "02", "03","04","05");
And below is the resuly i obtain when i var_dump the $pageviews2 array
Array
(
[00] => Array
(
[00] => Array
(
[ARTICLE 1] => 100
[ARTICLE 2] => 50
[ARTICLE 3] => 30
)
[01] => Array
(
[ARTICLE 1] => 40
[ARTICLE 2] => 100
[ARTICLE 4] => 200
)
[02] => Array
(
[ARTICLE 1] => 30
[ARTICLE 2] => 40
[ARTICLE 3] => 30
)
[03] => Array
(
[ARTICLE 5] => 30
)
)
[01] => Array
(
[00] => Array
(
[ARTICLE 1] => 100
[ARTICLE 2] => 50
[ARTICLE 3] => 30
)
[01] => Array
(
[ARTICLE 1] => 40
[ARTICLE 2] => 100
[ARTICLE 4] => 200
)
[02] => Array
(
[ARTICLE 1] => 30
[ARTICLE 2] => 40
[ARTICLE 3] => 30
)
[03] => Array
(
[ARTICLE 5] => 30
)
)
The way I've typically dealt with this type of situation is as you've mentioned, pre-populating each "outer" elements with a set of known values. This works under the assumption that you know exactly how many elements you want in the second dimension of the array, and that you know ahead of time how many elements will be in the first. Here is some code to demonstrate:
$pageviews = array();
$articles = Array("00", "01", "02", "03");
$prepopulated = Array (
"Article 1" => 0,
"Article 2" => 0,
"Article 3" => 0,
"Article 4" => 0,
"Article 5" => 0
);
You can then create a skeleton array before importing the csv:
foreach ($articles as $article) {
$pageviews[$article] = $prepopulated;
}
Then, when you import the data from the csv, all that you're doing is overwriting the default values in the skeleton array you just created:
if (($handle = fopen("pageviews.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$pageviews[$data[0]][$data[1]] = $data[2];
}
fclose($handle);
}