我该如何调用此数组输出?

时间:2017-01-18 01:20:23

标签: javascript php

我想调用这个数组输出:

var result = list.GroupBy(x => (x.Date - 1)/ 7)
    .Select(x => x.Sum(y => y.Qty));

这是此代码的结果:

[{"title":["OFFER 1, INCENT, US"],"link":["http:\/\/jump.ogtrk.net\/aff_c?aff_id=15447&offer_id=1930&aff_sub=MYID"]}][{"title":["OFFER 2, Free, INCENT, US"],"link":["http:\/\/jump.ogtrk.net\/aff_c?aff_id=15447&offer_id=2081&aff_sub=MYID"]}][{"title":["OFFER 3, Free, INCENT, US, 113M"],"link":["http:\/\/jump.ogtrk.net\/aff_c?aff_id=15447&offer_id=2993&aff_sub=MYID"]}][{"title":["OFFER 4"],"link":["http:\/\/jump.ogtrk.net\/aff_c?aff_id=15447&offer_id=3293&aff_sub=MYID"]}][{"title":["OFFER 5"],"link":["http:\/\/jump.ogtrk.net\/aff_c?aff_id=15447&offer_id=3295&aff_sub=MYID"]}]

我实际上用它来通过javascript调用标题和链接:

foreach($json['offers'] as $offer) {

$Myoffers = array(array("title"=> array($offer['name']), "link"=> array($offer['link'])));

echo json_encode($Myoffers);

但没有出现!

2 个答案:

答案 0 :(得分:1)

t.title[0]

应该是

t[0].title[0]

由于数据是一个对象数组。

[{}, {}...];

如果结构是 {{}, {}...};而不是t.title[0]会有效。

let data = [{
  "title": ["OFFER 1, INCENT, US"],
  "link": ["http:\/\/j"]
}];

var first = data[0]; // Gives access to the first element
var allTitles = first.title; // Access to the title array
var specificTitle = allTitles[i]; // i - to access a specific value
var link = first.link[0]; // access the link for the first object

<强>更新

我认为问题是你在循环的每次迭代中使用echo。只需构造一个新数组并推送每次迭代的项目。一旦完成for循环,就echo

// All offers 
$allOffers = array; 
//loop through the offers 
foreach($json['offers'] as $offer) { 

   //as an example we output the offer names 
   //echo $offer['link']; 
   $Myoffers = array("title"=> array($offer['name']), "link"=> array($offer['link'])); 
   $allOffers[] = $Myoffers; 
} 

// echo it after the foreach loop 

echo json_encode($allOffers);

答案 1 :(得分:0)

您的数组输出不是有效对象。在输出中,包含该对象的数组以单个对象结尾,也没有分隔符, 您需要纠正输出创建的逻辑。结果应该像

[
    {
        "title": [
            "OFFER 1, INCENT, US"
        ],
        "link": [
            "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=1930&aff_sub=MYID"
        ]
    },
    {
        "title": [
            "OFFER 2, Free, INCENT, US"
        ],
        "link": [
            "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=2081&aff_sub=MYID"
        ]
    },
    {
        "title": [
            "OFFER 3, Free, INCENT, US, 113M"
        ],
        "link": [
            "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=2993&aff_sub=MYID"
        ]
    },
    {
        "title": [
            "OFFER 4"
        ],
        "link": [
            "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=3293&aff_sub=MYID"
        ]
    },
    {
        "title": [
            "OFFER 5"
        ],
        "link": [
            "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=3295&aff_sub=MYID"
        ]
    }
]

或者

   [
        [
            {
                "title": [
                    "OFFER 1, INCENT, US"
                ],
                "link": [
                    "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=1930&aff_sub=MYID"
                ]
            }
        ],
        [
            {
                "title": [
                    "OFFER 2, Free, INCENT, US"
                ],
                "link": [
                    "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=2081&aff_sub=MYID"
                ]
            }
        ],
        [
            {
                "title": [
                    "OFFER 3, Free, INCENT, US, 113M"
                ],
                "link": [
                    "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=2993&aff_sub=MYID"
                ]
            }
        ],
        [
            {
                "title": [
                    "OFFER 4"
                ],
                "link": [
                    "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=3293&aff_sub=MYID"
                ]
            }
        ],
        [
            {
                "title": [
                    "OFFER 5"
                ],
                "link": [
                    "http://jump.ogtrk.net/aff_c?aff_id=15447&offer_id=3295&aff_sub=MYID"
                ]
            }
        ]
    ]

正确创建对象后,您可以像访问它一样访问它。

console.log(resp[0].title[0]) // as per first structure

console.log(resp[0][0].title[0]) // as per second structure