logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ hàm array_values() trong php

Hàm array_values() trong php

by Admin _ April 25, 2022

What is a PHP Array?

A PHP array is a variable that stores more than one piece of related data in a single variable.Think of an array as a box of chocolates with slots inside.The box represents the array itself while the spaces containing chocolates represent the values stored in the arrays.The diagram below illustrates the above syntax.

Bạn đang xem: Hàm array_values() trong php

Numeric Arrays

Numeric arrays use number as access keys.An access key is a reference khổng lồ a memory slot in an array variable.The access key is used whenever we want khổng lồ read or assign a new value an array element.Below is the syntax for creating numeric array in php. Array Example

Or value, …);?>HERE, “$variable_name…” is the name of the variable “” is the access index number of the element “value” is the value assigned khổng lồ the array element.Let’s now look at an example of a numeric array.Suppose we have 5 movies that we want to lớn store in array variables.We can use the example shown below to vì that.Here,

*
Each movie is given an index number that is used lớn retrieve or modify its value. Observe the following code-Output:Once upon a time in trung quốc Eastern CondorsAs you can see from the above examples, working with arrays in PHP when dealing with multiple values of the same nature is very easy and flexible.Alternatively, the above array variables can also be created using the following code. "Shaolin Monk", 1 => "Drunken Master", 2 => "American Ninja", 3 => "Once upon a time in China", 4 =>"Replacement Killers" );echo $movie<4>;?>Output:Replacement Killers

PHP Associative Array

Associative array differ from numeric array in the sense that associative arrays use descriptive names for id keys.Below is the syntax for creating associative array in php. Value);?>HERE, “$variable_name…” is the name of the variable “<"key_name">” is the access index number of the element “value” is the value assigned to the array element.Let’s suppose that we have a group of persons, and we want khổng lồ assign the gender of each person against their names.We can use an associative array to vày that.The code below helps us to vày that. "Female", "John" => "Male", "Mirriam" => "Female");print_r($persons); echo ""; echo "Mary is a " . $persons<"Mary">;?> HERE,
*
Output:Array ( => Female => Male => Female ) Mary is a FemaleAssociative array are also very useful when retrieving data from the database.The field names are used as id keys.

PHP Multi-dimensional arrays

These are arrays that contain other nested arrays.The advantage of multidimensional arrays is that they allow us khổng lồ group related data together.Let’s now look at a practical example that implements a php multidimensional array.The table below shows a menu of movies by category.

Xem thêm: Cung Cấp Hóa Chất Gelatin Trong Thực Phẩm Là Gì? Ứng Dụng Trong Đời Sống Của

Movie titleCategory
Pink PantherComedy
John EnglishComedy
Die HardAction
ExpendablesAction
The Lord of the ringsEpic
Romeo and JulietRomance
See no evil hear no evilComedy
The above information can be represented as a multidimensional array. The code below shows the implementation. Array("Pink Panther", "John English", "See no evil hear no evil"),"action" => array("Die Hard", "Expendables"),"epic" => array("The Lord of the rings"),"Romance" => array("Romeo & Juliet"));print_r($movies);?>HERE,
*
Output:Array ( => Array ( <0> => Pink Panther <1> => John English <2> => See no evil hear no evil ) => Array ( <0> => Die Hard <1> => Expendables ) => Array ( <0> => The Lord of the rings ) => Array ( <0> => Romeo và Juliet ) )Another way to define the same array is as follows array( 0 => "Pink Panther", 1 => "john English", 2 => "See no evil hear no evil" ), "action" => array ( 0 => "Die Hard", 1 => "Expendables" ), "epic" => array ( 0 => "The Lord of the rings" ), "Romance" => array ( 0 => "Romeo và Juliet" ));echo $film<"comedy"><0>;?>Output:Pink PantherNote: the movies numeric array has been nested inside the categories associative array

PHP Arrays: Operators

OperatorNameDescriptionHow to vị itOutput
x + yUnionCombines elements from both arrays 1);$y = array("value" => 10);$z = $x + $y;?>Array( => 1 => 10)
X == yEqualCompares two arrays if they are equal và returns true if yes. 1);$y = array("id" => "1");if($x == $y)echo "true";elseecho "false";?>True or 1
X === yIdenticalCompares both the values và data types 1);$y = array("id" => "1");if($x === $y)echo "true";elseecho "false";?>False or 0
X != y, x yNot equal 1);$y = array("id" => "1");if($x != $y)echo "true";elseecho "false";?>False or 0
X !== yNon identical 1);$y = array("id" => "1");if($x !== $y)echo "true";elseecho "false";?>True or 1

PHP Array Functions

Count function

The count function is used khổng lồ count the number of elements that an php array contains. The code below shows the implementation.Output:3

is_array function

The is_array function is used to determine if a variable is an array or not. Let’s now look at an example that implements the is_array functions.Output:1

Sort

This function is used to lớn sort arrays by the values.If the values are alphanumeric, it sorts them in alphabetical order.If the values are numeric, it sorts them in ascending order.It removes the existing access keys and địa chỉ new numeric keys.The output đầu ra of this function is a numeric array "Female", "John" => "Male", "Mirriam" => "Female");sort($persons);print_r($persons);?>Output:
Share Tweet Linkedin Pinterest
Previous Post

Break continue php foreach

Next Post

Search operations

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
free php encoder

Free php encoder

28/05/2022
xuất file pdf trong php

Xuất file pdf trong php

28/05/2022
php

Php

28/05/2022
học php cần phần mềm gì

Học php cần phần mềm gì

27/05/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Phần mềm join file

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.