Some basic PHP syntax (array, string)

Source: Internet
Author: User
Tags arrays explode php tutorial

Some basic PHP syntax (array, string)
I wrote some basic PHP syntax, as well as form submission and processing. In this article, I wrote the usage of arrays and the processing of arrays and strings, hope to help you:

 

Multi-dimensional array:

$ Arr1 = array ('a' => 'MD', 'B' => 'ad '...);

$ Arr2 = array ('C' => 'SD', 'D' => 'AE '...);

$ Arr = array ('us' => $ arr1, 'AB' => $ arr2 );

Use multi-dimensional array:

Echo "{$ arr ['us'] ['A']}";

 

Eg:

--- Arr1.php tutorial

<Body>

<Form action = "arr. php" method = "post">

<Fieldset> <legend> Enter your information in the form below: </legend>

<P> <B> NAME: </B> <input type = "text" name = "name" maxlength = "40" size = "20"> </p>

 

<P> <B> Interests: </B>

<Input type = "checkbox" name = "interests []" value = "Music"/> Music

<Input type = "checkbox" name = "interests []" value = "Movies"/> Movies

<Input type = "checkbox" name = "interests []" value = "Books"/> Books

<Input type = "checkbox" name = "interests []" value = "Skiing"/> Skiing

<Input type = "checkbox" name = "interests []" value = "Napping"/> Napping

</P>

 

</Fieldset>

 

<Div align = "center"> <input type = "submit" name = "submit" value = "submit"/> </div>

</Form>

</Body>

 

--- Arr. php

 

<Body>

<? Php

If (! Empty ($ _ POST ['name'])

{

$ Name = strips tutorial lashes ($ _ POST ['name']);

}

Else

{

$ Nam = NULL;

Echo '<p> <font color = "red"> You forgot to enter your name </font> </p> ';

}

 

If (isset ($ _ POST ['interests'])

{

$ Interests = true;

}

Else

{

$ Interests = NULL;

Echo '<p> <font color = "red"> You forgot to enter your interests! </Font> </p> ';

}

If ($ name & $ interests)

{

Echo "Thank you, <B> $ name </B>, You entered your interests as: <ul> ";

Foreach ($ _ POST ['interests'] as $ value)

{

Echo "<li >$ value </li> n ";

}

Echo '</ul> ';

}

Else

{

Echo '<p> <font color = "red"> Please go back and fill out of the form again. </font> </p> ';

}

?>

</Body>

 

Array and string

Array and string conversion:

$ Array = explode (separator, $ string );

$ String = implode (glue, $ array );

 

Separator (separator) and glue. When an array is converted into a string, it is set to glue-the characters and code inserted into the array value in the generated string. When converting a string into an array, a delimiter is used to describe the code that generates links between different elements in the array.

Eg:

$ String1 = 'mon-Tue-Wed-Thur-fri ';

$ Days = explode ('-', $ string1 );

$ Days has five elements: 0 => 'Mon '...

$ String2 = implode (',', $ days );

$ String2 is a comma-separated list of days in a week: Mon, Tue ...;

 

 

In the preceding example, the arr. php part can be changed:

                     

If (isset ($ _ POST ['interests'])

{

$ Interests = implode (',', $ _ POST ['interests']);

}

Then, $ interests can be output as a string.

 

 

Summary:

 

Synonym of implode () function in join () function

The third optional parameter of the explode () function is a number used to limit the number of array elements to be created.

 

 

Array sorting

You can use sort () to sort the array by value, but it discards the original key. After sorting, the key of the array is reset. Therefore, if the key-value relationship is very important, you are advised not to use this function.

Sort by value with asort () to maintain the key.

You can use ksort () to sort arrays.

If you change the preceding functions to rsort (), arsort (), and krsort (), you can sort the arrays in reverse mode.

 

Eg:

// -------- Arrsort. php

<Body>

<? Php

$ Movies = array (10 => 'CA', 9 => 'to', 2 => 'The ', 8 => 'sideway ', 7 => 'donni ');

Echo '<p> In their original order: <br/> <pre> Rating Title: <br/> ';

Foreach ($ movies as $ key => $ value)

{

Echo "$ keyt $ valuen ";

}

Echo '</pre> </p> ';

 

Echo '<p> Sorted by title: <br/> <pre> Rating Title: <br/> ';

Asort ($ movies); // sort the array by value.

Foreach ($ movies as $ key => $ value)

{

Echo "$ keyt $ valuen ";

}

Echo '</pre> </p> ';

 

Echo '<p> Sorted by rating: <br/> <pre> Rating Title: <br/> ';

Krsort ($ movies); // Sort by pressing the buttons and invert the array position.

Foreach ($ movies as $ key => $ value)

{

Echo "$ keyt $ valuen ";

}

Echo '</pre> </p> ';

?>

</Body>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.