Computers Windows Internet

Add new item to php array. PHP: Adding and Removing Array Elements. Defining elements in an array

Adding items to an array

If the array exists, additional elements can be added to it. This is done directly using the assignment operator (equal sign) in the same way as assigning a value to a string or number. In this case, you do not need to set the key of the added element, but in any case, when accessing the array, you need square brackets... By adding two new items to the $ List, we write:

$ List = "pears";
$ List = "tomatoes";

If no key is specified, each element will be added to the existing array and indexed with the next sequential number. If we add new elements to the array from the previous section, whose elements had indexes 1, 2 and 3, then pears will have index 4, and tomatoes will have 5. When you explicitly set the index, and the value with it is already exists, then the existing value in this place will be lost and replaced by a new one:

$ List = "pears";
$ List = "tomatoes";

Now the value of the element at index 4 is "tomatoes" and the element "oranges" is no longer there. I would advise not to specify a key when adding elements to an array, unless, of course, you want to specifically overwrite any existing data. However, if strings are used as indices, the keys must be specified so as not to lose values.

We will try to add new elements to the array by rewriting the soups.php script. First, by printing the original elements of the array, and then the original ones along with the added ones, we can easily see the changes that have occurred. Just as you can find out the length of a string (the number of characters it contains) using the strlen () function, it is also easy to determine the number of elements in an array using the count () function:

