Count the occurence of a string in multidimentional array - php

I'm trying to count the occurence of a string in multidimentional array and get it.
So I found this : http://php.net/manual/fr/function.array-count-values.php
and an exemple in the "User Contributed Notes".
Here my code :
$count_values = array();
foreach ($Bigtable as $a)
{
foreach ($a as $table)
{
$count_values[$table]++; // line 32
}
}
asort($count_values,SORT_NUMERIC );
$onet_code=end ($count_values);
Here my error: Notice: Undefined index: 11-1011.00 in /home/goldiman1/www/postQuestion.php on line 32
I think that the error is in the last line when I try to get the string.
What do you guys think about it?
Thank you
Goldiman
Edit: Thank you everybody for your help ! All solutions work like a charm and I understood what was the issue, it was easier to understand Kasia answer because i'm more familiar with isset()

$count_values[$table]++;
This line is trying to increment the value in $count_values[$table] - but you never initialize that value in the first place!
You need to change this code to create the value at 1 if it doesn't already exist, and only increment it if it does exist.

If $table exists on the array(key) then increment the value else create new one. Try this -
foreach ($Bigtable as $a)
{
foreach ($a as $table)
{
if (array_key_exists($table, $count_values)) {
$count_values[$table]++;
} else {
$count_values[$table] = 1;
}
}
}

by default $count_values[$table] won't be set
foreach ($a as $table)
{
if(isset($count_values[$table])){
$count_values[$table]++;
} else {
$count_values[$table] =1
}
}

Related

Iterating through 3 loops of different length - PHP

I have 2 arrays and i want to make a 3rd array after comparison of the 2 arrays. Code is as follows:
foreach($allrsltntcatg as $alltests)
{
foreach($alltests as $test)
{
foreach($allCatgs as $catg)
{
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
$catcounts[$catg['testcategoryname']] +=1;
}
}
}
}
It, although returns the right answer, it also generates a PHP error and says undefined index and prints all errors and also the right answer.
I just want to avoid the array out of bound error. Kindly help me
Problem is in if condition correct like below : You have to initialize array first and than you can increment value
if($catg['testcategoryid'] == $test['testcategory_testcategoryid'])
{
if (isset($catcounts[$catg['testcategoryname']]))
$catcounts[$catg['testcategoryname']] +=1;
else
$catcounts[$catg['testcategoryname']] =1;
}
When the array try to add some arithmetic operation of undefined index such as $catg['testcategoryname'] in the $catcounts array then the warning generates. Before add the number you have to check the index is present or not, and of not then just assign value otherwise add into it.
So do it in this way just if condition-
if(....){
if(array_key_exists($catg['testcategoryname'], $catcounts))
$catcounts[$catg['testcategoryname']] +=1; // Add into it
else
$catcounts[$catg['testcategoryname']] = 1; // Assign only
}
More about array key exists--See more
$catg['testcategoryname'] should represent an index in $catcounts array.

Any ideas why this simple code isnt working?

Does anyone know why this simple bit of code wont work? The second loop doesnt seem to take place.
while(list($get_key, $get_value) = each($HTTP_GET_VARS)) {
if (!${$get_key}) {
${$get_key}=$get_value;
}
}
while(list($keyone, $valueone) = each($HTTP_GET_VARS))
{
echo $keyone;
}
Your problem here is the use of each(). It remembers the index it has reached in the array so in your second loop there is nothing left to loop out so it evaluates to false. If you use reset() this should resolve the issue.
while(list($get_key, $get_value) = each($HTTP_GET_VARS)) {
if (!${$get_key}) {
${$get_key}=$get_value;
}
}
reset($HTTP_GET_VARS);//will put the pointer back to the first element
while(list($keyone, $valueone) = each($HTTP_GET_VARS))
{
echo $keyone;
}
Alternatively you can just use the foreach(){} syntax which will always start from the first element in the array.

How do I create variables based on an array? PHP

