0

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
            )

    )
1
  • It would be a better question if you provide some code that we can actually copy/paste to try (like a real array instead of the var_dump output). And also explain what you tried so far, what didn't work; your current code doesn't do much... Commented Jan 12, 2014 at 0:04

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

the problem is i can't tell in advance the number of times to be displayed in the second dimension of the array and i dont know how many elements will be in the first. }
Ah. I'll update the answer to include a more generic approach that will determine the range of the elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.