Count duplicate values in PHP array - php

my problem is, i have this script
foreach ($arr1 as $k => $val) {
if (in_array($val, $arr2)) {
echo 'Obsazeno <br>';
} else {
echo $val . "<BR>";
}
}
Where $arr1 is generated array of days and $arr2 is array from mysql results. Actually it works fine. If one of days from $arr1 is in database, it will echo "Obsazeno" instead of day value.
But i need small upgrade. Script check if $val is in $arr2 and i need small counting contidion that will do this:
If $val is in $arr2 once - it will echo $val.
If $val is in $arr2 twice - it will echo $val.
But if $val is in $arr2 thrice - it will echo "Obsazeno".
I hope you understand my question.
I quest, it is possible by array_count_values. I try it by myself but no success.

You can achieve this with array_count_values.
$values = array_count_values($arr2);
foreach ($arr1 as $k => $val) {
if (array_key_exists($val, $values) && $values[$val] >= 3) {
echo 'Obsazeno <br>';
} else {
echo $val . '<br>';
}
}

try
$i=0;
foreach($arr1 as $k=>$val) {
if(in_array($val,$arr2)) {
$i++;
if($i == 3) {
echo 'Obsazeno <br>';
}
else {
echo $val."<BR>";
}
}
}

$data = array();
foreach($arr1 as $k=>$val)
{
if(!array_key_exists($val,$data)){
$data[$val] = 0;
}
if(in_array($val,$arr2))
{
$data[$val]++;
if($data[$val]==3){
echo 'Obsazeno <br>';
}else{
echo $val."<BR>";
}
}
else
{
echo $val."<BR>";
}
}

Related

I want to echo something only on the first time foreach runs

