Jump to content
php.lv forumi

Salīdzināt Variabli ar Array


Bunkertor 7
 Share

Recommended Posts

Sveiki!

 

Es te eksperimentēju ar Array. Ideja ir tāda, ka skripts saņem inputu četrzīmju numura veidā. Sadala pa cipariem (to momentu jau es esmu pieveicis) un tad nosauc tos ciparus vārdā. Piemēram, ja inputs ir 4582, tad outputs ir four five eight two.

Iespējams, ka es mēģinu sarežģīt lietas, bet:

$inputnum = "4582";
$num1 = substr($num,0,1);
$num2 = substr($num,1,1);
$num3 = substr($num,2,1);
$num4 = substr($num,3,1);

$matchbox = array(1=>'one', 2=>'two', 3=>'three', 4=>'four', 5=>'five', 6=>'six', 7=>'seven', 8=>'eight', 9=>'nine', 0=>'zero');
foreach ($matchbox as $num1=>$numerals) {
preg_match ($num1, $matchbox);
if (count($matchbox) > 0) {
echo $numerals;
}
}

 

Vai tas ir pareizais virziens, vai labāk domāt ko citu?

 

Paldies!

Link to comment
Share on other sites

Nez, imo var vienkāršāk

 

$input = "12345678354w634"; //"neierobežotā" garumā šis var būt
$matchbox = array('zero','one','two','three','four','five','six','seven','eight','nine'); //pirmais masīva elements ir ar indeksu 0, tālāk 1,2,3...

$arr = str_split($input);
foreach($arr as $item){
echo $matchbox[$item];
}

Edited by briedis
Link to comment
Share on other sites

Atkal kaut kas nedarbojas. Mēģinu pielāgot šo skriptu, lai viņš zīmē cipariņus :) Sanāk tā, ka man viens foreach ir iekš otra... pareizāk sakot, nesanāk

 

<?php
$cardnum = "4586";

 $matchbox = array('Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'); 
 $xcoords = array(100, 120, 130, 140);


foreach ($xcoords as $value)
 {
$arr = str_split($cardnum);
foreach($arr as $item
 {
$output = $matchbox[$item];
ImageString($image, 4, $xcoords[$value], 100, $output, $blue);
 }
 }
?>

 

Respektīvi, viņam vajadzētu zīmēt tos cipariņus ar 10 pikseļu atstatumu, bet viņš nezīmē. Varbūt kādam ir kāda laba ideja.

Link to comment
Share on other sites

uz to pusi, pārbaudi pats.

$image = imagecreate(300, 300);
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 255);

$input = "1478";
$matchbox = array('zero','one','two','three','four','five','six','seven','eight','nine');
$arr = str_split($input);
$xcoords = array(10, 90, 170, 240);

foreach($arr as $k=>$item) {
$value = $matchbox[$item];
ImageString($image, 4, $xcoords[$k], 100, $value, $textcolor);
};

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...