zblogPHP數(shù)組 php中數(shù)組

php中數(shù)組的定義有幾種方法及區(qū)別

數(shù)組在PHP中包含2種表現(xiàn),

網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)一個展示的機(jī)會來證明自己,這并不會花費(fèi)您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗(yàn),一切以用戶為中心。

一種是普通數(shù)組,定義方式有二種,

第一:

$a = array("aa","bb","cc")

第二:

$a[] = "aa";

$a[] = "bb"

$a[] = "cc";

一般用數(shù)組遍歷的方式可以打印出所有數(shù)據(jù),也可以使用數(shù)組的位標(biāo)(從0開始計(jì)算)自定義打印,比如:

$a[0]的值就是aa,$a[1]的值是"bb"

數(shù)組遍歷自己搜索下,也是2種方式

另外是別名數(shù)組,定義也是2種

第一:

$a = array("aa"= "11","bb"="22")

第二:

$a["aa"] = "11";

$a["bb"] = "22";

使用數(shù)組的位標(biāo)在別名數(shù)組就不行了,必須用別名

$a[0]就取不到值了,$a[“aa”]才能取到值"11"

數(shù)組遍歷其中foreach有點(diǎn)不一樣,自己搜索下

如何將php里面的數(shù)組內(nèi)容進(jìn)行分頁顯示出來?

1、前言

分頁顯示是一種非常常見的瀏覽和顯示大量數(shù)據(jù)的方法,屬于web編程中最常處理的事件之一。對于web編程的老手來說,編寫這種代碼實(shí)在是和呼吸一樣自然,但是對于初學(xué)者來說,常常對這個問題摸不著頭緒,因此特地撰寫此文對這個問題進(jìn)行詳細(xì)的講解,力求讓看完這篇文章的朋友在看完以后對于分頁顯示的原理和實(shí)現(xiàn)方法有所了解。本文適合初學(xué)者閱讀,所有示例代碼均使用php編寫。

2、原理

所謂分頁顯示,也就是將數(shù)據(jù)庫中的結(jié)果集人為的分成一段一段的來顯示,這里需要兩個初始的參數(shù):

每頁多少條記錄($PageSize)?

當(dāng)前是第幾頁($CurrentPageID)?

現(xiàn)在只要再給我一個結(jié)果集,我就可以顯示某段特定的結(jié)果出來。

至于其他的參數(shù),比如:上一頁($PreviousPageID)、下一頁($NextPageID)、總頁數(shù)($numPages)等等,都可以根據(jù)前邊這幾個東西得到。

以mysql數(shù)據(jù)庫為例,如果要從表內(nèi)截取某段內(nèi)容,sql語句可以用:select * from table limit offset, rows??纯聪旅嬉唤Msql語句,嘗試一下發(fā)現(xiàn)其中的規(guī)率。

前10條記錄:select * from table limit 0,10

第11至20條記錄:select * from table limit 10,10

第21至30條記錄:select * from table limit 20,10

……

這一組sql語句其實(shí)就是當(dāng)$PageSize=10的時候取表內(nèi)每一頁數(shù)據(jù)的sql語句,我們可以總結(jié)出這樣一個模板:

select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize

拿這個模板代入對應(yīng)的值和上邊那一組sql語句對照一下看看是不是那么回事。搞定了最重要的如何獲取數(shù)據(jù)的問題以后,剩下的就僅僅是傳遞參數(shù),構(gòu)造合適的sql語句然后使用php從數(shù)據(jù)庫內(nèi)獲取數(shù)據(jù)并顯示了。以下我將用具體代碼加以說明。

3、簡單代碼

請?jiān)敿?xì)閱讀以下代碼,自己調(diào)試運(yùn)行一次,最好把它修改一次,加上自己的功能,比如搜索等等。

?php

// 建立數(shù)據(jù)庫連接

$link = mysql_connect("localhost", "mysql_user", "mysql_password")

or die("Could not connect: " . mysql_error());

// 獲取當(dāng)前頁數(shù)

if( isset($_GET['page']) ){

$page = intval( $_GET['page'] );

}

else{

$page = 1;

}

// 每頁數(shù)量

$PageSize = 10;

// 獲取總數(shù)據(jù)量

$sql = "select count(*) as amount from table";

$result = mysql_query($sql);

$row = mysql_fetch_row($result);

$amount = $row['amount'];

// 記算總共有多少頁