$ HowMany = count ($ Array);

  1. Open the soups.php file in text editor.
  2. After initializing the array with the array () function, add the following entry:
  3. $ HowMany = count ($ Soups);
    print ("The array contains $ HowMany elements.

    \ n ");

    The count () function will determine how many elements are in the $ Soups array. By assigning this value to a variable, you can print it.

  4. Add three additional elements to the array.
  5. $ Soups ["Thursday"] = "Chicken Noodle";
    $ Soups ["Friday"] = "Tomato";
    $ Soups ["Saturday"] = "Cream of Broccoli";
  6. Recalculate the elements in the array and print this value.
  7. $ HowManyNow = count ($ Soups);
    print ("The array now contains $ HowManyNow elements.

    \ n ");

  8. Save the script (Listing 7.2), upload it to the server, and test it in a browser (Figure).

Listing 7.2 You can directly add one element at a time to an array by assigning a value to each element using the appropriate operator. The count () function can be used to find out how many elements are in the array.

1
2
3 Using arrays</TITLEx/HEAD><br> 4 <BODY><br> 5 <?php<br>6 $ Soups = array ( <br>7 "Monday" => "Clam Chowder", <br>8 "Tuesday" => "White Chicken Chili", <br>9 "Wednesday" => "Vegetarian"); <br><br>11 print ("The array contains $ HowMany <br>elements. <P>\ n "); <br>12 $ Soups ["Thursday"] = "Chicken Noodle"; <br>13 $ Soups ["Friday"] = "Tomato"; <br>14 $ Soups ["Saturday"] = "Cream of <br>Broccoli "; <br>15 $ HowManyNow = count ($ Soups); <br>16 print ("The array now contains <br>$ HowManyNow elemente. <P>\ n "); <br> 17 ?><br> 18 </BODY><br> 19 </HTML> </p><p>PHP 4.0 introduced <a href="https://appcube.ru/en/chto-za-novaya-funkciya-v-instagram-prodvizhenie-cherez-instagram-stories-v.html">new function</a>, allowing you to add one array to another. This operation can also be called merging or concatenating arrays. The array_merge () function is called like this:</p><p>$ NewArray = array_merge ($ OneArray, $ TwoArray);</p><p>You can rewrite the soups.php page using this function if you are using a server with PHP 4.0 installed.</p> <p>Combining two arrays</p> <ol><li>Open the soups.php file in a text editor if it is not already open.</li> <li>After initializing the $ Soups array, count its elements and print the result.</li>$ HowMany = count ($ Soups); <br>print ("The $ Soups array contains $ HowMany elements. <P>\ n "); <ol>Create a second array, count its elements, and print the result as well.</ol>$ Soups2 = array ( <br>"Thursday" => "Chicken Noodle", <br>"Friday" => "Tomato", <br>"Saturday" => "Cream of Broccoli"); <br>$ HowMany2 = count ($ Soups2); <br>print ("The $ Soups2 array contains $ HowMany2 elements. <P>\ n "); <li>Concatenate the two arrays into one.</li>$ TheSoups = array_merge ($ Soups, $ Soups2); <p>Make sure that the arrays are in this order ($ Soups, then $ Soups2), that is, Thursday and Friday elements should be added to Wednesday's Monday elements, and not vice versa.</p> <li>Count the elements of the new array and print the result.</li>$ HowMany3 = count ($ TheSoups); <br>print ("The $ TheSoups array contains <br>- $ HowMany3 elements. <P>\ n "); <li>Close the PHP and HTML document.</li> ?></BODYx/HTML> <li>Save the file (Listing 7.3), upload it to the server, and test it in a browser (Figure).</li> </ol><img src='https://i0.wp.com/weblibrary.biz/bimages/php/img49.gif' height="256" width="217" loading=lazy loading=lazy><p>Listing 7.3 The Array_merge () function is new. This is one of several additional PHP 4.0 functions for working with arrays. Using arrays can save you a lot of time.</p><p>1 <HTML><br> 2 <HEAD><br> 3 <TITLE>Using arrays</TITLEx/HEAD><br> 4 <BODY><br> 5 <?php<br>6 $ Soups = array! <br>7 "Monday" => "Clam Chowder", <br>"Tuesday" => "White Chicken Chili", <br>8 "Wednesday" => "Vegetarian" <br> 9);<br>10 $ HowMany = count ($ Soups); <br>11 print ("The $ Soups array contains $ HowMany elements. <P>\ n "); <br>12 $ Soups2 = array ( <br>13 "Thursday" => "Chicken Noodle", <br>14 "Friday" => "Tomato", <br>15 "Saturday" => "Cream of Broccoli" <br> 16); .<br>17 $ HowMany2 = count ($ Soups2); <br>18 print ("The $ Soups2 array contains $ HowMany2 elements. <P>\ n "); <br>19 $ TbeSoupe = array_merge ($ Soups, $ Soups2); <br>20 $ HowMany3 = count ($ TheSoups); <br>21 print ("The $ TheSoups array contains. $ HowMany3 elements. <P>\ n "); <br> 22 ?> "<br> 23 </BODY><br> 24 </HTML> </p><p>Be careful when adding elements to the array directly. The correct way to do this is: $ Ar ray = "Add This"; $ Aggau = "Add This"; but it’s correct like this: $ Aggau = "Add This" ;. If you forget to put parentheses, then the added value will destroy the existing array, turning it into a simple string or number.</p> <p>PHP 4.0 has several new functions for working with arrays. Not all of them are covered in the book. However, complete information on this subject is contained in the PHP language manual, which can be found on the PHP website. Be careful not to use new features unique to PHP 4.0 if your server is running PHP 3.x.</p> <p>Let's look at ways to write values ​​to an array. An existing array can be modified by explicitly setting values ​​in it. This is done by assigning values ​​to an array.</p> <p>An assignment to an array element looks the same as an assignment to a variable, except for the square brackets () that are added after the name of the array variable. The index / key of the element is indicated in square brackets. If no index / key is specified, PHP will automatically pick the smallest unoccupied numeric index.</p><p> <?php $my_arr = array(0 =>"zero", 1 => "one"); $ my_arr = "two"; $ my_arr = "three"; var_dump ($ my_arr); // assignment without specifying index / key $ my_arr = "four"; $ my_arr = "five"; echo " <br>"; var_dump ($ my_arr);?></p><p>To change a specific value, you just need to assign a new value to an already existing element. To remove any element of an array with its index / key, or to completely remove the array itself, use the unset () function:</p><p> <?php $my_arr = array(10, 15, 20); $my_arr = "радуга"; // изменяем значение первого элемента unset($my_arr); // Удаляем полностью второй элемент (ключ/значение) из массива var_dump($my_arr); unset($my_arr); // Полностью удаляем массив?> </p><p>Note: As mentioned above, if an element is added to an array without specifying a key, PHP will automatically use the previous largest integer key value, incremented by 1. If there are no integer indexes in the array yet, the key will be 0 (zero).</p> <p>Note that the largest integer key value <b>does not necessarily exist in the array at the moment</b>, this may be due to the removal of array elements. After the elements have been removed, the array is not reindexed. Let's give the following example to make it clearer:</p><p> <?php // Создаем простой массив с числовыми индексами. $my_arr = array(1, 2, 3); print_r($my_arr); // Теперь удаляем все элементы, но сам массив оставляем нетронутым: unset($my_arr); unset($my_arr); unset($my_arr); echo "<br>"; print_r ($ my_arr); // Add an item (note that the new key will be 3 instead of 0). $ my_arr = 6; echo" <br>"; print_r ($ my_arr); // Reindexing: $ my_arr = array_values ​​($ my_arr); $ my_arr = 7; echo" <br>"; print_r ($ my_arr);?></p><p>This example uses two new functions, print_r () and array_values ​​(). Array_values ​​() returns an indexed array (re-indexes the returned array with numeric indices), and print_r works like var_dump, but outputs arrays in a more readable way.</p> <p>We can now consider a third way to create arrays:</p><p> <?php // следующая запись создает массив $weekdays = "Понедельник"; $weekdays = "Вторник"; // тоже самое, но с указанием индекса $weekdays = "Понедельник"; $weekdays = "Вторник"; ?> </p><p>The example showed the third way to create an array. If the $ weekdays array has not been created yet, it will be created. However, this type of array creation is not recommended because if the $ weekdays variable has already been created and contains a value, this can lead to unexpected results from the script.</p> <p>If you have any doubts about whether a variable is an array, use the is_array function. For example, validation can be performed as follows:</p><p> <?php $yes = array("это", "массив"); echo is_array($yes) ? "Массив" : "Не массив"; echo "<br>"; $ no =" regular string "; echo is_array ($ no)?" Array ":" Not an array ";?></p> <p><b>PHP</b> supports scalar and composite data types. In this article, we will discuss one of the composite types: arrays. An array is a collection of data values, organized as an ordered set of key-value pairs.</p> <p>This article talks about creating an array, adding items to an array. There are many built-in functions that work with arrays in <b>PHP,</b> because arrays are common and useful to use. For example, if you want to send an email to more than one email address, you can store the email addresses in an array and then loop through the array, sending messages to the email address taken from the array.</p> <h2>Indexed and associative arrays</h2> <p>There are two kinds of arrays in PHP: indexed and associative. Indexed array keys are integers starting at 0. Indexed arrays are used when you need a specific position in an array. Associative arrays behave like two columns of a table. The first column is the key that is used to access the value (second column).</p> <p><b>PHP</b> internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is that the keys appear. Some functions are primarily intended for use with indexed arrays, since they assume that your keys are sequential integers starting at 0. In both cases, the keys are unique - that is, you cannot have two elements with the same key, regardless on whether the key is a string or an integer.</p> <p>V <b>PHP</b> arrays have an internal ordering of their elements that is independent of keys and values, and there are functions that you can use to traverse arrays based on this internal order.</p> <h2>Defining elements in an array</h2> <p>You can access specific values ​​from an array by using the array name followed by the element key (sometimes called an index) in square brackets:</p><p>$ age ["Fred"]; $ shows;</p><p>The key can be a string or an integer. String values ​​as numbers (no leading zeros) are treated as integers. Thus, <b>$ array</b> and <b>$ array [‘3’]</b> refer to the same element, but <b>$ array ['03 ']</b> refers to another element. Negative numbers can also be used as keys, but they do not specify positions from the end of the array, as in <b>Perl.</b></p> <p>It is not necessary to enclose the key in quotes. For example, <b>$ array [‘Fred’]</b> like <b>$ arrat.</b> Still considered good style <b>PHP</b> always use quotes. If the index is without quotes, then PHP uses the constant value as the index:</p><p>Define ("index", 5); echo $ array; // will return $ array, not $ array ["index"];</p><p>If you want to substitute a number into the index, then you need to do this:</p><p>$ age ["Clone $ number"]; // will return, for example, $ age ["Clone5"];</p><p>However, do not include the key in quotes in the following case:</p><p>// wrong print "Hello, $ person [" name "]"; print "Hello, $ person [" name "]"; // correct print "Hello, $ person";</p><h2>Storing data in arrays</h2> <p>When you try to store a value in an array, an array will be automatically created, if it did not exist before, but when you try to retrieve a value from an array that was not defined, the array will not be created. For example:</p><p>// $ addresses not defined until now echo $ addresses; // nothing echo $ addresses; // nothing $ addresses = "spam@cyberpromo.net"; echo $ addresses; // print "Array"</p><p>You can use simple assignment to initialize an array in a program:</p><p>$ addresses = "spam@cyberpromo.net"; $ addresses = "abuse@example.com"; $ addresses = "root@example.com"; // ...</p><p>We have declared an index array with integer indices starting at 0.</p> <p>Associative array:</p><p>$ price ["Gasket"] = 15.29; $ price ["Wheel"] = 75.25; $ price ["Tire"] = 50.00; // ...</p><p>An easier way to initialize an array is to use the construct <b>Array ()</b> which builds an array from its arguments:</p><p>$ addresses = array ("spam@cyberpromo.net", "abuse@example.com", "root@example.com");</p><p>To create an associative array using <b>Array (),</b> use <b>=> </b> character separating indices from values:</p><p>$ price = array ("Gasket" => 15.29, "Wheel" => 75.25, "Tire" => 50.00);</p><p>Pay attention to the use of spaces and alignment. We could group the code, but this would be less descriptive:</p><p>$ price = array ("Gasket" => 15.29, "Wheel" => 75.25, "Tire" => 50.00);</p><p>To create an empty array, you need to call the construction <b>Array ()</b> no arguments:</p><p>$ addresses = Array ();</p><p>You can specify a start key in an array and then a list of values. The values ​​are entered into an array, starting with a key and then incrementing:</p><p>$ days = array (1 => "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); // 2 is Tuesday, 3 is Wednesday, etc.</p><p>If the starting index is a string, then subsequent indexes become integers starting at 0. So the following code is probably an error:</p><p>$ whoops = array ("Friday" => "Black", "Brown", "Green"); // same as $ whoops = array ("Friday" => "Black", 0 => "Brown", 1 => "Green");</p><h2>Adding a new element to the end of the array</h2> <p>To insert multiple values ​​at the end of an existing indexed array, use the syntax:</p><p>$ family = array ("Fred", "Wilma"); // $ family = "Fred" $ family = "Pebbles"; // $ family = "Pebbles"</p><p>This construct assumes that the array indices are numbers and assigns the next available numeric index to the element, starting at 0. Trying to add an element to an associative array is almost always a programmer mistake, but <b>PHP</b> will add new elements with numeric indices (starting at 0) without issuing a warning:</p><p>$ person = array ("name" => "Fred"); // $ person ["name"] = "Fred"; $ person = "Wilma"; // $ person = "Wilma"</p><p>At this stage, we will finish the introductory part of working with arrays in PHP. I am waiting for you in the next article.</p> <p>There are many functions and operators for converting arrays in php: Collection of functions for working with arrays</p><p>There are several ways to add an array to an array using php, and all of them can be useful for individual cases.</p><h2>"Operator +"</h2><p>This is a simple but tricky way:</p><p>$ c = $ a + $ b</p><p><b>This adds only those keys that are not already in the $ a array. In this case, the elements are appended to the end of the array.</b></p><p>That is, if the key from the $ b array is absent in the $ a array, then an element with this key will be added to the resulting array. <br>If the $ a array already contains an element with such a key, then its value will remain unchanged.</p><p><b>In other words, the sum changes from the change of places of the terms: $ a + $ b! = $ B + $ a - this is worth remembering.</b></p><p>Now for a more detailed example to illustrate this:</p><p>$ arr1 = ["a" => 1, "b" => 2]; $ arr2 = ["b" => 3, "c" => 4]; var_export ($ arr1 + $ arr2); // array (// "a" => 1, // "b" => 2, // "c" => 4, //) var_export ($ arr2 + $ arr1); // array (// "b" => 3, // "c" => 4, // "a" => 1, //)</p><h2>Array_merge () function</h2><p>You can use this function as follows:</p><p>$ result = array_merge ($ arr1, $ arr2)</p><p>It resets numeric indices and replaces strings. Great for concatenating two or more numerically indexed arrays:</p><blockquote><p>If the input arrays have the same string keys, then each subsequent value will replace the previous one. However, if the arrays have the same numeric keys, the value mentioned last will not replace the original value, but will be appended to the end of the array.</p> </blockquote><h2>Array_merge_recursive function</h2><p>Does the same thing as array_merge but also recursively iterates through each branch of the array and does the same with the descendants.</p><h2>Array_replace () function</h2><p>Replaces elements of an array with elements of other passed arrays.</p><h2>Array_replace_recursive () function</h2><p>The same as array_replace only processes all the branches of the array.</p> <p><b>array_pad</b></p><p>Adds multiple elements to the array. <br>Syntax:</p><p>Array array_pad (array input, int pad_size, mixed pad_value)</p><p>Array_pad () returns a copy of the input array, to which elements with pad_values ​​have been added so that the number of elements in the resulting array is equal to pad_size. <br>If pad_size> 0, then the elements will be added to the end of the array, and if<0 - то в начало. <br>If the pad_size value is less than the elements in the original input array, then no addition will occur, and the function will return the original input array. <br>An example of using the array_pad () function:</p><p>$ arr = array (12, 10, 4); <br>$ result = array_pad ($ arr, 5, 0); <br>// $ result = array (12, 10, 4, 0, 0); <br>$ result = array_pad ($ arr, -7, -1); <br>// $ result = array (-1, -1, -1, -1, 12, 10, 4) <br>$ result = array_pad ($ arr, 2, "noop"); <br>// will not add</p><p><b>array_map</b></p><p>Applying a custom function to all elements of the specified arrays. <br>Syntax:</p><p>Array array_map (mixed callback, array arr1 [, array ...])</p><p>The array_map () function returns an array that contains the elements of all the specified arrays after being processed by the custom callback function. <br>The number of parameters passed to the user-defined function must match the number of arrays passed to the array_map () function.</p><p>An example of using the array_map () function: Processing one array</p><p> <?phpfunction cube($n) {<br>return $ n * $ n * $ n; <br>} <br>$ a = array (1, 2, 3, 4, 5); <br>$ b = array_map ("cube", $ a); <br>print_r ($ b); <br>?> </p><p>Array ( <br> => 1<br> => 8<br> => 27<br> => 64<br> => 125<br>) </p><p>An example of using the array_map () function: Processing multiple arrays</p><p> <?phpfunction show_Spanish($n, $m) {<br>return "The number $ n in Spanish is $ m"; <br>} <br>function map_Spanish ($ n, $ m) ( <br>return array ($ n => $ m); <br>}</p><p>$ a = array (1, 2, 3, 4, 5); <br>$ b = array ("uno", "dos", "tres", "cuatro", "cinco"); <br>$ c = array_map ("show_Spanish", $ a, $ b); <br>print_r ($ c);</p><p>$ d = array_map ("map_Spanish", $ a, $ b); <br>print_r ($ d); <br>?> </p><p>The above example will output the following:</p><p>// printout of $ cArray ( <br>=> The number 1 in Spanish is uno <br>=> The number 2 in Spanish is dos <br>=> Number 3 in Spanish is tres <br>=> Number 4 in Spanish is cuatro <br>=> The number 5 in Spanish is cinco <br>)</p><p>// printout of $ dArray ( <br>=> Array <br>=> uno <br>)</p><p>=> Array <br>=> dos <br>)</p><p>=> Array <br>=> tres <br>)</p><p>=> Array <br>=> cuatro <br>)</p><p>=> Array <br>=> cinco <br>)</p><p>Typically, the array_map () function is applied to arrays of the same dimension. If the arrays have different lengths, then the smaller ones are padded with elements with empty values. <br>It should be noted that if you specify null instead of the name of the processing function, an array of arrays will be created. <br>An example of using the array_map () function: Creating an array of arrays</p><p> <?php$a = array(1, 2, 3, 4, 5);<br>$ b = array ("one", "two", "three", "four", "five"); <br>$ c = array ("uno", "dos", "tres", "cuatro", "cinco"); <br>$ d = array_map (null, $ a, $ b, $ c); <br>print_r ($ d); <br>?> </p><p>The above example will output the following:</p><p>Array ( <br>=> Array <br> => 1<br>=> one <br>=> uno <br>)</p><p>=> Array <br> => 2<br>=> two <br>=> dos <br>)</p><p>=> Array <br> => 3<br>=> three <br>=> tres <br>)</p><p>=> Array <br> => 4<br>=> four <br>=> cuatro <br>)</p><p>=> Array <br> => 5<br>=> five <br>=> cinco <br>)</p><p>Function supported by PHP 4> = 4.0.6, PHP 5</p><p><b>array_pop</b></p><p>Retrieves and removes the last elements of an array. <br>Syntax:</p><p>Mixed array_pop (array arr);</p><p>The array_pop () function retrieves the last element from the arr array and returns it, removing it after that. With this function, we can build structures that resemble a stack. If the arr array was empty, or if it is not an array, the function returns an empty NULL string.</p><p>After using the array_pop () function, the array cursor is set to the beginning. <br>An example of using the array_pop () function:</p><p> <?php$stack = array("orange", "apple", "raspberry");<br>$ fruits = array_pop ($ stack); <br>print_r ($ stack); <br>print_r ($ fruits); <br>?> </p><p>The example will output the following:</p><p>Array ( <br>=> orange <br>=> banana <br>=> apple <br>) </p><p>The function is supported by PHP 4, PHP 5</p><p><b>array_push</b></p><p>Adds one or more elements to the end of an array. <br>Syntax:</p><p>Int array_push (array arr, mixed var1 [, mixed var2, ..])</p><p>Array_push () adds var1, var2, etc. to arr. She assigns them numerical indices - just as it does for standard ones. <br>If you only need to add one element, it might be easier to use this operator:</p><p>Array_push ($ Arr, 1000); // call the function $ Arr = 100; // the same, but shorter</p><p>An example of using the array_push () function:</p><p> <?php$stack = array("orange", "banana");<br>array_push ($ stack, "apple", "raspberry"); <br>print_r ($ stack); <br>?> </p><p>The example will output the following:</p><p>Array ( <br>=> orange <br>=> banana <br>=> apple <br>=> raspberry <br>) </p><p>Note that array_push () takes an array like a stack and always adds elements to the end of it. <br>The function is supported by PHP 4, PHP 5</p><p><b>array_shift</b></p><p>Retrieves and removes the first element in an array. <br>Syntax:</p><p>Mixed array_shift (array arr)</p><p>Array_shift () function retrieves the first element of the arr array and returns it. It closely resembles array_pop (), <br>but it only receives the initial, not the final element, and also produces a rather strong "shake-up" of the entire array: after all, when extracting the first element, you have to adjust all the numeric indices of all the remaining elements, since all subsequent elements of the array are shifted one position forward. The string keys of the array are not changed. <br>If arr is empty or is not an array, the function returns NULL.</p><p>After using this function, the array pointer moves to the beginning. <br>An example of using the array_shift () function:</p><p> <?php$stack = array("orange", "banana", "apple", "raspberry");<br>$ fruit = array_shift ($ stack); <br>print_r ($ stack); <br>?> </p><p>This example will output the following:</p><p>Array ( <br>=> banana <br>=> apple <br>=> raspberry <br>) </p><p>and the variable $ fruit will have the value "orange"</p><p>The function is supported by PHP 4, PHP 5</p><p><b>array_unshift</b></p><p>Adds one or more values ​​to the beginning of an array. <br>Syntax:</p><p>Int array_unshift (list arr, mixed var1 [, mixed var2, ...])</p><p>Array_unshift () adds the passed var values ​​to the beginning of the arr array. The order of the new elements in the array is preserved. All numeric indices of the array will be changed so that it starts at zero. All string indices in the array are unchanged. <br>The function returns the new number of elements in the array. <br>An example of using the array_unshift () function:</p><p> <?php$queue = array("orange", "banana");<br>array_unshift ($ queue, "apple", "raspberry"); <br>?> </p><p>Now the $ queue variable will have the following elements:</p><p>Array ( <br>=> apple <br>=> raspberry <br>=> orange <br>=> banana <br>) </p><p>The function is supported by PHP 4, PHP 5</p><p><b>array_unique</b></p><p>Removes duplicate values ​​in an array. <br>Syntax:</p><p>Array array_unique (array arr)</p><p>Array_unique () returns an array of all the unique values ​​in arr, along with their keys, by removing all duplicate values. The first encountered key => value pairs are placed in the resulting array. Indexes are saved. <br>An example of using the array_unique () function:</p><p> <?php$input = array("a" =>"green", "red", "b" => <br>"green", "blue", "red"); <br><br>print_r ($ result); <br>?> </p><p>The example will output the following:</p><p>Array ( <br>[a] => green <br>=> red <br>=> blue <br>) </p><p>An example of using the array_unique () function: Comparing data types</p><p> <?php$input = array(4, "4", "3", 4, 3, "3");<br>$ result = array_unique ($ input); <br>var_dump ($ result); <br>?> </p><p>The example will output the following:</p><p>Array (2) ( <br>=> int (4) <br>=> string (1) "3" <br>} </p><p>Function supported by PHP 4> = 4.0.1, PHP 5</p><p><b>array_chunk</b></p><p>The function splits the array into parts. <br>Syntax:</p><p>Array array_chunk (array arr, int size [, bool preserve_keys])</p><p>Array_chunk () function splits the original arr array into several arrays, the length of which is specified by the number size. If the dimension of the original array is not divisible by exactly size parts, then the last array will have a lower dimension. <br>The array_chunk () function returns a multidimensional array, the indices of which start from 0 to the number of arrays received, and the values ​​are the arrays obtained as a result of splitting. <br>The optional preserve_keys parameter specifies whether to preserve the keys of the original array or not. If this parameter is false (default value), then the indices of the resulting arrays will be specified as numbers starting from zero. If the parameter is true, then the keys of the original array are preserved. <br>An example of using the array_chunk () function:</p><p>$ array = array ("1st element", <br>"2nd element", <br>"3rd element", <br>"4th element", <br>"5th element"); <br>print_r (array_chunk ($ array, 2)); <br>print_r (array_chunk ($ array, 2, TRUE));</p><p>The example will output the following:</p><p>Array ( <br>=> Array <br>=> 1st element <br>=> 2nd element <br>)</p><p>=> Array <br>=> 3rd element <br>=> 4th element <br>)</p><p>=> Array <br>=> 5th element <br>)</p><p>)<br>Array ( <br>=> Array <br>=> 1st element <br>=> 2nd element <br>)</p><p>=> Array <br>=> 3rd element <br>=> 4th element <br>)</p><p>=> Array <br>=> 5th element <br>)</p><p>Function supported by PHP 4> = 4.2.0, PHP 5</p><p><b>array_fill</b></p><p>The function fills the array with specific values. <br>Syntax:</p><p>Array array_fill (int start_index, int num, mixed value)</p><p>The array_fill () function returns an array that contains the num-sized values ​​specified in the value parameter, starting at the element specified in the start_index parameter. <br>An example using array_diff_uassoc ():</p><p> <?php$a = array_fill(5, 6, "banana"); <br>print_r ($ a); <br>?> </p><p>The example will output the following:</p><p>Array ( <br>=> banana <br>=> banana <br>=> banana <br>=> banana <br>=> banana <br>=> banana <br>) </p><p>Function supported by PHP 4> = 4.2.0, PHP 5</p><p><b>array_filter</b></p><p>The function applies a filter to an array using a custom function. <br>Syntax:</p><p>Array array_filter (array input [, callback callback])</p><p>The array_filter () function returns an array that contains the values ​​in the input array, filtered according to the results of the custom callback function. <br>If the original input array is an associative array, the indices are stored in the resulting array. <br>An example of using the array_filter () function:</p><p> <?phpfunction odd($var) {<br>return ($ var% 2 == 1); <br>}</p><p>function even ($ var) ( <br>return ($ var% 2 == 0); <br>}</p><p>$ array1 = array ("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5); <br>$ array2 = array (6, 7, 8, 9, 10, 11, 12); <br>echo "Odd: n"; <br>print_r (array_filter ($ array1, "odd")); <br>echo "Even: n"; <br>t_r (array_filter ($ array2, "even")); <br>?> </p><p>The example will output the following:</p><p>Odd: Array ( <br>[a] => 1 <br>[c] => 3 <br>[e] => 5 <br>Even: Array ( <br> => 6<br> => 8<br> => 10<br> => 12<br>) </p><p>It is worth noting that instead of the name of the filtering function, you can specify an array that contains a reference to the object and the name of the method. <br>It is also worth noting that when processing an array using the array_filter () function, it cannot be changed: add, remove elements or zero the array, since this can lead to incorrect operation of the function. <br>Function supported by PHP 4> = 4.0.6, PHP 5</p> <br> <br> <script>document.write("<img style='display:none;' src='//counter.yadro.ru/hit;artfast_after?t44.1;r"+ escape(document.referrer)+((typeof(screen)=="undefined")?"": ";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth? screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+";h"+escape(document.title.substring(0,150))+ ";"+Math.random()+ "border='0' width='1' height='1' loading=lazy loading=lazy>");</script> </div> </article> <div class="post-bottom"> <div class="post-share"> <script src="//yastatic.net/es5-shims/0.0.2/es5-shims.min.js"></script> <script src="//yastatic.net/share2/share.js"></script> <div class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir" data-counter=""></div> </div> </div> <div class='yarpp-related'> <div class="related-items"> <div class="headline">Did not find an answer to your question? Look at here</div> <div class="items"> <div class="related-item"> <a class="related-item__title" href="https://appcube.ru/en/upravlyaemaya-raspredelennaya-arhitektura-arhitektura-raspredelennoi-sistemy-upravleniya-na-osnove-reko.html"><img src="/uploads/e4c78289b8726a7f311fa0aabf5cf29e.jpg" width="120" height="120" alt="Architecture of a distributed control system based on a reconfigurable multi-pipeline computing environment L-Net"прозрачные" распределенные файловые системы" class="related-item__image" / loading=lazy loading=lazy>Architecture of a distributed control system based on a reconfigurable multi-pipeline computing environment L-Net "transparent" distributed file systems</a> <div class="related-item__comments"><span></span></div> </div> <div class="related-item"> <a class="related-item__title" href="https://appcube.ru/en/skanirovanie-i-otpravka-elektronnoi-pochty-na-hp-laserjet-m5025-stranica.html"><img src="/uploads/c614635382cdebd6920dbfa329f4789a.jpg" width="120" height="120" alt="Email sending page Fill relay_recipients file with addresses from Active Directory" class="related-item__image" / loading=lazy loading=lazy>Email sending page Fill relay_recipients file with addresses from Active Directory</a> <div class="related-item__comments"><span></span></div> </div> <div class="related-item"> <a class="related-item__title" href="https://appcube.ru/en/pereklyuchenie-yazyka-na-klaviature-programma-propala-yazykovaya.html"><img src="/uploads/c5be3ecb817093adcd97d7fd3f79ec9f.jpg" width="120" height="120" alt="Missing language bar in Windows - what to do?" class="related-item__image" / loading=lazy loading=lazy>Missing language bar in Windows - what to do?</a> <div class="related-item__comments"><span></span></div> </div> </div> </div> </div> <div style="text-align: center; margin-top: 15px; margin-bottom: 15px; " id="vanna-1965575812"><div class="adsense"><script type="text/javascript">ga_1();</script></div></div> </main> <aside class="sidebar"> <div class="advices" data-theme="vannapedia_v.3"> <div class="headline"></div> <div class="advices-content"> <img src="/uploads/d3c9c835a316a04f113cf710a2a6243c.jpg" width="120" height="120" alt="Macro execution Ways to execute macros" class="advices__image" / loading=lazy loading=lazy> <div class="advices__title" data-id="3334"><a href="https://appcube.ru/en/sozdanie-i-ispolzovanie-makrokomand-vypolnenie-makrosa-sposoby.html">Macro execution Ways to execute macros</a></div> </div> </div> <div class="vk-widget" id="text-3"> <div class="textwidget"><script type="text/javascript" src="//vk.com/js/api/openapi.js?130"></script> <div id="vk_groups"></div> </div> </div> <div class="sidebar-questions"> <div class="headline">New</div> <ul> <li><a href="https://appcube.ru/en/oformlenie-listingov-programm-listing-osnovnoi-programmy-chto-takoe.html" >Listing of the main program What is a listing in programming</a></li> <li><a href="https://appcube.ru/en/ustanovlenie-svyazei-mezhdu-sushchnostyami-proektirovanie-modeli-v-erwin-erwin.html" >Designing a Model in ERWin Erwin Examples</a></li> <li><a href="https://appcube.ru/en/soobshchenie-vasha-tranzakciya-uspeshno-zavershena-chto-takoe-tranzakciya-po.html" >What is a bank card transaction</a></li> <li><a href="https://appcube.ru/en/rukovodstvo-po-bystromu-vyboru-ssylki-na-skachivanie-besplatnyh.html" >Quick Selection Guide (links to download free programs for replacing and editing icons) Download the program for creating ico</a></li> <li><a href="https://appcube.ru/en/programma-dlya-ochistki-istorii-kompyutera-udalenie-istorii.html" >Deleting your browsing history on the Internet</a></li> <li><a href="https://appcube.ru/en/kak-sozdat-svoi-komiks-sozdanie-komiksov-onlain-programma-gde-est.html" >Create comics online Program with comics</a></li> </ul> </div> <div class="section"> <div id="macire1" style="height:500px;width:240px;" align="center"></div> </div> <div class="section"> <div class="headline">Popular articles</div> <ul class="sidebar-posts"> <li><a href="https://appcube.ru/en/bazovaya-zashchita-pk-kakoi-antivirus-vybrat-ukraincy-otkazyvayutsya.html"><img src="/uploads/8b27640486c2e86bdd989505ace23fdf.jpg" width="80" height="80" alt="What antivirus to choose: Ukrainians refuse Russian software Hackers do not sleep" / loading=lazy loading=lazy>What antivirus to choose: Ukrainians refuse Russian software Hackers do not sleep</a></li> <li><a href="https://appcube.ru/en/osnovnye-komandy-postgresql-osnovnye-komandy-postgresql-poisk-i.html"><img src="/uploads/fb3321549c16f1aedad4133f0038b115.jpg" width="80" height="80" alt="Basic PostgreSQL Commands Finding and Changing the Location of a Cluster Instance" / loading=lazy loading=lazy>Basic PostgreSQL Commands Finding and Changing the Location of a Cluster Instance</a></li> <li><a href="https://appcube.ru/en/kanal-sredizemnoe-i-krasnoe-more-sueckii-kanal-granica-mezhdu-dvumya.html"><img src="/uploads/db9d7a789a0ed701639e7e7630ff91ee.jpg" width="80" height="80" alt="Suez Canal - border between two continents" / loading=lazy loading=lazy>Suez Canal - border between two continents</a></li> </ul> </div> <div class="section"> <div class="headline">New on the site</div> <ul class="sidebar-posts sidebar-photo"> <li><a href="https://appcube.ru/en/zaregistrirovannye-19-oktyabrya-v-odnoklassnikah-odnoklassniki.html">Odnoklassniki: Registration and profile creation</a></li> <li><a href="https://appcube.ru/en/e-yavlyaetsya-e-funkcii-e-vyrazheniya-cherez-trigonometricheskie.html">E is. E (functions E). Expressions in terms of trigonometric functions</a></li> <li><a href="https://appcube.ru/en/podrobnyi-katalog-socialnyh-setei-socseti-rossii-seichas-v-soc.html">Social networks of Russia Now in social networks</a></li> <li><a href="https://appcube.ru/en/neostorozhnyi-foros-otkryt-levoe-menyu-foros-put-ot-aeroporta.html">Open left menu foros</a></li> <li><a href="https://appcube.ru/en/analitik-tv-na-yutube-videoanalitika-v-sistemah-videonablyudeniya.html">Video analytics in video surveillance systems</a></li> </ul> </div> </aside> </div> <footer class="footer"> <nav class="footer__nav"><ul><li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-1219"><a href="https://appcube.ru/en/">New</a> <ul class="sub-menu"> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6692"><a href="https://appcube.ru/en/pyat-sposobov-proyasnit-situaciyu-deistvennye-uprazhnenie.html">Actionable Exercise to Clarify and Change the Situation Clarify the Situation</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6691"><a href="https://appcube.ru/en/tatarskaya-klaviatura-dlya-android-download-tatarskaya-klaviatura-for-pc-kak.html">Download Tatar Keyboard for PC How to Write Letters in Tatar</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6689"><a href="https://appcube.ru/en/procedury-v-paskale-prezentaciya-procedury-i-funkcii-v-paskale.html">Procedures and functions in Pascal</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6688"><a href="https://appcube.ru/en/sreda-programmirovaniya-paskal-avs-prezentaciya-yazyk-programmirovaniyaabc-pascal-prezentaciya-k-uroku.html">Programming language ABC Pascal presentation for a lesson in computer science and ICT on the topic</a></li> </ul> </li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-1230"><a href="https://appcube.ru/en/">Popular</a> <ul class="sub-menu"> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6687"><a href="https://appcube.ru/en/prezentaciya-po-biologii-na-temu-virusy-i-fagi-10-klass-prezentaciya.html">Biology presentation on "Viruses and Phages" (Grade 10) Phage Typing S</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6686"><a href="https://appcube.ru/en/chto-takoe-rastrovaya-grafika-i-gde-e-primenenie-rastrovaya-grafika-obshchie.html">Raster graphics, general information - lecture The concept of a raster image</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6685"><a href="https://appcube.ru/en/prezentaciya-na-temu-pamyat-kompyutera-vnutrennyaya-pamyat.html">Internal Computer Memory High Density Blu-ray Discs</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6684"><a href="https://appcube.ru/en/postoyannyi-elektricheskii-tok-ponyatie-ob-elektricheskom-toke.html">Presentation on physics "Electric current in different environments" Light presentation on the topic of electrical current</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6683"><a href="https://appcube.ru/en/razrabotka-uroka-i-prezentaciya-po-okruzhayushchemu-miru-pochemu-listya.html">Or the role of green leaves for plants and humans "</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6682"><a href="https://appcube.ru/en/pochemu-listya-zelenye-prezentaciya-prezentaciya-pochemu-rasteniya-zelenye.html">Presentation why plants are green</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6680"><a href="https://appcube.ru/en/veb-kvest-kak-sposob-aktivizacii-uchebnoi-deyatelnosti-uchashchihsya.html">Web quest as a way of enhancing the educational activities of students Creation of web quests</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6679"><a href="https://appcube.ru/en/statisticheskaya-obrabotka-dannyh-i-ee-osobennosti-prezentaciya-na.html">Presentation on the topic "elements of statistical data processing" The main objectives of studying the elements of statistics</a></li> </ul> </li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-1236"><a href="https://appcube.ru/en/">Recommended</a> <ul class="sub-menu"> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6678"><a href="https://appcube.ru/en/istoriya-sozdaniya-dvoichnogo-kodirovaniya-prezentaciya-dvoichnoe.html">Binary coding Information and information processes</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6677"><a href="https://appcube.ru/en/ustanovka-plansheta-apple-ipad-v-avtomobil-derzhateli-dlya-ipad-ipad-air-ipad.html">Holders for iPad, iPAd Air, iPad mini for a car with mounting in a CD slot, on a dashboard, on a headrest Car holders for iPad mini</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6676"><a href="https://appcube.ru/en/zalipaet-knopka-gromkosti-na-telefone-lyuft-knopok-gromkosti-i-vklyucheniya-na.html">Backlash of the volume and power buttons on the iPhone - a marriage or not?</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6675"><a href="https://appcube.ru/en/setevaya-karta-ne-vidit-kabel-sposoby-resheniya-problemy.html">The network card does not see the cable: instructions for solving the problem What to do if the Internet cable does not work</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6674"><a href="https://appcube.ru/en/mobilnoe-prilozhenie-lenta-gipermarket-skachat-na-aifon-stocard.html">StoCard and Wallet: discount cards from the application</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6672"><a href="https://appcube.ru/en/kak-delaetsya-skrinshoty-na-aipade-kak-sdelat-skrinshot-na-iphone.html">How to take a screenshot on iPhone, iPad or iPod in five ways How to do it</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6671"><a href="https://appcube.ru/en/znakomstva-bez-registracii-premium-i-vip-akkauntov-smski-molodoi-i-besplatnyi.html">Smski: young and free</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6670"><a href="https://appcube.ru/en/svetofor-s-dvumya-krasnymi-signaly-svetofora-pravila-dorozhnogo.html">Traffic light with two red</a></li> </ul> </li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-6898"><a href="https://appcube.ru/en/">About the site</a> <ul class="sub-menu"> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6900"><a href="">About the site</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6901"><a href="">Advertising on the website</a></li> <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-6902"><a href="">Contacts</a></li> </ul> </li> </ul></nav> <div class="footer-bottom"> <div class="footer-left"> <div class="foot__logo"> <div class="footer__logo-sitename">appcube.ru <span>ru</span></div> </div> <style> .foot__logo { min-height: 35px; margin: 0 0 11px -79px; padding: 9px 0 0 79px; text-decoration: none; } </style> <p>© 2021 All rights reserved</p> <p>Website about computers</p> <ul class="footer-bottom__nav"> <li><a href="" >Advertising on the project</a></li> </ul> </div> <div class="footer-buttons"> </div> <ul class="footer__soc"> <li><a href="http://vk.com/" target="_blank" class="vk">In contact with</a></li> <li>classmates</li> <li><a href="http://www.facebook.com/" target="_blank" class="fb">Facebook</a></li> <li><a href="https://twitter.com/" target="_blank" class="twi">Twitter</a></li> </ul> <div class="footer-right"> <div class="footer__note"></div> <div class="footer__counters" id="text-2"> <div class="textwidget"></div> </div> </div> </div> </footer> </div> </div> <link rel='stylesheet' id='wp-lightbox-bank.css-css' href='/wp-content/plugins/wp-lightbox-bank/assets/css/wp-lightbox-bank.css?ver=4.8.3' type='text/css' media='all' /> <script type='text/javascript' src='https://appcube.ru/wp-content/themes/vannapedia_v.3/js/scripts.js'></script> <script type='text/javascript' src='/wp-includes/js/comment-reply.min.js?ver=4.8.3'></script> <script type='text/javascript' src='/assets/scripts1.js'></script> <script type='text/javascript'> /* <![CDATA[ */ var tocplus = { "smooth_scroll":"1"} ; /* ]]> */ </script> <script type='text/javascript' src='https://appcube.ru/wp-content/plugins/table-of-contents-plus/front.min.js?ver=1509'></script> <script type='text/javascript'> var q2w3_sidebar_options = new Array(); q2w3_sidebar_options[0] = { "sidebar" : "sidebar-fixed", "margin_top" : 10, "margin_bottom" : 0, "stop_id" : "respond", "screen_max_width" : 0, "screen_max_height" : 0, "width_inherit" : false, "refresh_interval" : 1500, "window_load_hook" : false, "disable_mo_api" : false, "widgets" : ['text-4'] } ; </script> <script type='text/javascript' src='https://appcube.ru/wp-content/plugins/q2w3-fixed-widget/js/q2w3-fixed-widget.min.js?ver=5.0.4'></script> <script type='text/javascript' src='/wp-includes/js/wp-embed.min.js?ver=4.8.3'></script> <script type='text/javascript' src='https://appcube.ru/wp-content/plugins/wp-lightbox-bank/assets/js/wp-lightbox-bank.js?ver=4.8.3'></script> <script type='text/javascript' src='https://appcube.ru/wp-content/plugins/akismet/_inc/form.js?ver=4.0'></script> </body> </html>