I want to echo something only on the first time foreach() runs!
let's say if I have an array $i
$i = array(............);
and I want to use it in foreach like...
foreach($i as $key => $value) {
echo '<h1>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
while this foreach runs the first time only i want to add class="show" to the h1 tag.
How to do that?
Update code:
$i=['hello','world'];
foreach($i as $key => $value) {
$class= ($key == 0)?'show':'';
echo '<h1 class="'.$class.'">'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
Output:
<div>
<h1 class="show">0</h1><p>hello</p>
<h1 class="">1</h1><p>world</p>
</div>
You'll need to add a var to track that
$first = 0;
foreach($i as $key => $value) {
$show=($first == 0) ? 'h1 class=show' : 'h1';
echo '<$show>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
$first++;
}
There are many ways to do it, but this might be easier to read
if you have single dimension array then you can use
$i=['test1','test2','test3'];
foreach($i as $key => $value) {
if($key == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
}
if you have an associative array
$i=array('a'=>'test1','b'=>'test2','c'=>'test3');
$j = 0;
foreach($i as $key => $value) {
if($j == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
$j++;
}
output will be

Iterate through Nested array in PHP

I have a nested array on this link Array Sample
I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.
printAllValues($ArrXML);
function printAllValues($arr) {
$keys = array_keys($arr);
for($i = 0; $i < count($arr); $i++) {
echo $keys[$i] . "$i.{<br>";
foreach($arr[$keys[$i]] as $key => $value) {
if(is_array($value))
{
printAllValues($value);
}
else
{
echo $key . " : " . $value . "<br>";
}
}
echo "}<br>";
}
}
What I am doing Wrong? Please help.
Version of J. Litvak's answer that works with SimpleXMLElement objects.
function show($array) {
foreach ($array as $key => $value) {
if (!empty($value->children())) {
show($value);
} else {
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
You can use recurcive function to print all values:
function show($array) {
foreach( $array as $key => $value) {
if (is_array($value)) {
show($value);
} else{
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);

How to Print multilevel array index in php

I have the following array:
$array=array("string",array(1,2,3),true,"php");
and I want to print indexes like:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value)
{
if(is_array($value))
{
foreach($value as $childkey=>$childvalue)
{
echo $key . "." . $childkey . "=>" . $childvalue . "\n";
}
}
elseif(is_bool($value))
{
echo $key . "=>" . ($value ? "true" : "false") . "\n";
}
else
{
echo $key . "=>" . $value . "\n";
}
}
Output:
0=>string
1.0=>1
1.1=>2
1.2=>3
2=>true
3=>php
Try this
<?php
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$value1){
echo $key.".".$key1." => ".$value1."</br>";
}
}
else{
echo $key." => ".$value."</br>";
}
}
<?php
$array=array("string",array(1,2,3),"true","php");
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$loop){
echo $key.'.'.$key1 .'=>'.$loop."<br>";
}
}else{
echo $key .'=>'.$value."<br>";
}
}
?>
Try this code
$array=array("string",array(1,2,3),true,"php");
foreach($array as $key=>$val){
if(is_array($val)){
foreach($val as $key1=>$val1){
echo $key.'.'.$key1 .'=>'.$val1.'<br/>';
}
}else{
echo $key .'=>'.$val.'<br/>';
}
}
You can try this-
<?php
$arr=array("string",array(1,2,3),true,"php");
$res=convArray($arr);
foreach($res as $k=>$v){
echo $k."=>".$v."\n";
}
function convArray($arr)
{
foreach($arr as $k1=>$v1){
if(is_array($v1)){
foreach($v1 as $k2=>$v2){
$res[$k1.'.'.$k2]=$v2;
}
}else{
$res[$k1]=$v1;
}
}
return $res;
}
?>
Try this:
<?php
$array=array("string",array(1,2,3,array('a','b','c')),true,"php");
$kt = array();
function showarray($arr,$k) {
global $kt;
foreach($arr as $key => $v) {
$nk = $k == '' ? $key:$k.'.'.$key;
if(is_array($v)) {
showarray($v,$nk);
} else {
$kt[$nk] = $v;
}
}
}
showarray($array,"");
print_r($kt);

Array inside assosiative array

I want to create an assosiative array which it will have as one of its elements an other array (not assosiative)
$temp= array(
'elem1' => $data1,
'elem2' => $data2,
'elem3' => $data5,
);
$data2 is a simple array already constructed. However, I cannot find the right syntax for doing that. By applying the classic following I cannot get the values of elem2.
while ($element=each($temp))
{
echo $element['key'];
echo " - ";
echo $element['value'];
echo "<br/>";
}
It's not quite clear from your question, but depending on the structure of $data2, printing its values separated by commas may suffice:
foreach ($temp as $key => $value) {
echo $key, ' - ';
if (is_array($value)) {
echo implode(',', $value);
}
else {
echo $value;
}
echo '<br/>';
}
try this
$data = ['elem1' => '1', 'elem2' => ['red','blue','orange' ],'elem3' => '3'];
$tmp ='';
foreach ($data as $key => $value ) {
$tmp .= $key.' - ' ;
if (is_array($value)) {
$tmp.= implode(',', $value).' ';
}else {
$tmp .= $value. ' ';
}
}
echo json_encode($tmp);
You can echo the values in this way:
foreach($temp as $key => $value) {
echo $key;
echo " - ";
if(is_array($value)) {
echo "Array: # ";
foreach($value as $value_key => $value_value) {
echo $value_key." - ".$value_value." # ";
}
} else {
echo $value;
}
echo "<br/>";
}

Limit number of sub_menu items to 4

I am trying to limit the number of sub_menu items (li) to maximum of 4. I'm no php developer but gave it a go with some code as provided below.
This is the existing code, which will just keep displaying it all, no limit set right now.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
foreach ($sub_menu_array as $sub_menu_row) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Here is what I tried but it ended up displaying nothing instead.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Syntax of PHP foreach statement has two different variants
foreach (array_expression as $value){
statement
}
foreach (array_expression as $key => $value)
statement
When you chanched code from
foreach ($sub_menu_array as $sub_menu_row) {
to
foreach ($sub_menu_array as $sub_menu_row => $v) {
You also changed values what appropriated to $sub_menu_row. For example:
$sub_menu_array = array ('a','b');
In the first variant at first iteration
$sub_menu_row=='a'
and in second variant
$sub_menu_row==0 #array's element key
$v=='a' #value
There are two solutions
Simply remove '=> $v'
Change $sub_menu_row to $v inside foreach statement
You have changed foreach, you should now use $v as a value.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($v['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
This should be enough for the menu part:
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
if($i < 4) {
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
$i++;
}
echo '</ul></nav>';
Insert this line before displaying the menu
$sliced_sub_menu_array = array_slice($sub_menu_array, 0, 4);
and then
foreach ($sliced_sub_menu_array as $sub_menu_row => $v) {
// displaying here
}
Use this code .
<?php
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if ($i == 3)
{
break;
}
$i++;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
?>

Categories