if( $amount ){

if( $amount $page_size ) //如果總數(shù)據(jù)量小于$PageSize,那么只有一頁

if( $amount % $page_size ){ //取總數(shù)據(jù)量除以每頁數(shù)的余數(shù)

$page_count = (int)($amount / $page_size) + 1; //如果有余數(shù),則頁數(shù)等于總數(shù)據(jù)量除以每頁數(shù)的結(jié)果取整再加一

}else{

$page_count = $amount / $page_size; //如果沒有余數(shù),則頁數(shù)等于總數(shù)據(jù)量除以每頁數(shù)的結(jié)果

}

}

else{

$page_count = 0;

}

// 翻頁鏈接

$page_string = '';

if( $page == 1 ){

$page_string .= '第一頁|上一頁|';

}

else{

$page_string .= 'a href="/?page=1";第一頁/a|a href="/?page='."($page-1).'上一頁/a|';

}

if( ($page == $page_count) || ($page_count == 0) ){

$page_string .= '下一頁|尾頁';

}

else{

$page_string .= 'a href="/?page='."($page+1).'下一頁/a|a href="/?page='."$page_count.'尾頁/a';

}

// 獲取數(shù)據(jù),以二維數(shù)組格式返回結(jié)果

if( $amount ){

$sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";

$result = mysql_query($sql);

while ( $row = mysql_fetch_row($result) ){

$rowset[] = $row;

}

}else{

$rowset = array();

}

// 沒有包含顯示結(jié)果的代碼,那不在討論范圍,只要用foreach就可以很簡單的用得到的二維數(shù)組來顯示結(jié)果

?

4、OO風(fēng)格代碼

以下代碼中的數(shù)據(jù)庫連接是使用的pear db類進(jìn)行處理

?php

// FileName: Pager.class.php

// 分頁類,這個類僅僅用于處理數(shù)據(jù)結(jié)構(gòu),不負(fù)責(zé)處理顯示的工作

Class Pager

{

var $PageSize; //每頁的數(shù)量

var $CurrentPageID; //當(dāng)前的頁數(shù)

var $NextPageID; //下一頁

var $PreviousPageID; //上一頁

var $numPages; //總頁數(shù)

var $numItems; //總記錄數(shù)

var $isFirstPage; //是否第一頁

var $isLastPage; //是否最后一頁

var $sql; //sql查詢語句

function Pager($option)

{

global $db;

$this-_setOptions($option);

// 總條數(shù)

if ( !isset($this-numItems) )

{

$res = $db-query($this-sql);

$this-numItems = $res-numRows();

}

// 總頁數(shù)

if ( $this-numItems 0 )

{

if ( $this-numItems $this-PageSize )

if ( $this-numItems % $this-PageSize )

{

$this-numPages= (int)($this-numItems / $this-PageSize) + 1;

}

else

{

$this-numPages = $this-numItems / $this-PageSize;

}

}

else

{

$this-numPages = 0;

}

switch ( $this-CurrentPageID )

{

case $this-numPages == 1:

$this-isFirstPage = true;

$this-isLastPage = true;

break;

case 1:

$this-isFirstPage = true;

$this-isLastPage = false;

break;

case $this-numPages:

$this-isFirstPage = false;

$this-isLastPage = true;

break;

default:

$this-isFirstPage = false;

$this-isLastPage = false;

}

if ( $this-numPages 1 )

{

if ( !$this-isLastPage )

if ( !$this-isFirstPage )

}

return true;

}

/***

*

* 返回結(jié)果集的數(shù)據(jù)庫連接

* 在結(jié)果集比較大的時候可以直接使用這個方法獲得數(shù)據(jù)庫連接,然后在類之外遍歷,這樣開銷較小

* 如果結(jié)果集不是很大,可以直接使用getPageData的方式獲取二維數(shù)組格式的結(jié)果

* getPageData方法也是調(diào)用本方法來獲取結(jié)果的

*

***/

function getDataLink()

{

if ( $this-numItems )

{

global $db;

$PageID = $this-CurrentPageID;

$from = ($PageID - 1)*$this-PageSize;

$count = $this-PageSize;

$link = $db-limitQuery($this-sql, $from, $count); //使用Pear DB::limitQuery方法保證數(shù)據(jù)庫兼容性

return $link;

}

else

{

return false;

}

}

/***

*

* 以二維數(shù)組的格式返回結(jié)果集

*

***/

function getPageData()

{

if ( $this-numItems )

{

if ( $res = $this-getDataLink() )

{

if ( $res-numRows() )

{

while ( $row = $res-fetchRow() )

{

$result[] = $row;

}

}

else

{

$result = array();

}

return $result;

}

else

{

return false;

}

}

else

{

return false;

}

}

function _setOptions($option)

{

$allow_options = array(

'PageSize',

'CurrentPageID',

'sql',

'numItems'

);

foreach ( $option as $key = $value )

{

if ( in_array($key, $allow_options) ($value != null) )

{

$this-$key = $value;

}

}

return true;

}

}

