Về cơ bản mảng trong PHP cũng tuân thủ cách khai báo và đặt tên giống như trong javaScript. Chỉ khác là ta dùng ký tự ‘ $ ‘ thay thế cho ‘ let’ và cũng có 2 cách để khai báo mảng đó là :
- $tên mảng = array (phần tử 1, phần tử 2,…..);
- $tên mảng = [phần tử 1, phần tử 2,…..];
Các phần tử trong mảng nếu là chuỗi(string) thì sẽ nằm trong dấu ” ” hoặc ‘ ‘ .
Để truy xuất các phần tử trong mảng ta dùng echo, print để truy xuất theo mảng chỉ số, hoặc gán giá trị cho các phần tử của mảng. Và dùng var_dump, print_r để truy xuất toàn bộ phần tử trong mảng.Dùng hàm count() để lấy độ dài của mảng, dùng for để duyệt qua các phần tử của mảng 1 chiều. Associative Array(mảng liên kết) là mảng sử dụng các khóa bằng tên thay cho chỉ số thông thường
Ví dụ: $age = array(“Peter”=>”35”, “Ben”=>”37″,”Joe”=>”43”);
Có thể truy xuất phần tử qua tên khóa, ví dụ: $age[‘Peter’] = “35”; …. Và có thể sử dụng foreach để duyệt qua tất cả các phần tử của một associative array(mảng liên kết). Ví dụ : foreach($age as $key => $value) { echo “Key=” . $key . “, Value=” . $value; }.Mảng nhiều chiều là mảng có các phần tử là các mảng.Có thể có mảng 2 chiều, 3 chiều, 4 chiều… hoặc nhiều hơn nhưng thông thường chúng ta chỉ làm việc với mảng 2 chiều, vì các mảng nhiều chiều hơn thì khó quản lý hơn .Mảng nhiều chiều thì cần nhiều chỉ số để có thể truy xuất đến một phần tử, chẳng hạn: Mảng 2 chiều thì cần 2 chỉ số để truy xuất phần tử còn mảng 3 chiều thì cần 3 chỉ số để truy xuất phần tử. Ví dụ khai báo mảng đa chiều:
$cars = array
(
array(“Volvo”,22,18),
array(“BMW”,15,13),
array(“Saab”,5,2),
array(“Land Rover”,17,15)
);
Sử dụng các vòng lặp lồng nhau để duyệt qua tất cả các phần tử của mảng nhiều chiều
• Ví dụ: <?php
for ($row = 0; $row < 4; $row++) {
echo “<ul>”;
for ($col = 0; $col < 3; $col++) {
echo “<li>”.$cars[$row][$col].”</li>”;
}
echo “</ul>”;
}
?>
Mọi người nên tìm đọc và xem ví dụ trực tiếp như vậy sẽ hiểu rõ hơn.
Thanks !
11 comments On Mảng trong PHP.
I¦ve learn some excellent stuff here. Definitely value bookmarking for revisiting. I surprise how so much effort you set to make one of these excellent informative website.
Thanks for some other fantastic post. The place else may just anyone get that kind of information in such a perfect approach of writing? I’ve a presentation subsequent week, and I am on the look for such information.
I really like what you guys tend to be up too. This sort of clever work and reporting! Keep up the excellent works guys I’ve included you guys to our blogroll.
Heya i’m for the first time here. I came across this board and I in finding It really useful & it helped me out a lot. I am hoping to offer something back and aid others like you helped me.
You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
Excellent goods from you, man. I have understand your stuff previous to and you’re just extremely fantastic. I really like what you have acquired here, really like what you are saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I can not wait to read far more from you. This is really a great web site.
Today, I went to the beachfront with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!
Valuable info. Lucky me I found your site by accident, and I’m shocked why this accident did not happened earlier! I bookmarked it.
Thank you for sharing excellent informations. Your website is so cool. I’m impressed by the details that you have on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched all over the place and simply couldn’t come across. What a great site.
Whats Happening i am new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I hope to give a contribution & assist other customers like its aided me. Great job.
It’s arduous to find knowledgeable folks on this topic, however you sound like you realize what you’re speaking about! Thanks
Sliding Sidebar
Facebook