array of hex values like range('a','z') - php

PHP has the function range() which can be used to loop through letters a-z. Is there something similar or a similar function that can let me loop through hex values 0-9 + a-f?

array_merge( range(0,9), range('a','f') )

I think you mean ASCII value equivalents, you can do:
$merged_result=array_merge(range('0','9'),range('a','f'));

The numbers don't actually change, just how you are outputting them. So something like:
for (range(0,15) as $value) {
$value = base_convert($value,10,16);
}

No need to do that loop from 0-15. dechex($yournumber).
0x0 = 0
0xF = 15
for($i = 0; $i < 15; $i++) {
print dechex($i);
}

Related

Compare string from input field with value in multidimension array

$klasseinput = strtoupper(trim($_POST["klasseliste"]));
$data = file('student.txt');
$data = array_filter($data);
foreach($data AS $row){
$student[] = explode(';', $row);
}
$antall = count($student);
for ($i = 0; $i < $antall; $i++){
if($klasseinput == $student[$i][3]){
print('<tr><td>'.$student[$i][0]."</td><td>".$student[$i][1]."</td><td>".$student[$i][2]."</td><td>".$student[$i][3]."</td></tr>");
}
}
/////////STUDENT.txt//////////
ph;petter;hanssen;IT1
gb;Geir;Bjarvin;IT2
mj;Marius;Johansen;IT3
/////////////////////////////
I am trying to compare an input form with an item in the multidimension array, but even tho the variable from the input field is exactly the same as the value in the array, it doesnt pass the if check.
$student[0][3] = IT1
$student[1][3] = IT2
$student[2][3] = IT3
If you have made sure there is no white space spoiling the comparison, then you might find a function like this useful to look at the strings on both sides of the comparison. You might find there are spurious characters causing trouble.
function hexdump($str)
{
for($i=0; $i<strlen($str);$i++)
{
echo "[$i] [".bin2hex($str[$i])."] [".$str[$i]."]<br />";
}
}
For instance, the string read from the file might contain CR LF characters. You could get rid of them using str_replace().
Thanks to Ravinder Reddy for the answer that was simple and worked for me:
" trim the value for \t\n if($klasseinput == trim($student[$i][3])){ "

How can I make an array of all lists length x of the numbers 0 and 1?

I need to create an array like this:
$array = array(array(1,1,1,0,0,0,1,1), array(1,1,1,1,0,0,1,0));
but with all combinations of 1 and 0.
I wish to do this automatically, so I was thinking a for loop would be the best idea.
In other words the inner arrays should be all combos like 0,0,0,0,0,0,0,0 then 0,0,0,0,0,0,0,1 then 0,0,0,0,0,0,1,0 then 0,0,0,0,0,0,1,1. for all combos.
I started like this:
$array = array();
for($i =0;$i<100; $i++){
$array[$i] = 0;
}
How do I get this to do what I am trying to do?
Use decbin to convert your counter to binary.
str_pad allows you to pad strings, in this case I'm 0-padding it to a length of 8 bits.
$array = array();
for($i =0;$i<100; $i++){
$array[$i] = str_pad(decbin($i), 8, "0", STR_PAD_LEFT);;
}
var_dump($array);
Demo
This will print out an array of all binary variations from 00000000 to 11111111 where each one is in it's own array of chars.
$array = array();
for ($i = 0; $i < 256;)
{
$array[] = str_split(sprintf('%08d', decbin($i++)));
}
print_r($array);
See example

CSV parse + multidim arrays

59516,?O?L?h,27,80,67,13,1,84.6,40,4,1,32632,??,3,5,32640,AES,3,5,32210,\
???i?Z??,1,1.2,32263,?,1,1.2,3?T,1,1.2,32104,,1,1,40012,??,1,0.8
How would I explode/list this line and pull any field that contains a five-character number and the field immediately preceding it?
i.e. $data['59516']['i'] would contain an array with
(32632=>1, 32640=>5, 32210=>5, 32263=>1.2,32104=>2, 40012=>1)
Not sure i fully understand your question, but it sounds like you want something like:
$items = str_getcsv($commaSeparatedList);
for ($i = 0; $i < count($items); $i++) {
if (strlen($items[$i]) == 5 && is_numeric($items[$i])) {
var_dump($items[$i]); // current match
if (isset($items[$i-1])) {
var_dump($items[$i-1]); // item and one before it
}
}
}
(?<=,)(\d+),(\d{5})
The $matches[1] has the value and the $matches[2] has your key.

Trying to write a simple alphabet program with php

Im trying to spit out each letter of the alphabet from an array on a single line, A-Z.
This is what my code looks like so far:
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
while ($alphabet) {
echo $alphabet;
$alphabet;
}
Im kinda stuck at this part and not quite sure what else to write to make this work. Any suggestions?
Use range and array_walk:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
Working example: http://codepad.org/pedjOlY9
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach($alphabet as $letter) {
echo $letter;
}
I am not sure why you need the array...
This is why we have the ASCII code.
You can do is like this:
for ($i = 65; $i <=90; $i++)
{
echo chr($i) . PHP_EOL;
}
chr() displays the character in the ASCII map - check it here: http://www.danshort.com/ASCIImap/. If you want to do lowercase - just use strtolower() or the numbers between 97-122 instead. PHP_EOL is a built in constant that outputs end of line. You can change it with ."" if you are doing HTML.
I think range is a little bit longer to be done, but it still works.
This might be help full for you
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$c = sizeof($alphabet);
for($i= 0; $i < $c ; $i++) {
echo $alphabet[$i];
}
and you can use count($alphabet) instead of sizeof() built-in function

Leading zeroes in PHP

I would like to present a list from 0 to 59 with the numbers 0 to 9 having a leading zero. This is my code, but it doesn't work so far. What is the solution?
for ($i=0; $i<60; $i++){
if ($i< 10){
sprintf("%0d",$i);
}
array_push($this->minutes, $i);
}
Using %02d is much shorter and will pad the string only when necessary:
for($i=0; $i<60; $i++){
array_push($this->minutes,sprintf("%02d",$i));
}
You are not assigning the result of sprintf to any variable.
Try
$padded = sprintf("%0d", $i);
array_push($this->minutes, $padded);
Note that sprintf does not do anything to $i. It just generates a string using $i but does not modify it.
EDIT: also, if you use %02d you do not need the if
Try this...
for ($i = 0; $i < 60; $i++) {
if ($i < 10) {
array_push($this->minutes, sprintf("%0d", $i));
}
array_push($this->minutes, $i);
}
You are ignoring the returned value of sprintf, instead of pushing it into your array...
important: The method you are using will result in some items in your array being strings, and some being integers. This might not matter, but might bite you on the arse if you are not expecting it...
Use str_pad:
for($i=0; $i<60; $i++){
str_pad($i, 2, "0", STR_PAD_LEFT)
}
I like the offered solutions, but I wanted to do it without deliberate for/foreach loops. So, here are three solutions (subtle variations):
Using array_map() with a designed callback function
$array = array_map(custom_sprintf, range(0,59));
//print_r($array);
function custom_sprintf($s) {
return sprintf("%02d", $s);
}
Using array_walk() with an inline create_function() call
$array = range(0,59);
array_walk($array, create_function('&$v', '$v = sprintf("%02d", $v);'));
// print_r($array);
Using array_map() and create_function() for a little code golf magic
$array = array_map(create_function('&$v', 'return sprintf("%02d", $v);'), range(0,59));

Categories