print_r statement doesn't seem to work - php

I am building a web crawler. It finds all the links on a page and their titles and meta descriptions etc. It does that fine. Then i wrote an array which gives all the starting urls for the links I want. So if it crawls a link and its url begins with any value in the array which gives the starting urls, insert into $news_stories.
The only problem is it doesn't seem to be inserting into them. The page returns blank and now it says that the array_intersect statement wants an array and that I havent specfied an array which I have.
In summary, I am struggling to understand where my code doesn't work and why the wanted urls aren't being inserted.
$bbc_values = array(
'http://www.bbc.co.uk/news/health-',
'http://www.bbc.co.uk/news/politics-',
'http://www.bbc.co.uk/news/uk-',
'http://www.bbc.co.uk/news/technology-',
'http://www.bbc.co.uk/news/england-',
'http://www.bbc.co.uk/news/northern_ireland-',
'http://www.bbc.co.uk/news/scotland-',
'http://www.bbc.co.uk/news/wales-',
'http://www.bbc.co.uk/news/business-',
'http://www.bbc.co.uk/news/education-',
'http://www.bbc.co.uk/news/science_and_enviroment-',
'http://www.bbc.co.uk/news/entertainment_and_arts-',
'http://edition.cnn.com/'
);
// BBC Algorithm
foreach ($links as $link) {
$output = array(
"title" => Titles($link), //dont know what Titles is, variable or string?
"description" => getMetas($link),
"keywords" => getKeywords($link),
"link" => $link
);
if (empty($output["description"])) {
$output["description"] = getWord($link);
}
}
$new_stories = array();
foreach ($output as $new_array) {
if (array_intersect($output['link'], $bbc_values) == true) {
$news_stories[] = $new_array;
}
print_r($news_stories);
}

You decalred the array as $new_stories and printing $news_stories..... diff is 'S'
check whether the code is coming inside this loop or not, i think not...
if (array_intersect($output['link'], $bbc_values) == true) {
echo 'here';
}

Hmm i don't think array_intersect is what you need for a comparison http://php.net/manual/en/function.array-intersect.php
Maybe you want to look for in_array http://php.net/manual/en/function.in-array.php

When the return parameter is used, this function uses internal output buffering so it cannot be used inside an ob_start() callback function.

Related

Array of String values, not an array of object Strings

Basically I am calling an API to get an array of URLs for an image.
I start off by doing this
$mainResponse = array(
"result" => array(
),
"ack" => "success"
);
I will then make my call and add the image URLs like this:
foreach($resp->Item as $item) {
$picture = $item->PictureURL;
array_push($mainResponse['result'], $picture);
}
Finally, I will echo this out to me.
echo json_encode($mainResponse);
The problem I am facing is that my response is
{"result":[{"0":"IMAGE_URL","1":"IMAGE_URL"}],"ack":"success"}
Where I would want it to be like....
{"result":["IMAGE_URL","IMAGE_URL"],"ack":"success"}
Where did I go wrong in my PHP code?
Seems like $picture is a associative array, change the foreach loop with this:
foreach($resp->Item->PictureURL as $item) {
foreach($item as $_item){
array_push($mainResponse['result'],$_item);}
}
For some reason this API returns an object instead of an array.
You can just do:
foreach ($resp->Item as $item) {
$picture = $item->PictureURL;
array_merge($mainResponse['result'], (array)$picture);
}
You can use array_push if you want every item to have separate pictures.

Combine more than 2 Smarty Arrays

When using array_combine I'm able to combine two array and use the loop through. In the example below I can us $title in my HTML.
{foreach array_combine($p_titles, $p_prices) as $title => $price}<!--SOME HTML-->{/foreach}
However if I try to add another array to this solution it breaks. My local host isn't displaying an error other than "Page not Working "localhost is currently unable to handle this request."
{foreach array_combine($p_titles, $p_prices, $p_ids) as $title => $price => $id}<!--SOME HTML-->{/foreach}
How do I combine 3 or more arrays using this method.
First of all, you tagged Javascript however what you have shown in you question is PHP.
Second, the array_combine only accepts two parameters. Documentation.
To use array_combine for three arrays do this:
$combinedArray = array_combine($p_titles, $p_prices);
$combinedArray = array_combine($combinedArray, $p_ids);
Edit: Answer that matches your code in normal PHP syntax.
$result = array();
foreach($p_titles as $key => $value)
{
$result[$value] = array('price' => $p_prices[$key], 'id' => $p_ids[$key]);
}
echo($result);
You can access this data like these: $result['title']['price'] or $result['title']['id'];
Edit 2: And the Smarty code.
I'm not that familiar with Smarty syntax so this might not work:
{ $result = array(); }
{ foreach from=$p_titles key=key item=value }
{ $result[$value] = array('price' => $p_prices[$key], 'id' => $p_ids[$key]) }
{ /foreach }

how to get the index of an array using the value in php