?

?php

// FileName: test_pager.php

// 這是一段簡單的示例代碼,前邊省略了使用pear db類建立數(shù)據(jù)庫連接的代碼

require "Pager.class.php";

if ( isset($_GET['page']) )

{

$page = (int)$_GET['page'];

}

else

{

$page = 1;

}

$sql = "select * from table order by id";

$pager_option = array(

"sql" = $sql,

"PageSize" = 10,

"CurrentPageID" = $page

);

if ( isset($_GET['numItems']) )

{

$pager_option['numItems'] = (int)$_GET['numItems'];

}

$pager = @new Pager($pager_option);

$data = $pager-getPageData();

if ( $pager-isFirstPage )

{

$turnover = "首頁|上一頁|";

}

else

{

$turnover = "a href='?page=1numItems=".$pager-numItems."'首頁/a|a href="/?page=".$pager-PreviousPageID."numItems=".$pager-numItems."'上一頁/a|";

}

if ( $pager-isLastPage )

{

$turnover .= "下一頁|尾頁";

}

else

{

$turnover .= "a href="/?page=".$pager-NextPageID."numItems=".$pager-numItems."'下一頁/a|a href="/?page=".$pager-numPages."numItems=".$pager-numItems."'尾頁/a";

}

?

需要說明的地方有兩個:

這個類僅僅處理數(shù)據(jù),并不負(fù)責(zé)處理顯示,因?yàn)槲矣X得將數(shù)據(jù)的處理和結(jié)果的顯示都放到一個類里邊實(shí)在是有些勉強(qiáng)。顯示的時候情況和要求多變,不如自己根據(jù)類給出的結(jié)果處理,更好的方法是根據(jù)這個Pager類繼承一個自己的子類來顯示不同的分頁,比如顯示用戶分頁列表可以:

?php

Class MemberPager extends Pager

{

function showMemberList()

{

global $db;

$data = $this-getPageData();

// 顯示結(jié)果的代碼

// ......

}

}

/// 調(diào)用

if ( isset($_GET['page']) )

{

$page = (int)$_GET['page'];

}

else

{

$page = 1;

}

$sql = "select * from members order by id";

$pager_option = array(

"sql" = $sql,

"PageSize" = 10,

"CurrentPageID" = $page

);

if ( isset($_GET['numItems']) )

{

$pager_option['numItems'] = (int)$_GET['numItems'];

}

$pager = @new MemberPager($pager_option);

$pager-showMemberList();

?

第二個需要說明的地方就是不同數(shù)據(jù)庫的兼容性,在不同的數(shù)據(jù)庫里截獲一段結(jié)果的寫法是不一樣的。

mysql: select * from table limit offset, rows

pgsql: select * from table limit m offset n

......

所以要在類里邊獲取結(jié)果的時候需要使用pear db類的limitQuery方法。

ok,寫完收功,希望花時間看完這些文字的你不覺得是浪費(fèi)了時間。

回答者

另外,虛機(jī)團(tuán)上產(chǎn)品團(tuán)購,超級便宜

php怎樣把一個數(shù)組寫入一個文件

方法一:

//將一個測試的數(shù)組寫入一個PHP文件:

?php //要寫入PHP文件的數(shù)組 $write_array = array( '1' = 'oneone', '2'

= 'two', '3' = 'three', '4' = 'four','5' = 'five' );

//字符串處理 $string_start = "?php\n"; $string_process =

var_export($write_array, TRUE);$string_end = "\n?"; $string =

$string_start.$string_process.$string_end; //開始寫入文件

echofile_put_contents('test_array.php', $string); ?

這里用到了兩個函數(shù):

1,var_export():

·var_export — 用來輸出或返回一個變量的字符串表示,它和 var_dump() 的區(qū)別是,var_export()

可以用來返回關(guān)于傳遞給該函數(shù)的變量的結(jié)構(gòu)信息,并且其返回的表示是合法的 PHP 代碼如果 “echo

$string_process;”,則可以看到輸出結(jié)果:

