Array ( [b] => Array"/>  Array ( [b] => Array">
search
HomeBackend DevelopmentPHP Tutorialphp 数组相加,看不懂,请些一个好例子给小弟我看一下好吗

php 数组相加,看不懂,请些一个好例子给我看一下好吗

$a ="a";<br />$b ="b";<br />$ay[$a][$b][]= 3;<br />print_r($ay);<br />

输出, Array ( [a] => Array ( [b] => Array ( [0] => 3 ) ) ),意思是不是数组a中存在数组b,数组b中存在一个数组,该数组索引0的时候值是3 。

//////////////////////////////////////
$Me[$a][$b] += $Me[$k][$c]; //请问这个相加之后左边的值会变成什么样??
$Me[$z][$m] = $Me[$sy][$my]; //这个赋值之后会变成怎么样?? 

这两句,看得我头很大,请帮忙,最好写一个例子给我演示一下这两句,我头脑真的不太好用。。

我只懂得下面这样简单的。。
$a[] =1;
$b[] =2;
echo $a[0] + b[0];结果是3 。
也懂得
$a[] =1;
$b[] =2;
echo $a[0] = b[0]; 结果a[0]是2;
------解决方案--------------------
$Me[$a][$b] += $Me[$k][$c]; //请问这个相加之后左边的值会变成什么样??
$Me[$z][$m] = $Me[$sy][$my]; //这个赋值之后会变成怎么样?? 


这是二维数组的表示法,表示一个元素的值,你大可以当作一个变量:
$a += 1;
$a =  3;
这样就能看懂了吧

------解决方案--------------------
$Me[$a][$b] += $Me[$k][$c]; //请问这个相加之后左边的值会变成什么样??
这是简写,等同
$Me[$a][$b] = $Me[$a][$b]+$Me[$k][$c];
等同
$a=$a+$b;

$Me[$z][$m] = $Me[$sy][$my]; //这个赋值之后会变成怎么样?? 
$Me[$z][$m]和$Me[$sy][$my]都是存放一个值的位置
假如$Me[$z][$m]存放的值为1,$Me[$sy][$my]存放的值为2
那么 echo $Me[$z][$m] = $Me[$sy][$my];
结果就是 $Me[$z][$m]是2
------解决方案--------------------
php array() 是多维数组,
1维的取值方式是:array[]
2维的取值方式是:array[][]
以此类推。
可以这样返回数组的维度。
<br />/** <br /> * 返回数组的维度 <br /> * @param  [type] $arr [description] <br /> * @return [type]      [description] <br /> */<br />function arrayLevel($arr){ <br />    $al = array(0); <br />    function aL($arr,&$al,$level=0){ <br />        if(is_array($arr)){ <br />            $level++; <br />            $al[] = $level; <br />            foreach($arr as $v){ <br />                aL($v,$al,$level); <br />            } <br />        } <br />    } <br />    aL($arr,$al); <br />    return max($al); <br />} <br /><br />$arr = array( <br />    '0'=>'0', <br />); <br /><br />echo arrayLevel($arr); <br />

------解决方案--------------------
合并数组建议采用php array自带的函数 array_merge()http://www.w3school.com.cn/php/func_array_merge.asp
------解决方案--------------------
多动手,少动眼,记得给脑子加点油
$Me = array(<br />  array(1, 2, 3, 4),<br />  array(4, 6, 7, 8),<br />  array(9, 10, 11, 12),<br />  array(13, 14, 15, 16),<br />);<br />$a = 0;<br />$b = 1;<br />$k = 2;<br />$c = 3;<br />$z = 3;<br />$m = 3;<br />$sy = 1;<br />$my = 2;<br />$Me[$a][$b] += $Me[$k][$c];<br />$Me[$z][$m] = $Me[$sy][$my];<br />print_r($Me);

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!