Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two arrays in my code that I want to print out on a webpage, the arrays hold search engine results, here are the structures of the foreach loops
This loop returns the url,title,snippet and score
foreach ($js->RESULT as $item)
{
$blekkoArray[str_replace ($find, '', ($item->{'url'}))] = array(
'title'=> $item->{'url_title'},
'snippet' => $item->{'snippet'},
'score' => $score--
);
}
This loop is used for combining two arrays and creating a ranked list
foreach($googleArray as $key=>$value)
{
if(isset($combined[$key]))
$combined[$key]["score"] += $value['score'];
else
$combined[$key] = array("score"=>$value['score'],"title"=>$value["title"], "snippet"=>$value["snippet"]);
}
I know I can use print_r to print arrays but its not very user friendly, I would also like to href the urls as clickable links, any ideas would be gratefully appreciated.
The arrays prints out
Array ( [red.com/] => Array ( [score] => 197 [title] => blah blah....[snippet] more blah blah )
Array ( [green.com/] => Array ( [score] => 196 [title] => blah blah....[snippet] more blah blah )
So I would like it to look like how a conventional search engine displays results, I don't need the score to be displayed
Here is a function I found on a blog a while ago that works for me. I don't remember where I found it, or I would credit them with it. See if this does what you need it to. It may not be perfect, but may help you find the keys you need, then you can modify this to print out what you need. Often with arrays I have trouble determining the structure enough to write my foreach loops, and this helps with that.
function listArrayRecursive($array_name, $ident = 0){
if (is_array($array_name)){
foreach ($array_name as $k => $v){
if (is_array($v)){
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . " | ";
listArrayRecursive($v, $ident + 1);
}else{
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . $v . " | ";
}
}
}else{
echo "Variable = " . $array_name;
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 14 days ago.
Improve this question
<?php
$money = ["Ahmed" => 100, "Sayed" => 150, "Osama" => 100, "Maher" => 250];
// Output
foreach ($array as $key => $value)
{
echo <<<"hello"
The Name Is $key And I Need $value Pound From Him
<br>
hello;
}
?>
In php how can i make this foreach loop with for loop to get $key and $value separated
is this what you are asking for
<?php
$money = ["Ahmed" => 100, "Sayed" => 150, "Osama" => 100, "Maher" => 250];
// Output
$keys = array_keys($money);
$values = array_values($money);
for ($i = 0; $i < count($keys); $i++) {
echo "The Name Is " . $keys[$i] . " And I Need " . $values[$i] . " Pound From Him<br>";
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
$data = array(
array(
'name' => 'Ahmed',
'age' => 24,
'hobbies' => array('swimming','Drawing','Programming')
)
);
The final output I am trying to achieve is:
My Hobbies:1)swimming 2)Drawing 3)Programming
foreach ($data as $d) {
echo "My hobbies:";
foreach ($d['hobbies'] as $i => $h) {
echo $i+1 . ")" . $h . " ";
}
}
If your array structure is fix try this code
echo "My hobbies:";
foreach ($data[0]['hobbies'] as $Key => $value) {
echo $Key+1 . ")" . $value . " ";
}
My Hobbies:
<?php
foreach($data[$key]['hobbies'] as $hob_key => $hobby){
print $hob_key . ') ' . $hobby
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have 3 arrays like this.
Array
(
[id] => 4
)
Array
(
[id] => 6
)
Array
(
[id] => 7
)
What I would like to do is make a variable called $id that prints the id values as a string 4,6,7. How would I accomplish this?
If you always have those three arrays with the element "your_key" each,In first put arrays in some variable then you can simply concatenate the values using '.' in php:
$arr1=Array
(
['id'] => 4
)
$arr2=Array
(
['id'] => 6
)
$arr3=Array
(
['id'] => 7
)
$key = "id";
$id = $arr1[$key] . "," . $arr2[$key] . "," . $arr3[$key];
echo $id;
Edit: You can use the same approach in a loop:
$id = "";
$key = "id";
foreach($results as $result) {
$id .= $result[$key] . ",";
}
$id = rtrim($id, ",");
echo $id;
Based on your comments, this seems like the way you should go
$id = '';
$i=0;
$results = $mysqli->query($query);
$len = count($results);
foreach($results as $result) {
$id .= $result['id'];
$i++;
if($i < $len) {
$id .= ',';
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This loop just gives me the output Array instead of the value that shows when I use the print_r function.
print_r gives me this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 1
)
but the echo just array
for($i=0; $i<($n*$n); $i++){
for($j=0; $j<($n*$n); $j++){
$number = "column" . $i . $j;
$plan = $field[$i][$j] = $_POST[$number];
$myvariable[] = $field[$i][$j];
}
echo $myvariable;
}
but if I remove the [] from $myvariable it prints out the values. the problem is that I need to use the array with an unique array
$unique = array_unique($myvariable);
if (count($unique) != count($myvariable)) {
echo ="no uniques";
}
Any tips?
You mean this ?
$unique = array_unique($myvariable);
foreach($unique as $value)
{
echo $value;
}
or make use of a typical for
$unique = array_unique($myvariable);
for($i=0;$i<count($unique);$i++)
{
echo $unique[$i];
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to generate a title automatically based on the most repeated word in a text using PHP.
Example: if the the word "PHP" is repeated the most in a text The title would be : "The text is about PHP".... and so on.
I have no clue what to do or from where to start.
Can anyone help me with this?
if i must complete your homework assignment for you, i require full attribution within the paper and the link to this question also in said paper.
i also require that you actually read, understand and attempt to run this code to enable you to understand it.
//get all the test from the file
$text_from_file = file_get_contents("filename.txt");
//get all the words within that text
$words = str_word_count($text_from_file , 1);
//count up all the unique words within the array
$unique = array_count_values($words);
//sort by most to least frequent
arsort($unique); //arsort required to keep keys and values together
//since we dont know the key values here, we need to use foreach
foreach($unique as $key => $val) {
echo("The most common word is " . $key . " which occurs " . $val . " times");
break; //always break after the first echo
}
<?php
function mostRepeated($string = false, $words_num = 5) {
$string = strtolower($string);
// extend this array
$omit_words = array('the', 'a', 'an', 'in', 'at', 'by', 'of', 'was', 'is', 'he', 'she');
$words = explode(' ', $string);
foreach($words as $k => $v) {
if(in_array($word, $omit_words)) unset($words[$k]);
}
$count = array_count_values($words);
arsort($count);
$result = array();
foreach($count as $k => $v) {
$result[] = $k;
}
return $result;
}
$text = 'PHP foo Bar php foO pHp';
$most_repeated_words_array = mostRepeated($text, 3);
print_r($most_repeated_words_array);
?>
output:
Array
(
[0] => php
[1] => foo
[2] => bar
)
using
print_r( array_count_values(str_word_count($text, 1)) );
will give you a count of all words. You can then choose the top one when sorted ?
rsort
will give you a sorted array from high to low