導航:首頁 > 編程語言 > phparraymax

phparraymax

發布時間:2022-04-18 13:41:35

『壹』 用php寫函數,比較a,b,c三個數,輸出其中最大的一個

echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5

-------
abc 是字元串,沒辦法比較大小的,如果應要比較也只能把數值給變數,例:
$a=10;
$b=20;
$c=30;
echo max($a,$b,$c); //說白了 這比較的還是10,20,30。

『貳』 PHP array max min (返值不正確,我已器死在廁所)

因為你組裝數組的時候就沒寫對啦,往裡面放元素為什麼要拼接換行符<br/>,看下圖,上一行是運行正常的結果,下一行就是像你那樣錯誤的結果

『叄』 PHP數組間的配對 如有兩個數組$a=array(1,2,3) $b=array(1,2,3) 想將兩個數組間的數隨機相乘,怎麼辦

如果你是想把a數組的每個元素和b數組的每個元素相乘,然後輸出結果小於3或者大於6的元素的話,可以用以下代碼,數據長度和元素以及輸出的結果條件可以調的
$a=array(1,2,3);
$b=array(1,2,3);
for($i=0;$i<count($a);$i++)
{
for($j=0;$j<count($b);$j++)
{
if( $a[$i]*$b[$j] < 3 || $a[$i]*$b[$j] > 6 )
{
echo "find:"."a".$i."*b".$j."=".$a[$i]*$b[$j]."<br/>";
}
}
}

8層嵌套是什麼意思,問題有那麼復雜嗎,可以抽絲剝繭簡化的

『肆』 PHP里獲取一維數組里的最大值和最小值要求,效率最好,速度最快

還有什麼會比PHP的方法高效?

最大的

<?php
$a=array('1','3','55','99');
$pos=array_search(max($a),$a);
echo$a[$pos];

最小的

<?php
$a=array('1','3','55','99');
$pos=array_search(min($a),$a);
echo$a[$pos];

『伍』 php中如何求出數組中重復個數最多的那個值

使用array_count_values()函數

print_r(array_count_values($array));

暈,都到這一步了。。。

用asort()對得到的數組排序,

再用array_search()輸出數值最大的鍵名。

『陸』 編寫函數,用指針法求某指針指向的數組中10個元素的最大值和最小值,並存入另外兩個指針變數指向的變數

#include"stdio.h"
#include<stdlib.h>
#include"time.h"
#defineN10
voidmaxmin(int*p,int*pmax,int*pmin){
inti;
for(*pmax=*pmin=p[i=0];i<N;i++){
if(p[i]>*pmax)
*pmax=p[i];
if(p[i]<*pmin)
*pmin=p[i];
}
}
intmain(intargc,char*argv[]){
intarray[N],i,max,min;
srand((unsigned)time(NULL));
for(i=0;i<N;printf("%d",array[i++]=rand()%100));
printf(" ");
maxmin(array,&max,&min);
printf("MAX=%d MIN=%d ",max,min);
return0;
}

運行樣例:

『柒』 在一個php數組中,裡面有(78.65.28.35等)求最大值和最小值,(不能用max等函數)

掃描一遍就可以獲得最大值、最小值、平均值,掃描數組使用foreach,下面是例子代碼:

<?php
$arr=array(78,65,28,35);
$max=$arr[0];
$min=$arr[0];
$sum=0;
$num=0;
foreach($arras$x){
if($x>$max)$max=$x;
if($x<$min)$min=$x;
$sum+=$x;
$num++;
}
$avg=$sum/$num;
echo"最大值{$max},最小值{$min},平均值{$avg}";
?>

『捌』 PHP中圖像處理怎麼寫一個折線統計圖

