PHP - Two Dimensional Array

Vilge234

Yeni üye
1 Eki 2020
28
0

PHP Two Dimensional Array

In the array, we sometimes need to save different properties of the index value. At situations like these, we use two dimensional array. For example : In the checkout application, there may be array elements that we need to add to the index value with product ID, such as product name, price, and quantity. In the software that we talked about before, we mentioned the checkout logic. Now moving on from this topic, I'll do the introduction.


Defining 2 Dimensional Array

We define the single dimensional array like this :

Kod:
[B][COLOR=White][FONT=Microsoft Sans Serif][COLOR=Silver][FONT=Lucida Console][COLOR=Sienna]$dizi_adi[/COLOR] =  [COLOR=MediumTurquoise]array[/COLOR]([COLOR=Lime]"eleman1"[/COLOR],[COLOR=lime]"eleman2"[/COLOR],[COLOR=lime]"eleman3"[/COLOR]);

[/FONT][/COLOR][/FONT][/COLOR][/B]
At two dimensional arrays we do the defination like this.
Kod:
[COLOR=White][FONT=Microsoft Sans Serif][COLOR=silver][FONT=Lucida Console] [COLOR=Sienna]<?php[/COLOR]  [COLOR=#ff5555]$dizi[/COLOR] = [COLOR=mediumturquoise]array[/COLOR](   [COLOR=MediumTurquoise]array[/COLOR]([COLOR=darkorange]1[/COLOR],[COLOR=darkorange]2[/COLOR],[COLOR=darkorange]3[/COLOR],[COLOR=darkorange]4[/COLOR]),   [COLOR=mediumturquoise]array[/COLOR]([COLOR=darkorange]2[/COLOR],[COLOR=darkorange]3[/COLOR],[COLOR=darkorange]4[/COLOR],[COLOR=darkorange]5[/COLOR]),   [COLOR=mediumturquoise]array[/COLOR]([COLOR=darkorange]5[/COLOR],[COLOR=darkorange]6[/COLOR],[COLOR=darkorange]7[/COLOR],[COLOR=darkorange]8[/COLOR]),   [COLOR=mediumturquoise]array[/COLOR]([COLOR=darkorange]9[/COLOR],[COLOR=darkorange]10[/COLOR],[COLOR=darkorange]11[/COLOR],[COLOR=darkorange]12[/COLOR]), );  echo [COLOR=#ff5555]$dizi[/COLOR][[COLOR=DarkOrange]1[/COLOR]][[COLOR=darkorange]2[/COLOR]];  [COLOR=sienna]?>[/COLOR][/FONT][/COLOR]
[COLOR=silver][FONT=Lucida Console] [/FONT][/COLOR]
[/FONT][/COLOR][COLOR=White][FONT=Microsoft Sans Serif] 
[/FONT][/COLOR]

The output is 2nd phrase. 3rd column, meaning it's going to be 4


Let's make a checkout application about this.

First of all let's create a session and define it as an array.

Kod:
[FONT=Microsoft Sans Serif][B][COLOR=White][FONT=Microsoft Sans Serif][COLOR=Silver][FONT=Lucida Console] [COLOR=Sienna]<?php[/COLOR]  [COLOR=MediumTurquoise]session_start[/COLOR](); [COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=Lime]"sepet"[/COLOR]] = [COLOR=mediumturquoise]array[/COLOR]();  [COLOR=sienna]?>[/COLOR][/FONT][/COLOR][/FONT][/COLOR][/B]
[/FONT]
Now, let's create an index through the product ID and add the datas as if there is an array with 3 elements. We don't need to define this array again. After that, let's take the output with print_r view it. I checked it by assigning the variables myself.

Kod:
[FONT=Microsoft Sans Serif][B][COLOR=White][FONT=Microsoft Sans Serif][FONT=Lucida Console] [COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=lime]"sepet"[/COLOR]][[COLOR=#ff5555]$urun_id[/COLOR]][[COLOR=DarkOrange]0[/COLOR]] = [COLOR=#ff5555]$urun_isim[/COLOR]; [COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=Lime]"sepet"[/COLOR]][[COLOR=#ff5555]$urun_id[/COLOR]][[COLOR=darkorange]1[/COLOR]] = [COLOR=#ff5555]$urun_fiyat[/COLOR]; [COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=lime]"sepet"[/COLOR]][[COLOR=#ff5555]$urun_id[/COLOR]][[COLOR=darkorange]2[/COLOR]] = [COLOR=#ff5555]$urun_adet[/COLOR];  [COLOR=MediumTurquoise]print_r[/COLOR]([COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=Lime]"sepet"[/COLOR]]);[/FONT][/FONT][/COLOR][/B] [URL="https://i.hizliresim.com/4dKDtQ.png"][IMG]https://i.hizliresim.com/4dKDtQ.png[/IMG][/URL]
To get output:

[/FONT]
Kod:
[FONT=Microsoft Sans Serif][B][COLOR=White][FONT=Microsoft Sans Serif][FONT=Lucida Console][COLOR=Silver] [COLOR=MediumTurquoise]echo [/COLOR][COLOR=lime]"<pre>"[/COLOR]; [COLOR=mediumturquoise]print_r[/COLOR]([COLOR=#ff5555]$_SESSION[/COLOR][[COLOR=Lime]"sepet"[/COLOR]]); [COLOR=mediumturquoise]echo [/COLOR][COLOR=lime]"</pre>"[/COLOR];[/COLOR][/FONT][/FONT][/COLOR][/B]
[/FONT]


The output will be like this:

Bu resim yeniden boyutlandırıldı, tam halini görmek için tıklayınız.













Content: 2 dimensional array at php, assigning array to session, assigning element to array. ---------------------
 
Moderatör tarafında düzenlendi:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.