我有一个像这样输出的数组
array:25 [▼
1 => array:3 [▼
"title" => "The Superfish problem is Microsoft's opportunity to fix a huge problem and have manufacturers ship their computers with a vanilla version of Windows. Versions of windows preloaded with crapware (and now malware) shouldn't even be a thing."
"created" => 1424600986
"comments" => array:6 [▼
0 => "<!-- SC_OFF --><div class="md"><p>Lenovo did a stupid/terrible thing by loading their computers with malware. But HP and Dell have been loading their computers with unnecessary software for years now.</p>"
1 => ""
2 => "<p>The people that aren&#39;t smart enough to uninstall that software, are also not smart enough to blame Lenovo or HP instead of Microsoft (and honestly, Microsoft deserves some of the blame for allowing these OEM installs anways).</p>"
3 => ""
4 => "<p>There are many other complications that result from all these differentiated versions of Windows. The time is ripe for Microsoft to stop letting companies ruin windows before the consumer even turns the computer on.</p>"
5 => "</div><!-- SC_ON -->"
]
]
2 => array:3 [▶]
3 => array:3 [▶]
我想要做的是访问comments数组和嵌套数组中的每个注释我要提取每个注释并将其保存到数据库中
我试过这样做
$counter = 1;
foreach ($posts as $post) {
//foreach post get the data and store it in a database
$allData[$counter]['title']= $post['data']['title'];
//Foreach Title, get a sentiment using the sentiment analysis
$sentiment = SentimentAnalysis::decision($allData[$counter]['title']);
$allData[$counter]['created']= $post['data']['created'];
RedditPosts::create([
'title' => $allData[$counter]['title'],
'created' => date($allData[$counter]['created']),
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
foreach($posts['comments'] as $comment) {
$sentiment = SentimentAnalysis::decision($allData[$counter]['comments']);
$comment = RedditComment::create([
'comment' => $comment,
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
}
$counter ++;
}
然而,这不起作用。我在评论中获得了未定义的索引。我相信我走在正确的轨道上,我只需要找到一种方法来访问帖子数组中的这一系列评论。
有没有办法循环遍历这个数组并抓取每个注释条html,然后将其保存到数据库中
***************************解决******************* ******************
这就像这样解决了
//Collect the data to be stored in the DB
$allData=[];
$counter = 1;
foreach ($posts as $post) {
//foreach post get the data and store it in a database
$allData[$counter]['title']= $post['data']['title'];
//Foreach Title, get a sentiment using the sentiment analysis
$sentiment = SentimentAnalysis::decision($allData[$counter]['title']);
$allData[$counter]['created']= $post['data']['created'];
//store this in the DB to have access to the information at a later stage
RedditPosts::create([
'title' => $allData[$counter]['title'],
'created' => date($allData[$counter]['created']),
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
$allData[$counter]['comments'] = explode(PHP_EOL, $post['data']['selftext_html']);
foreach($allData[$counter]['comments'] as $comment) {
$sentiment = SentimentAnalysis::decision($comment);
//store this in the DB to have access to the information at a later stage
RedditComments::create([
'comment' => strip_tags(html_entity_decode($comment)),
'created' => date($allData[$counter]['created']),
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
}
$counter ++;
}
答案 0 :(得分:0)
(function($) {
$.fn.tabSlideOut = function(callerSettings) {
var settings = $.extend({
tabHandle: '.handle',
speed: 300,
action: 'click',
tabLocation: 'left',
topPos: '200px',
leftPos: '20px',
fixedPosition: false,
positioning: 'absolute',
pathToTabImage: null,
imageHeight: null,
imageWidth: null,
onLoadSlideOut: false
}, callerSettings || {});
settings.tabHandle = $(settings.tabHandle);
var obj = this;
if (settings.fixedPosition === true) {
settings.positioning = 'fixed';
} else {
settings.positioning = 'absolute';
}
//ie6 doesn't do well with the fixed option
if (document.all && !window.opera && !window.XMLHttpRequest) {
settings.positioning = 'absolute';
}
//set initial tabHandle css
if (settings.pathToTabImage != null) {
settings.tabHandle.css({
'background': 'url(' + settings.pathToTabImage + ') no-repeat',
'width': settings.imageWidth,
'height': settings.imageHeight
});
}
settings.tabHandle.css({
'display': 'block',
'textIndent': '-99999px',
'outline': 'none',
'position': 'absolute'
});
obj.css({
'line-height': '1',
'position': settings.positioning
});
var properties = {
containerWidth: parseInt(obj.outerWidth(), 10) + 'px',
containerHeight: parseInt(obj.outerHeight(), 10) + 'px',
tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
};
//set calculated css
if (settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
obj.css({
'left': settings.leftPos
});
settings.tabHandle.css({
'right': 0
});
}
if (settings.tabLocation === 'top') {
obj.css({
'top': '-' + properties.containerHeight
});
settings.tabHandle.css({
'bottom': '-' + properties.tabHeight
});
}
if (settings.tabLocation === 'bottom') {
obj.css({
'bottom': '-' + properties.containerHeight,
'position': 'fixed'
});
settings.tabHandle.css({
'top': '-' + properties.tabHeight
});
}
if (settings.tabLocation === 'left' || settings.tabLocation === 'right') {
obj.css({
'height': properties.containerHeight,
'top': settings.topPos
});
settings.tabHandle.css({
'top': 0
});
}
if (settings.tabLocation === 'left') {
obj.css({
'left': '-' + properties.containerWidth
});
settings.tabHandle.css({
'right': '-' + properties.tabWidth
});
}
if (settings.tabLocation === 'right') {
obj.css({
'right': '-' + properties.containerWidth
});
settings.tabHandle.css({
'left': '-' + properties.tabWidth
});
$('html').css('overflow-x', 'hidden');
}
//functions for animation events
settings.tabHandle.click(function(event) {
event.preventDefault();
});
var slideIn = function() {
if (settings.tabLocation === 'top') {
obj.animate({
top: '-' + properties.containerHeight
}, settings.speed, settings.onSlideIn).removeClass('open');
} else if (settings.tabLocation === 'left') {
obj.animate({
left: '-' + properties.containerWidth
}, settings.speed, settings.onSlideIn).removeClass('open');
} else if (settings.tabLocation === 'right') {
obj.animate({
right: '-' + properties.containerWidth
}, settings.speed, settings.onSlideIn).removeClass('open');
} else if (settings.tabLocation === 'bottom') {
obj.animate({
bottom: '-' + properties.containerHeight
}, settings.speed, settings.onSlideIn).removeClass('open');
}
};
var slideOut = function() {
if (settings.tabLocation == 'top') {
obj.animate({
top: '-3px'
}, settings.speed, settings.onSlideOut).addClass('open');
} else if (settings.tabLocation == 'left') {
obj.animate({
left: '-3px'
}, settings.speed, settings.onSlideOut).addClass('open');
} else if (settings.tabLocation == 'right') {
obj.animate({
right: '-3px'
}, settings.speed, settings.onSlideOut).addClass('open');
} else if (settings.tabLocation == 'bottom') {
obj.animate({
bottom: '-3px'
}, settings.speed, settings.onSlideOut).addClass('open');
}
settings.onSlideOut
};
var clickScreenToClose = function() {
obj.click(function(event) {
event.stopPropagation();
});
$(document).click(function() {
slideIn();
});
};
var clickAction = function() {
settings.tabHandle.click(function(event) {
if (obj.hasClass('open')) {
slideIn();
} else {
slideOut();
}
});
clickScreenToClose();
};
var hoverAction = function() {
obj.hover(
function() {
slideOut();
},
function() {
slideIn();
});
settings.tabHandle.click(function(event) {
if (obj.hasClass('open')) {
slideIn();
}
});
clickScreenToClose();
};
var slideOutOnLoad = function() {
slideIn();
setTimeout(slideOut, 500);
};
//choose which type of action to bind
if (settings.action === 'click') {
clickAction();
}
if (settings.action === 'hover') {
hoverAction();
}
if (settings.onLoadSlideOut) {
slideOutOnLoad();
};
};
})(jQuery);
$(function() {
$('.slide-out-div').tabSlideOut({
tabHandle: '.handle', //class of the element that will become your tab
pathToTabImage: 'http://s14.postimg.org/pugbeet4h/cs_slide_out.png', //path to the image for the tab //Optionally can be set using css
imageHeight: '217px', //height of tab image //Optionally can be set using css
imageWidth: '33px', //width of tab image //Optionally can be set using css
tabLocation: 'right', //side of screen where tab lives, top, right, bottom, or left
speed: 500, //speed of animation
action: 'click', //options: 'click' or 'hover', action to trigger animation
topPos: '200px', //position from the top/ use if tabLocation is left or right
leftPos: '20px', //position from left/ use if tabLocation is bottom or top
fixedPosition: false //options: true makes it stick(fixed position) on scroll
});
$('.slide-out-div > .handle').click();
});
您需要将线程父级的概念与子级分开。您不必使用我在这里使用过的模型名称,但这是基本的想法。
您的另一个选择是只有一个模型来表示reddit线程,并且$threads = $SearchResultsArray['data']['children'];
foreach($threads as $thread) {
$parent = RedditThread::create(['title' => $thread['title']);
foreach($threads['comments'] as $comment) {
$comment = RedditComment::create(['comment' => $comment]);
$parent->comments()->save($comment);
}
}
属性是序列化数组。如果你很好奇,请阅读Laravel文档中的属性转换。
答案 1 :(得分:0)
//Collect the data to be stored in the DB
$allData=[];
$counter = 1;
foreach ($posts as $post) {
//foreach post get the data and store it in a database
$allData[$counter]['title']= $post['data']['title'];
//Foreach Title, get a sentiment using the sentiment analysis
$sentiment = SentimentAnalysis::decision($allData[$counter]['title']);
$allData[$counter]['created']= $post['data']['created'];
//store this in the DB to have access to the information at a later stage
RedditPosts::create([
'title' => $allData[$counter]['title'],
'created' => date($allData[$counter]['created']),
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
$allData[$counter]['comments'] = explode(PHP_EOL, $post['data']['selftext_html']);
foreach($allData[$counter]['comments'] as $comment) {
$sentiment = SentimentAnalysis::decision($comment);
//store this in the DB to have access to the information at a later stage
RedditComments::create([
'comment' => strip_tags(html_entity_decode($comment)),
'created' => date($allData[$counter]['created']),
'sentiment' => $sentiment,
'search_identifier' => $search_id,
'search_phrase' => $searchPhrase
]);
}
$counter ++;
}