php二維數組及三維數組
Ⅰ php 二維數組變三維數組
$a="上面的數組";
$b=array();
for($i=0;$i<count($a);$i++){
$b[$a[$i]['Id']]['Phone'][]=$a[$i]['Phone']
if(!empty($b[$a[$i]['Id']]['Id'])){
if($b[$a[$i]['Id']]['Id']==$a[$i]['Id'])continue;
}else{
$b[$a[$i]['Id']]['Key']=$a[$i]['Key'];
$b[$a[$i]['Id']]['Id']=$a[$i]['Id'];
$b[$a[$i]['Id']]['name']=$a[$i]['name'];
}
}
Ⅱ php 如何定義一個數組
php中定義數組的方法:x0dx0ax0dx0a1、PHP定義數組的格式:x0dx0a數組名=array();x0dx0a如:$aa=array();//這樣就定義了一個數組,x0dx0a之後給元素賦值:x0dx0a$aa[0]="9016";x0dx0a$aa[1]="9017";x0dx0a$aa[2]="9018";x0dx0ax0dx0a2、PHP輸出數組的方法:x0dx0aforeach($aa as $val)x0dx0a{x0dx0aecho$val;x0dx0a}x0dx0a也可以在定義數組時直接賦值x0dx0a$aa=array(0=>"9016",1=>"9017";2=>"9018");x0dx0ax0dx0a3、PHP的數組還可以用字元做下標,不一定要數字:x0dx0a$aa["name"]="Joan";x0dx0a$aa["num"]="9018";x0dx0a$aa["email"]="[email protected]";x0dx0a也可以這樣x0dx0a$aa=array("name"=>"joan","num"=>"9018","email"=>"[email protected]");x0dx0a將一個一維數組的元素也定義為數組,就是一個二維數組,x0dx0a$aa=array(0=>"a1",1=>"a2");x0dx0a$bb=array(0=>"b1",1=>"b2");x0dx0a$cc=array(0=>$aa;1=>$bb);此時,$cc[0]也是一個數組,$cc[1]也是一個數組,$cc就是一個二維數組。x0dx0a同理,三維,四維數組也可以繼續定義下去。x0dx0ax0dx0a4、數組的元素不僅於數字和字元串,可以是類的對象。
Ⅲ php如何實現一次性上傳多個文件
文件夾x0dx0aif(isset($_FILES['myfile'])){x0dx0a //由於 $_FILES['myfile'] 是個數組,所以需要使用循環遍歷x0dx0a for($i=0;$i<$max_files;$i++){x0dx0a //如果未出錯x0dx0a if($_FILES['myfile']['error'][$i]==0){x0dx0a if(move_uploaded_file($_FILES['myfile']['tmp_name'][$i],$up_folder."/".$_FILES['myfile']['name'][$i])){x0dx0a //成功上傳後,計數器增 1x0dx0a $up_ok_files +=1;x0dx0a }x0dx0a else{x0dx0a echo "
在伺服器中保存失敗
";x0dx0a }x0dx0a }x0dx0a }x0dx0a echo "成功上啟搏傳 ".$up_ok_files. " 個文件
"; x0dx0a}x0dx0a?>x0dx0ax0dx0a 如果對 $_FILES['myfile'] 這個數組的結構不清楚,那麼可以在循環之前加入這洞旁耐句代碼將其顯示出來:x0dx0aprint_r($_FILES['myfile']);x0dx0ax0dx0a 對這個數組的納春結構了解之後,理解上述代碼就比較容易了。x0dx0aforeach循環實現x0dx0a常規下,php上傳一張圖片,頁面上添加一個文件域,上傳後,得到的數組是二維數組x0dx0a轉變一下,x0dx0a頁面上添加多個文件域,並且他們的name屬性相同,而且是以數組形式提交(比如:name="file[]")x0dx0a那麼上傳後,得到的是三維數組x0dx0a而,php中,foreach循環對遍歷數組那真是完美至極x0dx0a用foreach遍歷一次這個三維數組,得到的結果就又是二維數組,那麼,就和上傳一張圖片一樣一樣的了Ⅳ php怎麼查看一個變數的佔用內存
我們在前面的php高效寫法提到,盡量不要復制變數,特別是數組。一般來說,PHP數組的內存利用率只有 1/10, 也就是說,一個在C語言裡面100M 內存的數組,在PHP裡面就要1G。下面我們可以粗略的估算PHP數組佔用內存的大小,首先我們測試1000個元素的整數佔用的內存:
[php] view plain print?
<?php
echo memory_get_usage() , '<br>';
$start = memory_get_usage();
$a = Array();
for ($i=0; $i<1000; $i++) {
$a[$i] = $i + $i;
}
$mid = memory_get_usage();
echo memory_get_usage() , '<br>';
for ($i=1000; $i<2000; $i++) {
$a[$i] = $i + $i;
}
$end = memory_get_usage();
echo memory_get_usage() , '<br>';
echo 'argv:', ($mid - $start)/1000 ,'bytes' , '<br>';
echo 'argv:',($end - $mid)/1000 ,'bytes' , '<br>';
輸出是:
353352
437848
522024
argv:84.416bytes
argv:84.176bytes
大概了解1000
個元素的整數數組需要佔用 82k 內存,平均每個元素佔用 84 個位元組。而純 C 中整體只需要 4k(一個整型佔用4byte * 1000
)。memory_get_usage() 返回的結果並不是全是被數組佔用了,還要包括一些 PHP
運行本身分配的一些結構,可能用內置函數生成的數組更接近真實的空間:
[php] view plain print?
<?php
$start = memory_get_usage();
$a = array_fill(0, 10000, 1);
$mid = memory_get_usage(); //10k elements array;
echo 'argv:', ($mid - $start )/10000,'byte' , '<br>';
$b = array_fill(0, 10000, 1);
$end = memory_get_usage(); //10k elements array;
echo 'argv:', ($end - $mid)/10000 ,'byte' , '<br>';
得到:
argv:54.5792byte
argv:54.5784byte
從這個結果來看似乎一個數組元素大約佔用了54個位元組左右。
首先看一下32位機C語言各種類型佔用的位元組:
[cpp] view plain print?
#include "stdafx.h"
//#include <stdio.h>
int main() {
printf("int:%d\nlong:%d\ndouble:%d\nchar*:%d\nsize_t:%d\n",
sizeof(int), sizeof(long),
sizeof(double), sizeof(char *),
sizeof(size_t));
return 0;
}
int:4
long:4
double:8
har*:4
size_t:4
在PHP中都使用long類型來代表數字,沒有使用int類型
大家都明白PHP是一種弱類型的語言,它不會去區分變數的類型,沒有int float char *之類的概念。
我們看看php在zend裡面存儲的變數,PHP中每個變數都有對應的 zval, Zval結構體定義在Zend/zend.h裡面,其結構:
[cpp] view plain print?
typedef struct _zval_struct zval;
struct _zval_struct {
/* Variable information */
zvalue_value value; /* The value 1 12位元組(32位機是12,64位機需要8+4+4=16) */
zend_uint refcount__gc; /* The number of references to this value (for GC) 4位元組 */
zend_uchar type; /* The active type 1位元組*/
zend_uchar is_ref__gc; /* Whether this value is a reference (&) 1位元組*/
};
PHP使用一種UNION結構來存儲變數的值,即zvalue_value 是一個union,UNION變數所佔用的內存是由最大
成員數據空間決定。
[cpp] view plain print?
typedef union _zvalue_value {
long lval; /* long value */
double dval; /* double value */
struct { /* string value */
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value obj; /*object value */
} zvalue_value;
最大成員數據空間是struct str,指針占*val用4位元組,INT佔用4位元組,共8位元組。
struct zval佔用的空間為8+4+1+1 = 14位元組,
其實呢,在zval中數組,字元串和對象還需要另外的存儲結構,數組則是一個 HashTable:
HashTable結構體定義在Zend/zend_hash.h.
[cpp] view plain print?
typedef struct _hashtable {
uint nTableSize;//4
uint nTableMask;//4
uint nNumOfElements;//4
ulong nNextFreeElement;//4
Bucket *pInternalPointer; /* Used for element traversal 4*/
Bucket *pListHead;//4
Bucket *pListTail;//4
Bucket **arBuckets;//4
dtor_func_t pDestructor;//4
zend_bool persistent;//1
unsigned char nApplyCount;//1
zend_bool bApplyProtection;//1
#if ZEND_DEBUG
int inconsistent;//4
#endif
} HashTable;
HashTable 結構需要 39 個位元組,每個數組元素存儲在 Bucket 結構中:
[cpp] view plain print?
typedef struct bucket {
ulong h; /* Used for numeric indexing 4位元組 */
uint nKeyLength; /* The length of the key (for string keys) 4位元組 */
void *pData; /* 4位元組*/
void *pDataPtr; /* 4位元組*/
struct bucket *pListNext; /* PHP arrays are ordered. This gives the next element in that order4位元組*/
struct bucket *pListLast; /* and this gives the previous element 4位元組 */
struct bucket *pNext; /* The next element in this (doubly) linked list 4位元組*/
struct bucket *pLast; /* The previous element in this (doubly) linked list 4位元組*/
char arKey[1]; /* Must be last element 1位元組*/
} Bucket;
Bucket
結構需要 33 個位元組,鍵長超過四個位元組的部分附加在 Bucket 後面,而元素值很可能是一個 zval 結構,另外每個數組會分配一個由
arBuckets 指向的 Bucket 指針數組, 雖然不能說每增加一個元素就需要一個指針,但是實際情況可能更糟。這么算來一個數組元素就會佔用
54 個位元組,與上面的估算幾乎一樣。
一個空數組至少會佔用 14(zval) + 39(HashTable) + 33(arBuckets) = 86
個位元組,作為一個變數應該在符號表中有個位置,也是一個數組元素,因此一個空數組變數需要 118
個位元組來描述和存儲。從空間的角度來看,小型數組平均代價較大,當然一個腳本中不會充斥數量很大的小型數組,可以以較小的空間代價來獲取編程上的快捷。但如果將數組當作容器來使用就是另一番景象了,實際應用經常會遇到多維數組,而且元素居多。比如10k個元素的一維數組大概消耗540k內存,而10k
x 10 的二維數組理論上只需要 6M 左右的空間,但是按照 memory_get_usage
的結果則兩倍於此,[10k,5,2]的三維數組居然消耗了23M,小型數組果然是劃不來的。
Ⅳ PHP高手請進。把一個三維數組里的所有二維數組取出來比較,取交集...
//foreach三維,一步步算下去就行
$arr = array(
array('a'=>'1','b'=>'2','c'=>'5','d'=>'56'),
array('a'=>'1','c'=>'5','d'=>'56'),
array('a'=>'1','c'=>'5','e'=>'44'),
array('a'=>'1','c'=>'5','d'=>'56','f'=>'d')
);
if (count($arr)>0)
{
foreach($arr as $key=>$val)
{
if ($key==0)//第一個先取出來
{
$tmp_arr = $val;
}
else
{
$tmp_arr = array_intersect_assoc($tmp_arr,$val);
}
}
}
print_r($tmp_arr);