douhan5547 2014-01-11 23:59
浏览 45

在重复的数组键/值中插入默认值

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条回答 默认 最新

  • dregvw1801 2014-01-12 01:05
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行