I'm quite confused in how would I be able to create variable based on an array's values, my code is:
$a = array("red","black","white","green","blue");
for($i=0;$i>5;$i++)
{
$$a[$i] = '0.00';
}
echo $red;
I was under the impression that emulating a statement that says $red = '0.00'; would run properly but alas, it says undefined variable red.
use this:
for($i=0;$i<5;$i++)
you got error in loop, you have used '>' sign, so loop doesn't work, actually... :)
It's only your assignation that is wrong.
Use a foreach loop to make it easier, and it will work :
$a = array("red","black","white","green","blue");
foreach ($a as $val) {
$$val = '0.00';
}
echo $red;
Output :
0.00

lost var in php

I am fairly new to PHP and Yii, and the problem that I am not nor as the question in google, so the only thing I can think of is to ask the question to this list that I have solved many problems.
The issue is as follows: in the code that I attached, I read several records that I keep in array and after the process.
Well, if you look at the debug entries in foreach in he first, all goes well and the variable $items is loaded, but when I get to the second debug $items variable has the correct number of elements, but the elements are empty : count ($items) = 2 but $items[0] and $items[1] are null
$idiomas=CListaMidiomas::model()->findAll();
$items=array();
$nombre=array();
$a=0;
foreach ($idiomas as $idioma){
$nombre[$a]=$idioma->sIdioma;
$items[$a]=TblCategoriastexto::model()->findAll(
array('condition'=>'id='.$data->id.' AND idIdioma='.$idioma->id_idioma));
echo "<br>---AAAAAAAAAAA--".$a."-----------<br>";
CVarDumper::dump($items); //in this moment is correct
if (empty($items[$a]) ||$items[$a]==null ){ // not enter because $items have content
$items[$a]=new TblCategoriastexto();
$items[$a]->idIdioma=$idioma->id_idioma;
}
$a++;
}
echo ">>>>>>>>>>>>>>>".count($items) ; //<<<<<<<<<<present 2
CVarDumper::dump($items); // but in this moment t0 2 are null
for ($a=0;$a<count($items) ;$a++){
echo "<b>".CHtml::encode($nombre[$a]).":</b>";
$out="";
$item=$items[$a];
echo "<br>-----".$a."-----------<br>";
CVarDumper::dump($items[$a]);<<<<<<<<<<<<<<<<<<<<<<<<null
for ($b=1;$b<=20;$b++){
$campo="tc".$b;
$out.=$items[$a]->$campo . ",";<<<<<<<<<<<<<<<<error
}
echo CHtml::encode($out);
echo"<br>";
}
This line: if (empty($items[$a]) ||$items[$a]=null ){ will always assign $items[$a] to null.
To compare values, use the comparison (for equality) operator, == instead of the assignment operator =.
Try changing this line:
if(isset($items[$a]->$campo)) {
$out.=$items[$a]->$campo . ",";
}

PHP/MySQL array help need

<?php
include 'db.php';
$i=0;
$result15=mysql_query("select c.dishes from c");
while($row=mysql_fetch_array($result15))
{
if($row['dishes']!=NULL)
{
$dish[$i]=$row['dishes'];
$i++;
}
}
mysql_close();
$j=0;
while($j<=$i)
{
echo $dish[$j];
$j++;
}
?>
Getting notice: Undefined offset: 2 in F:\xampp\htdocs....on line 18
You mean while($j<$i) there.
Remember, you incremented $i after the last insert. This means that $i will be higher than the maximum key of $dish.
Some thoughts:
Any time you're testing for equality with null, you should consider using is_null (or !is_null). It is more accurate.
This:
$dish[$i]=$row['dishes'];
$i++;
Would be better as:
// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes'];
That final while loop would be better as a foreach:
foreach($dish as $val)
{
echo $val;
}
You need to make sure the index exists before echoing it, as so:
while($j<=$i)
{
if (isset($dish[$j])) echo $dish[$j];
$j++;
}
Your second while loop is going to far. You only define dishes up to i-1 and then you loop through it up to and including i. Change it to:
while($j<$i)

Categories