Number of indexes in an Array [duplicate] - php

This question already has answers here:
php - Is it possible to count the number of keys in an array?
(4 answers)
Closed 8 years ago.
I wish to count the number of indexes my new array have.
Below is a screenshot of my code. The values within the violet box is my output. My $High array lists down the values from my accu.php and new.php (within the require).
Now, want to list the unique values of my $High array using my new array, named $result. Then, I want to count the indexes of my $result array. I have tried the count() and sizeof() function and it still displays 9 as seen below.
Below is my code:
<?php
require("accu.php");
require("new.php");
$High = array ($Accu_High1,$Accu_High2,$Accu_High3,$Accu_High4,$Accu_High5,$new1,$new2,$new3,$new4,$new5,$new6,$new7,$new8,$new9,$new0);
$result = array_unique($High); // remove similar
$count = sizeof($result); //count
for ($int = 0; $int<=14; $int++)
{
echo "<b>$int</b> $High[$int]<br>";
}
echo "<br><br><br><b>$count</b>";
?>
And below is my output:
0 22
1 32
2 33
3 32
4 32
5 30
6 31
7 31
8 31
9 30
10 23
11 30
12 31
13 30
14 30
9

Array indexes are unique. So count($array) is all you need to count the 'indexes'.
But, I'm not sure this question display minimal understanding of PHP arrays. The PHP online manual or a book are the answer here. Like how foreach works and such trivial matters.
So use var_dump against your array and analyze the output. Then see what's actually going on. There's many array functions/functionality and you should know them.

Related

PHP write a number out of an array

I have a PHP problem.
I need to write a number from a sets of digits 0-9. Each set has 10 digits, each digit once.
I need to count the number of sets that I have to use to write the number.
For example number 10 is written from one set, but number 300 uses 2 sets because it has two zeros.
But, the problem is that 6 and 9 are considered the same. They can be rotated by 180 degrees.
Number 266 will be using one set, 369 also is using one set, but 5666 is using 2 sets.
I would be very grateful if you could somehow help me.
Here is how I have started and stuck up, have no more clue how to loop through it. Tried many things, nothing successful.
<?php
function countSet($num) {
$array = str_split($num);
$statarr = [0,1,2,3,4,5,6,7,8,9];
$a1 = $array; $a2 = $statarr;
$result = array_intersect($a1,$a2);
$count = array_count_values($result); }
?>
If you just need to know how many sets you need for a number, you can solve by counting the numbers. For the case of the 9, just replace every 9 by a 6 and divide the number of 6 by two. Something like this (sorry if there's any syntax error, I'm on mobile):
function countSet($input) {
// Convert the input into a string and replace every 9 by a 6. Then convert it to array
$numbers = str_split(str_replace("9", "6", strval($input)));
// Count occurrences for each number
$usedNumbers = array_count_values($numbers);
// If we have a 6, must divide it by 2 (6 represents 6 and 9
if (array_key_exists("6", $usedNumbers)) {
$usedNumbers["6"] = ceil($usedNumbers["6"] / 2);
}
// Now we just need to know the max number of occurrences for a single number as our result.
return max($usedNumbers);
}
See an online demo here

How to get top 3 values from array in PHP [duplicate]

This question already has answers here:
Returning first x items from array
(5 answers)
How to remove duplicate values from an array in PHP
(26 answers)
Getting first 3 values of an array in PHP
(3 answers)
Get the first N elements of an array?
(5 answers)
Closed 2 years ago.
I have a sorted array in descending order such as with values 100 , 98, 96, 90 ....
and I am using foreach() loop to iterate over the array and use if condition with limit 3 such as `
foreach($array as $arr){
if(loop_counter<3)
{
echo 'something';
}}
to get top 3 positions. but the problem is that if there exist two same values such as 100 , 98, 98, 96, 90 . . then limits should increase from 3 to 4 so that on position 2 there exist two values 98, 98 and position 3 contain value 90 instead of 2nd 98.
thanks in advance

I want to print numbers pattern program but it not give me wrong out put

I want to get my output like this
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
I am trying like this:
<?php
for($a=1; $a<=16; $a++)
{
for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
}
?>
The above code gives me the wrong output.
Let's debug.
You are starting from 1 in your outer loop and in your inner loop, you are going from $a till 1 times.
This doesn't comply with your requirements because we have to print an increasing sequence in each row.
You can also notice that every number in a row differs by 4.
So, logic would be like below:
Pseudocode:
rows = 4
starting_number = 1
loop from 1 to rows
number = starting_number
loop from 1 to 4 // since each row has 4 numbers
print number
number += 4
print new_line
starting_number++
Demo: https://3v4l.org/9YjIP

How to calculate sum of digits of a date in php? [duplicate]

This question already has answers here:
Get the sum of all digits in a numeric string
(13 answers)
Closed 8 months ago.
I've date in following format.
2019-02-27
I've to find sum of digits in above date. I've exploded the above value like this,
$date = '2019-02-27';
$dateArray = explode('-',$date);
Now date array will be like this:
array:3 [▼
0 => "2019"
1 => "01"
2 => "02"
]
With 2 loops, I can get sum of all digits but if there any much better way or builtin function to do this ?
Any kind of suggestion or help is appreciated.
You can use str_split to split the date into separate characters and then array_sum them. array_sum will ignore the - characters which are not valid numbers:
$date = '2019-01-02';
echo array_sum(str_split($date));
Output
15
Note that for your $date string of '2019-02-27' the correct result is 23. 15 is the correct result for '2019-01-02' which is what your var_dump output says was the contents of your $date variable at the time.
With 2 loops, I can get sum of all digits but if there any much better way or builtin function to do this ?
Yes
$total = array_sum(preg_split('//','2019-03-17'));
echo $total;
Output
23
Sandbox

reset key count in array

I have an array that is like this:
Array{
10 - 2011 Headlight Assembly Nissan Versa
11 - LH 07-11 INS QTLY O.E.M - FREE SAME DAY SHIPPING
12 - 000
13 - A0
14 - 40626A1
15 - $165 actual
16 - More Desc Stuff
}
that is produced from a simple dom result. There are multiple items within the list. What I would like to do is reset the key back to 10 after 17 is reached so that I can loop over all the results within the array and find the proper values without having to look for say keys 10, 14, 15 - and then keys 20, 24, 25, etc.
Not quite sure if I have explained it correctly, or how to accomplish it. Any guidance is appreciated. Thanks in advance!
Well, for the indexes to deny I suggest you tu use an array of these indexes. and to loop through this array from 10 to 16 and then come back to 10, I suggest you to do this
$indexes_to_deny = [14,15,16];
$index = 10;
while( condition to stop the loop )
{
if($index%17===0)
$index = 10;
if(in_array($index,$indexes_to_deny))
{
$index++;
continue;
}
/*
your code here
you can access the items inside the array with $array[$index]
*/
$index++:
}
My solution was to use array_slice to drop off the unnecessary items and then array_chuck the rest and finally just drop the rest at a break point.

Categories