在PHP中,有一些簡單的圖像函數是可以直接使用的,但大多數要處理的圖像,都需要在編譯PHP時加上GD庫。除了安裝GD庫之外,在PHP中還可能需要其他的庫,這可以根據需要支持哪些圖像格式而定。GD庫可以網上免費下載,不同的GD版本支持的圖像格式不完全一樣,最新的GD庫版本支持GIF、JPEG、PNG、WBMP、XBM等格式的圖像文件,此外還支持一些如FreeType、Type 1等字體庫。通過GD庫中的函數可以完成各種點、線、幾何圖形、文本及顏色的操作和處理,也可以創建或讀取多種格式的圖像文件。
在PHP中,通過GD庫處理圖像的操作,都是先在內存中處理,操作完成以後再以文件流的方式,輸出到瀏覽器或保存在伺服器的磁碟中。創建一個圖像應該完成如下所示的4個基本步驟。
(1)創建畫布:所有的繪圖設計都需要在一個背景圖片上完成,而畫布實際上就是在內存中開辟的一塊臨時區域,用於存儲圖像的信息。以後的圖像操作都將基於這個背景畫布,該畫布的管理就類似於我們在畫畫時使用的畫布。
(2)繪制圖像:畫布創建完成以後,就可以通過這個畫布資源,使用各種畫像函數設置圖像的顏色、填充畫布、畫點、線段、各種幾何圖形,以及向圖像中添加文本等。
(3)輸出圖像:完成整個圖像的繪制以後,需要將圖像以某種格式保存到伺服器指定的文件中,或將圖像直接輸出到瀏覽器上顯示給用戶。但在圖像輸出之前,一定要使用header()函數發送Content-type通知瀏覽器,這次發送的是圖片不是文本。
(4)釋放資源:圖像被輸出以後,畫布中的內容也不再有用。出於節約系統資源的考慮,需要及時清除畫布佔用的所有內存資源。
php中用GD繪制折線圖,代碼如下:
Class Chart{
private $image; // 定義圖像
private $title; // 定義標題
private $ydata; // 定義Y軸數據
private $xdata; // 定義X軸數據
private $seriesName; // 定義每個系列數據的名稱
private $color; // 定義條形圖顏色
private $bgcolor; // 定義圖片背景顏色
private $width; // 定義圖片的寬
private $height; // 定義圖片的長
/*
* 構造函數
* String title 圖片標題
* Array xdata 索引數組,X軸數據
* Array ydata 索引數組,數字數組,Y軸數據
* Array series_name 索引數組,數據系列名稱
*/
function __construct($title,$xdata,$ydata,$seriesName) {
$this->title = $title;
$this->xdata = $xdata;
$this->ydata = $ydata;
$this->seriesName = $seriesName;
$this->color = array('#DC', '#B', '#EDB', '#DDDF', '#CBE', '#E', '#FF', '#FFF', '#AFC');
}
/*
* 公有方法,設置條形圖的顏色
* Array color 顏色數組,元素取值為'#DC'這種形式
*/
function setBarColor($color){
$this->color = $color;
}
/*
* 繪制折線圖
*/
public function paintLineChart() {
$ydataNum = $this->arrayNum($this->ydata); // 取得數據分組的個數
$max = $this->arrayMax($this->ydata); // 取得所有呈現數據的最大值
$max = ($max > )? $max : ;
$multi = $max/; // 如果最大數據是大於的則進行縮小處理
$barHeightMulti = .; // 條形高縮放的比例
$lineWidth = ;
$chartLeft = (+strlen($max))*; // 設置圖片左邊的margin
$lineY = ; // 初始化條形圖的Y的坐標
// 設置圖片的寬、高
//$this->width = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/.;
$margin = ; // 小矩形描述右邊margin
$recWidth = ; // 小矩形的寬
$recHeight = ; // 小矩形的高
$space = ; // 小矩形與條形圖的間距
$tmpWidth = ;
// 設置圖片的寬、高
$lineChartWidth = $lineWidth*count($this->xdata) + $chartLeft - $lineWidth/. ;
// 兩個系列數據以上的加上小矩形的寬
if($ydataNum > ) {
$tmpWidth = $this->arrayLengthMax($this->seriesName)**/ + $space + $recWidth + + $margin;
}
$this->width = $lineChartWidth + $tmpWidth;
$this->height = ;
$this->image = imagecreatetruecolor($this->width ,$this->height); // 准備畫布
$this->bgcolor = imagecolorallocate($this->image,,,); // 圖片的背景顏色
// 設置條形圖的顏色
$color = array();
foreach($this->color as $col) {
$col = substr($col,,strlen($col)-);
$red = hexdec(substr($col,,));
$green = hexdec(substr($col,,));
$blue = hexdec(substr($col,,));
$color[] = imagecolorallocate($this->image ,$red, $green, $blue);
}
// 設置線段的顏色、字體的顏色、字體的路徑
$lineColor = imagecolorallocate($this->image ,xcc,xcc,xcc);
$fontColor = imagecolorallocate($this->image, x,xf,xf);
$fontPath = 'font/simsun.ttc';
imagefill($this->image,,,$this->bgcolor); // 繪畫背景
// 繪畫圖的分短線與左右邊線
for($i = ; $i < ; $i++ ) {
imageline($this->image,$chartLeft-,$lineY-$barHeightMulti*$max//$multi*$i,$lineChartWidth,$lineY-$barHeightMulti*$max//$multi*$i,$lineColor);
imagestring($this->image,,,$lineY-$barHeightMulti*$max//$multi*$i-,floor($max/*$i),$fontColor);
}
imageline($this->image,$chartLeft-,,$chartLeft-,$lineY,$lineColor);
imageline($this->image,$lineChartWidth-,,$lineChartWidth-,$lineY,$lineColor);
$style = array($lineColor,$lineColor,$lineColor,$lineColor,$lineColor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor,$this->bgcolor);
imagesetstyle($this->image,$style);
// 繪制折線圖的分隔線(虛線)
foreach($this->xdata as $key => $val) {
$lineX = $chartLeft + + $lineWidth*$key;
imageline($this->image,$lineX,,$lineX,$lineY,IMG_COLOR_STYLED);
}
// 繪畫圖的折線
foreach($this->ydata as $key => $val) {
if($ydataNum == ) {
// 一個系列數據時
if($key == count($this->ydata) - ) break;
$lineX = $chartLeft + + $lineWidth*$key;
$lineY = $lineY-$barHeightMulti*($this->ydata[$key+])/$multi;
// 畫折線
if($key == count($this->ydata) - ) {
imagefilledellipse($this->image,$lineX+$lineWidth,$lineY,,,$color[]);
}
imageline($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,$lineX+$lineWidth,$lineY,$color[]);
imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$val/$multi,,,$color[]);
}elseif($ydataNum > ) {
// 多個系列的數據時
foreach($val as $ckey => $cval) {
if($ckey == count($val) - ) break;
$lineX = $chartLeft + + $lineWidth*$ckey;
$lineY = $lineY-$barHeightMulti*($val[$ckey+])/$multi;
// 畫折線
if($ckey == count($val) - ) {
imagefilledellipse($this->image,$lineX+$lineWidth,$lineY,,,$color[$key%count($this->color)]);
}
imageline($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,$lineX+$lineWidth,$lineY,$color[$key%count($this->color)]);
imagefilledellipse($this->image,$lineX,$lineY-$barHeightMulti*$cval/$multi,,,$color[$key%count($this->color)]);
}
}
}
// 繪畫條形圖的x坐標的值
foreach($this->xdata as $key => $val) {
$lineX = $chartLeft + $lineWidth*$key + $lineWidth/ - ;
imagettftext($this->image,,-,$lineX,$lineY+,$fontColor,$fontPath,$this->xdata[$key]);
}
// 兩個系列數據以上時繪制小矩形及之後文字說明
if($ydataNum > ) {
$x = $lineChartWidth + $space;
$y = ;
foreach($this->seriesName as $key => $val) {
imagefilledrectangle($this->image,$x,$y,$x+$recWidth,$y+$recHeight,$color[$key%count($this->color)]);
imagettftext($this->image,,,$x+$recWidth+,$y+$recHeight-,$fontColor,$fontPath,$this->seriesName[$key]);
$y += $recHeight + ;
}
}
// 繪畫標題
$titleStart = ($this->width - .*strlen($this->title))/;
imagettftext($this->image,,,$titleStart,,$fontColor,$fontPath,$this->title);
// 輸出圖片
header("Content-Type:image/png");
imagepng ( $this->image );
}
/*
* 私有方法,當數組為二元數組時,統計數組的長度
* Array arr 要做統計的數組
*/
private function arrayNum($arr) {
$num = ;
if(is_array($arr)) {
$num++;
for($i = ; $i < count($arr); $i++){
if(is_array($arr[$i])) {
$num = count($arr);
break;
}
}
}
return $num;
}
/*
* 私有方法,計算數組的深度
* Array arr 數組
*/
private function arrayDepth($arr) {
$num = ;
if(is_array($arr)) {
$num++;
for($i = ; $i < count($arr); $i++){
if(is_array($arr[$i])) {
$num += $this->arrayDepth($arr[$i]);
break;
}
}
}
return $num;
}
/*
* 私有方法,找到一組中的最大值
* Array arr 數字數組
*/
private function arrayMax($arr) {
$depth = $this->arrayDepth($arr);
$max = ;
if($depth == ) {
rsort($arr);
$max = $arr[];
}elseif($depth > ) {
foreach($arr as $val) {
if(is_array($val)) {
if($this->arrayMax($val) > $max) {
$max = $this->arrayMax($val);
}
}else{
if($val > $max){
$max = $val;
}
}
}
}
return $max;
}
/*
* 私有方法,求數組的平均值
* Array arr 數字數組
*/
function arrayAver($arr) {
$aver = array();
foreach($arr as $val) {
if(is_array($val)) {
$aver = array_merge($aver,$val);
}else{
$aver[] = $val;
}
}
return array_sum($aver)/count($aver);
}
/*
* 私有方法,求數組中元素長度最大的值
* Array arr 字元串數組,必須是漢字
*/
private function arrayLengthMax($arr) {
$length = ;
foreach($arr as $val) {
$length = strlen($val) > $length ? strlen($val) : $length;
}
return $length/;
}
// 析構函數
function __destruct(){
imagedestroy($this->image);
}
}
測試代碼如下:
$xdata = array('測試一','測試二','測試三','測試四','測試五','測試六','測試七','測試八','測試九');
$ydata = array(array(,,,,,,,,),array(,,,,,,,,));
$color = array();
$seriesName = array("七月","八月");
$title = "測試數據";
$Img = new Chart($title,$xdata,$ydata,$seriesName);
$Img->paintLineChart();
效果圖如下:
到此代碼結束。
下面給大家介紹php中GD庫的一些簡單使用
今天了解了一些GD庫的簡單使用,現在稍微做一下總結!
GD庫是什麼?,graphic device,圖像工具庫,gd庫是php處理圖形的擴展庫,gd庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片。 在網站上 GD庫通常用來生成縮略圖或者用來對圖片加水印或者對網站數據生成報表。
php並不局限於輸出HTML文本。php通過使用GD擴展庫還能用來動態輸出圖像,例如文字按鈕、驗證碼、數據統計圖等。哈可以輕松地編輯圖像,力圖處理縮略圖和為圖片添加水印等,具有強大的圖像處理能力。
首先我們來說下GD庫,繪制個簡單圖形的一些步驟:
1、首先是創建畫布,此處我們利用imagecreatetruecolor函數,也可以利用imagecreate,區別在於前者創建了一個真彩圖像,後者創建了一個基於調色板的圖像
$img=imagecreatetruecolor(100,100),其中有兩個參數分別對應,我們創建的圖像的寬和高
2、設置一些必要的"染料盒"
其實就是定義一些之後會用到的填充顏色,此處我們統一定義在這個位置,此處我們利用imagecolorallocate函數
$white=imagecolorallocate($img,0xFF,0xFF,0xFF)或者可以使用RGB的顏色命名方式 如$white=imagecolorallocate($img,255,255,255);
$gray = imagecolorallocate($img, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($img, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($img, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($img, 0x00, 0x00, 0x50);
$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($img, 0x90, 0x00, 0x00);
$black=imagecolorallocate($img,0x00,0x00,0x00);
此處我們定義多一些所需要的顏色
3、填充區域顏色,可以簡單的理解為填充圖片的背景顏色,利用imagefill函數
imagefill($img,0,0,$white),此處的0 0表示從坐標x y處開始填充背景色
4、繪制圖形,例如繪制餅狀圖,所需要的是imagefilledarc函數
imagefilledarc()的參數相對來說較多,形如imagefilledarc($img,50,$i,100,50,0,45,$red,IMG_ARC_PIE);
其中分別表示以red顏色字img圖像上繪制一個以50,$i為起點,以0 45角度這個范圍內繪制弧線
5、期間我們還可以添加一些說明問題,比如水平的添加一個字元串,利用 imagestring($img,1,20,40,"hello,world!",$red),表示在img圖片中以20 40為坐標,寫上一個紅色的hello,world!字樣
6、就是講圖像輸出
首先要告之瀏覽器要以何種圖片格式輸出,例如以png輸出,則使用header("Content-type:image/png");
其次 將圖片輸出到瀏覽器中,imagepng($img);
最後,銷毀圖片,即釋放該圖片存儲所佔用的內存 imagedestroy(img);,

『玖』 例如數組:Array ( [34] => 2 [7] => 100 ) PHP 如何獲取值最大的鍵值 這個例子中應該返回7

<?php
$arr=array(34=>2,7=>100);//定義一個數組
$t=max($arr);//獲取最大的值
$brr=array_flip($arr);//將數組鍵與值互換
echo $brr[$t];//輸出結果,即為原數組最大值的鍵
?>

『拾』 php 數組

<?php
$sum=0; //總和
$avg = 0; //平均值
$arrs=[34,88,67,91,82,55,73];
//$arrs2 = array();
//unset ?
$maxInt = array_search(max($arrs),$arrs);
$minInt = array_search(min($arrs), $arrs);
unset($arrs[$maxInt]);
unset($arrs[$minInt]);
for($i=0; $i<count($arrs); $i++){
$sum += $arrs[$i];
}
$avg = $sum/count($arrs);
$arrs = array_values($arrs); //重新索引
for($i=0; $i<count($arrs); $i++) {
$arrs2[]=abs($avg-$arrs[$i]);
}
$maxInt2 = array_search(max($arrs2),$arrs2);
$minInt2 = array_search(min($arrs2),$arrs2);
//echo $arrs[$maxInt2].';'.$arrs[$minInt2];
echo '平均值為'.$avg.';與平均值最不接近的值為'.$arrs[$maxInt2].';與平均值最接近的值為'.$arrs[$minInt2];
?>

閱讀全文

與phparraymax相關的資料

熱點內容
安卓qq瀏覽器怎麼轉換到ios 瀏覽:292
不同編譯器的庫可以調用嗎 瀏覽:455
灰度信託基金加密 瀏覽:421
宿遷程序員兼職網上接單 瀏覽:924
電視編譯器怎麼設置 瀏覽:276
手機如何解壓漢字密碼的壓縮包 瀏覽:701
為什麼很多程序員愛用vim 瀏覽:828
安卓手機怎麼連接寶華韋健音響 瀏覽:555
12星座製作解壓球 瀏覽:867
java調用oracle數據 瀏覽:827
怎麼在伺服器上上傳小程序源碼 瀏覽:304
空中加油通達信指標公式源碼 瀏覽:38
分卷解壓只解壓了一部分 瀏覽:760
php網站自動登錄 瀏覽:705
合肥凌達壓縮機招聘 瀏覽:965
怎麼找到文件夾的圖標 瀏覽:237
linuxc編程pdf百度雲 瀏覽:192
會計pdf下載 瀏覽:835
c開源cf源碼 瀏覽:951
如何取消掉添加進app資源庫 瀏覽:732