array ( 1 = 'oneone', 2 = 'two', 3 = 'three', 4 = 'four', 5 = 'five', )

而它就是我們要寫入 test_array.php 文件的內(nèi)容(除去 php 標(biāo)簽);

·var_dump() 函數(shù)用來打印變量的相關(guān)信息,它只用來“打印”,而不會返回值,它的原型是 void var_dump(……),我們來 “var_dump($string_process);”,則可以看到輸出結(jié)果:

string(86) "array ( 1 = 'oneone', 2 = 'two', 3 = 'three', 4 = 'four', 5 = 'five', )"

可以看到輸出的string(86) “…”,再一次說明了 var_export() 返回的是一個字符串。

2,file_put_contents():

file_put_contents — 將一個字符串寫入文件,原型是 int file_put_contents ( string

filename, string data [, int flags [, resource context]]

),這里我們只用到了兩個參數(shù),”string filename”:要寫入的文件名;”string data”:字符串?dāng)?shù)據(jù);

此函數(shù)返回寫入到文件內(nèi)數(shù)據(jù)的字節(jié)數(shù),如果我們 “echo file_put_contents(’test_array.php’, $string);”,則會輸出一個整數(shù) :95。

因?yàn)檩敵龅?array() 占了 86 個字節(jié),還有的 $string_start 和 $string_end 又占了 9 個字節(jié),轉(zhuǎn)義字符 換行符 在這里只占 1 個字節(jié)。(不知道這樣解釋恰當(dāng)不恰當(dāng),還有望大家多多指正)

方法二:json_encode()

我們常見一些網(wǎng)站在做ajax時返回JSON格式的數(shù)據(jù):

返回的是json格式的數(shù)據(jù)返回的是json格式的數(shù)據(jù)

這有什么好處那?很顯然前端在接到返回的數(shù)據(jù)時可以直接使用,而不用再用eval_r('(+ returnString +)')或者 $.parseJSON(returnString ) (jQuery的函數(shù))來轉(zhuǎn)化為js對象,這樣顯然為用戶省電了。。。

在網(wǎng)上搜索了一下,這個問題在搜索中文信息的時候比較少,一些說是返回json的都是在前端進(jìn)行的轉(zhuǎn)化處理,根本不是返回JSON格式,其實(shí)返回json相當(dāng)?shù)暮唵巍?/p>

原來的數(shù)據(jù)就是JSON格式

下例來自《鋒利的jQuery》:

$(function(){

$('#send').click(function() {

$.getJSON('', function(data) {

$('#resText').empty();

var html = '';

$.each( data , function(commentIndex, comment) {

html += 'div class="comment"h6' +

comment['username'] + ':/h6p class="para"' +

comment['content'] + '/p/div';

})

$('#resText').html(html);

})

})

})

你需要做的就是將數(shù)據(jù)存儲為格式正確的 .json或者.js 文件。以下為示例所傳送的json格式的數(shù)據(jù)

[

{

"username": "張三",

"content": "沙發(fā)."

},

{

"username": "李四",

"content": "板凳."

},

{

"username": "王五",

"content": "地板."

}

]

php輸出JSON格式

那么php如何輸出json格式?php 使用json_encode函數(shù),然后jQuery使用datatype:json 就可以了嘛? 它的輸出如下:

php 使用json_encode函數(shù),jQuery使用datatype:json的返回類型php 使用json_encode函數(shù),jQuery使用datatype:json的返回類型

顯然并非所愿。還是字符串,到底怎么實(shí)現(xiàn)?其實(shí)很簡單,只要在php文件頭部加入以下代碼:

header('Content-type: text/json');

這個頭就是告知此文件輸出類型為 json,這種形式我們見的最多的是驗(yàn)證碼——php輸出驗(yàn)證圖片,有時php可以輸出css文件,js文件等做一些有趣的事情。好的,我們測試一下吧。查看示例

示例代碼:

?php

header('Content-type: text/json');

$fruits = array (

"fruits" = array("a" = "orange", "b" = "banana", "c" = "apple"),

"numbers" = array(1, 2, 3, 4, 5, 6),

"holes" = array("first", 5 = "second", "third")

);

echo json_encode($fruits);

?

本文題目:zblogPHP數(shù)組 php中數(shù)組
轉(zhuǎn)載來源:http://bm7419.com/article42/dohjjec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站改版網(wǎng)站制作、網(wǎng)站建設(shè)動態(tài)網(wǎng)站、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站