gpt4 book ai didi

javascript - 我应该如何调用这个数组输出?

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:17 27 4
gpt4 key购买 nike

我想调用这个数组输出:

[{"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"]}]

这是这段代码的结果:

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

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

echo json_encode($Myoffers);

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

<a target="_blank" href="'+t.link[0]+'">'+t.title[0]+"</a>

但什么也没显示!

最佳答案

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);

关于javascript - 我应该如何调用这个数组输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41709894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com