<?php
$interests[50] = array('fav_beverages' => "beer");
?>
now i need the index (i.e. 50 or whatever the index may be) from the value beer.
I tried array_search(), array_flip(), in_array(), extract(), list() to get the answer.
please do let me know if I have missed out any tricks for the above functions or any other function I`ve not listed. Answers will be greatly appreciated.
thanks for the replies. But for my disappointment it is still not working. btw I have a large pool of data like "beer");
$interests[50] = array('fav_cuisine' => "arabic");
$interests[50] = array('fav_food' => "hummus"); ?> . my approach was to get the other data like "arablic" and "hummus" from the user input "beer". So my only connection is via the index[50].Do let me know if my approach is wrong and I can access the data through other means.My senior just informed me that I`m not supposed to use loop.
This should work in your case.
$interests[50] = array('fav_beverages' => "beer");
function multi_array_search($needle, $interests){
foreach($interests as $interest){
if (array_search($needle, $interest)){
return array_search($interest, $interests);
break;
}
}
}
echo multi_array_search("beer", $interests);
If your array contains multiple sub-arrays and you don't know which one contains the value beer, then you can simply loop through the arrays, and then through the sub-arrays, to search for the value, and then return the index if it is found:
$needle = 'beer';
foreach ($interests as $index => $arr) {
foreach ($arr as $value) {
if ($value == $needle) {
echo $index;
break;
}
}
}
Demo

Different Array Values depending on the if statement

I am building a web crawler which scans links, titles and meta descriptions from links that are found from one url submitted
This if statement i think is correct. $description is the variable which holds all the descriptions from the array $link. But i notice not all sites have a meta description (wikipedia for example) so i have decided that i would like the first twenty characters to act as the description if the description is empty. (By the way, the function and calling of everything works, i just wanted you to see it)
if ($description == '') {
$html = file_get_contents($link);
preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re);
$res = get_custom_excerpt($re[1]);
echo "\n";
echo $res;
echo "\n";
}
However, in the array, the links are stored in [link], the title of the link in [title] and the description in [description]. But i don't know how i would cope with adding $res to my array and to only use if the if statement works.
$output = Array();
foreach ($links as $thisLink) {
$output[] = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink), getMetas($res));
}
print_r($output);
You can use array_push() to add $res back to your array and then evaluate the array however you need to; not 100% sure what you're trying to do...
From your wording I think you want to do this:
$outputs = array();
foreach ($links as $thisLink) {
$output = array("link" => $thisLink, "title" => Titles($thisLink), "description" => getMetas($thisLink));
if ($output['description'] == null) {
$output['description'] = getMetas($res);
}
$outputs[] = $output;
}
You might want to adjust the if statement because I do not know what getMetas() returns when there is not description.

PHP Multidimensional Arrays

I'm trying to make a universal script that adds keywords to my individual pages (since header is in an include file) so I am getting the end of the url (multi.php) and retrieving the desc etc. from it's array. For some reason instead of returning keywords or descriptions it instead just returns "m" . . . it's kind of random and has me scratching my head. Here's what I got
<html>
<head>
<title>Multi-Demensional Array</title>
<?php
$path = pathinfo($_SERVER['PHP_SELF']);
$allyourbase = $path['basename'];
$pages = array
(
"multi.php" => array
(
"keywords" => "index, home, test, etc",
"desc" => "This is the INDEX page",
"style" => "index.css"
),
"header.php" => array
(
"keywords" => "showcase, movies, vidya, etc",
"desc" => "SHOWCASE page is where we view vidya.",
"style" => "showcase.css"
)
);
?>
</head>
<body>
<?php
foreach($pages as $key => $value)
{
if($key == $allyourbase)
{
echo $key['desc'];
}
}
?>
</body>
</html>
The reason why this is happening is because in PHP if I had the following code:
$hello = 'world';
and I attempted to do the following:
echo $hello[0];
PHP Would treat the string as an array and return me whatever is in position 0, which would result in w, when your using a foreach your asking PHP to set the key of the array to $key, and it's value to $value.
you then echo echo $key['desc'];, as the value is a string, php sees it as an integer based index, so it will ignore your call for desc and then return the first index, if you were to change echo $key['desc'] to echo $value['desc'] which is a hash based array it will return the desired results.
You should just be able to do this:
if(isset($pages[$allyourbase]))
{
echo $pages[$allyourbase]['desc'];
}
No need for the loop
try
echo $key['desc'];
replace with
echo $value['desc'];
Other people have provided some great solutions, but it's important that you understand exactly what is happening here, so you don't make the same mistake again. Pay careful attention to the comments, and you will be on your way to successful coding!
Here's what is happening:
foreach ($pages as $key => $value) {
if ($key == $allyourbase) {
// At this point: $key = 'multi.php'
// Also: $value = array( ... );
// Keep in mind: $key['desc'] = $key[0] = 'm';
// You are grabbing the first letter of the 'multi.php' string.
// When dealing with strings, PHP sees $key['desc'] as $key[0],
// which is another way to grab the very first character of 'multi.php'
echo $key['desc'];
// You really want $pages[$key]['desc'], but below
// is a better way to do it, without the overhead of
// the loop.
}
}
If you kept the loop, which is really unnecessary, it would look like this:
foreach ($pages as $key => $value) {
if ($key == $allyourbase) {
echo $value['desc'];
}
}
The best solution is to replace the loop with the following code:
if (isset($pages[$allyourbase])) {
echo $pages[$allyourbase]['desc'];
} else {
// error handling
}
If I'm reading this right, echo $key['desc']; should be echo $value['desc'];.

Categories