counting matching elements of multiple arrays [duplicate] - php

This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 4 years ago.
I am looping through two associative array. First one is tags and second is data. I want to count matches for each tags in each data items.
for example
$tags= array("japan", "china", "usa")
$data=array("something...", "something..", "something..")
I want to count how many times japan in each posts ,how many times china in each posts and how many times usa in each post. Unfortunately I am getting count result for only first tag input japan. How to correct this ??
foreach ($tags as $tag) {
foreach ($data as $value) {
$count= substr_count($value,$tag);
}

Put the results in an associative array keyed by tag, and use += to increment the value for that tag.
$results = array();
foreach ($tags as $tag) {
$results[$tag] = 0;
foreach ($data as $value) {
$results[$tag] += substr_count($value, $tag);
}
}

Related

Php count conditions inside loop [duplicate]

This question already has answers here:
PHP - count specific array values
(10 answers)
Closed 9 days ago.
Close my question when it´sdifferent to this not duplicater, the case it´s different in all :
PHP - count specific array values
My question not the same
I have this array for example :
$array_test=array("gren","green","red","red","green","blue");
My idea is know inside loop number of elementos with the condition i want, the array for show it´s more complex and this is only example for understand i need, because try different ways with "count" and don´t show right this number in each case.
I try this :
foreach($array_test as $array_ts) {
if($array_ts=="green") { Count number of elements green /// }
if($array_ts=="red") { Count number of elements red /// }
if($array_ts=="blue") { Count number of elements blue /// }
}
Thank´s for the help, regards.
You can create an array and fill it with the count for each color:
$input = ["gren","green","red","red","green","blue"];
$count = [];
foreach ($input as $color) {
if (array_key_exists($color, $count)) {
$count[$color]++;
} else {
$count[$color] = 1;
}
}
$count will contain:
["green"=>3, "red"=>2, "blue"=>1]

How to push element with kay and value in array using PHP? [duplicate]

This question already has answers here:
PHP add elements to multidimensional array with array_push
(4 answers)
Closed 9 months ago.
I am iterating through an array of arrays that contains sales data returning from a MySQL query:
$result = mysql_query("select salespersonId,grossProfit FROM sales");
foreach ($result as $line) {
$salespersonId = $line['salespersonId'];
$grossProfit = $line['grossProfit'];
}
I want to push the line into separate arrays per salespersonId (so that the 10 lines belonging to salespersonId 1 are in one array, and the 10 lines belonging to salespersonId 2 are in a different array) but I cannot seem to figure out how to do this. I've tried
things like:
array_push(${'data'.$salespersonId},$line); to no avail. I feel like I might need an associative array of arrays, but you can't array_push into associative arrays. What am I missing?
You can do something like this
$result = mysql_query("select salespersonId,grossProfit FROM sales");
$arr = [];
foreach ($result as $line) {
if (empty($line['salespersonId'])) {
continue;
}
$arr[$line['salespersonId']][] = $line;
}
print_r($arr);

How to extract child key value from an array [PHP]? [duplicate]

This question already has answers here:
Loop a multidimensional array and only print two specific column values per row
(6 answers)
Closed 4 years ago.
The [1152] key is dynamic.
I need to access the child keys (ex: guid) but the parent key [1152] is dynamic and there is no way for me to know which number is going to be generated.
Is it possible to access [guid] without knowing what the number is in the parent key?
Yes process the first level of the array in a simple foreach and then the inner array specifically like this
foreach ($array as $dynamic) {
foreach ($dynamic as $key=>$val) {
echo $key . ' = ' . $val;
}
}
Or even more simply
foreach ($array as $dynamic) {
echo $dyanmic['guid'];
}

Using PHP foreach How to get first item from DB then others [duplicate]

This question already has answers here:
PHP - Grab the first element using a foreach
(10 answers)
Closed 5 years ago.
how can I get first item from db by foreach
$posts = new Posts();
$post = $posts->feature_post($conn);
foreach($post as $feature) { ?>
my html code is different for 1st item. so I need to get 1st item then other item,
how I can do it?
thanks in advance.
To get the first item of an array, you can use the reset function
http://php.net/manual/fr/function.reset.php
<?php
$posts = new Posts();
$listPost = $posts->feature_post($conn);
$firstPost = reset($listPost);
...
Also if you want to know if you loop through the first element and if the keys of your arrays are 0,1,2,3 etc..
<?php
foreach($array as $key => $cell) {
if ($key === 0) {
// this is your first element
....
}
}
If the keys of your array are not numeric indexes but you don't intend to use them, you can obtain such array by using the array_values function

How do I make a loop that will print out all of the elements in my array if the keys are strings and not ints? [duplicate]

This question already has answers here:
How to access "key" and "value" from an array passed to a for loop?
(5 answers)
Closed 8 years ago.
I want to print out all of the values in my array, however the keys for the elements are strings. How do I make a loop that will print out all of the elements in my array if the keys are strings and not ints?
Here is my code so far: http://codepad.viper-7.com/xGhmhX
You could use a foreach construct :
foreach ($your_array as $key => $value) {
// Work with either $key or $value
}
Or, if you don't need the key inside the loop, you can just use :
foreach ($your_array as $value) {
// Work with $value
}

Categories