PHP: Adding and removing array elements. Adding elements to an array Create an array and add data php

PHP 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.

This article talks about creating an array, adding elements to an array. There are many built-in functions that work with arrays in PHP 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.

Indexed and associative arrays

There are two types of arrays in PHP: index and associative. The keys of an indexed array are integers starting from 0. Indexed arrays are used when you require a specific position in the array. Associative arrays behave like two columns of a table. The first column is the key, which is used to access the value (the second column).

PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is that the keys appear. Some functions are provided primarily 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 whether the key is a string or an integer.

IN PHP arrays have an internal order of their elements that is independent of keys and values, and there are functions that can be used to traverse arrays based on this internal order.

Defining elements in an array

You can access specific values ​​from an array by using the array name followed by the element key (sometimes called index) in square brackets:

$age["Fred"]; $shows;

The key can be a string or an integer. String values ​​as numbers (without leading zeros) are treated as integers. Thus, $array And $array['3'] refer to the same element, but $array[’03’] 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 Perl.

It is not necessary to write the key in quotes. For example, $array['Fred'] like $arrat. However, it is considered good style PHP always use quotes. If the index is without quotes, then PHP uses the value of the constant as the index:

Define("index",5); echo $array; // will return $array, not $array["index"];

If you want to substitute a number into the index, you need to do this:

$age["Clone$number"]; // will return, for example $age["Clone5"];

However, do not quote the key in the following case:

// incorrect print "Hello, $person["name"]"; print "Hello, $person["name"]"; // correct print "Hello, $person";

Storing data in arrays

When you try to store a value in an array, the array will be automatically created if it did not exist previously, but when you try to retrieve a value from an array that has not been defined, the array will not be created. For example:

// $addresses is not defined until now echo $addresses; // nothing echo $addresses; // nothing $addresses = " [email protected]"; echo $addresses; // print "Array"

You can use a simple assignment to initialize an array in a program:

$addresses = " [email protected]"; $addresses = " [email protected]"; $addresses = " [email protected]"; // ...

We declared an index array with integer indices starting at 0.

Associative array:

$price["Gasket"] = 15.29; $price["Wheel"] = 75.25; $price["Tire"] = 50.00; // ...

An easier way to initialize an array is to use the construct Array(), which builds an array from its arguments:

$addresses = array(" [email protected]", "[email protected]", "[email protected]");

To create an associative array using Array(), use => symbol separating indices from values:

$price = array("Gasket" => 15.29, "Wheel" => 75.25, "Tire" => 50.00);

Pay attention to the use of spaces and alignment. We could group the code, but it would be less clear:

$price = array("Gasket"=>15.29,"Wheel"=>75.25,"Tire"=>50.00);

To create an empty array, you need to call the construct Array() without arguments:

$addresses = Array();

You can specify a starting key in an array and then a list of values. Values ​​are entered into the array, starting with the key and then increasing:

$days = array(1 => "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"); // 2 is Tuesday, 3 is Wednesday, etc.

If the starting index is a string, then subsequent indices become integers, starting at 0. So the following code is probably an error:

$whoops = array("Friday" => "Black", "Brown", "Green"); // same as $whoops = array("Friday" => "Black", 0 => "Brown", 1 => "Green");

Adding a new element to the end of an array

To insert multiple values ​​at the end of an existing indexed array, use the syntax:

$family = array("Fred", "Wilma"); // $family = "Fred" $family = "Pebbles"; // $family = "Pebbles"

This construct assumes that array indices are numbers and assigns the element the next available numeric index, starting at 0. Trying to add an element to an associative array is almost always a programmer error, but PHP will add new elements with numeric indices (starting from 0) without issuing a warning:

$person = array("name" => "Fred"); // $person["name"] = "Fred"; $person = "Wilma"; // $person = "Wilma"

At this stage, we will finish the introductory part of working with arrays in PHP. I look forward to seeing you in the next article.

Adding elements to an array

If the array exists, you can add additional elements 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 don’t have to specify the key of the added element, but in any case, square brackets are required when accessing the array. Adding two new elements to $List, we write:

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

If the key is not specified, each element will be added to the existing array and indexed by the next ordinal 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 index 5. When you explicitly specify an index, and the value with it is already exists, the existing value at that location will be lost and replaced with a new one:

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

Now the value of the element with 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 you specifically want to overwrite any existing data. However, if strings are used as indexes, 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. By first 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 a text editor.
  2. After initializing the array using 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, it can be printed.

  4. Add three additional elements to the array.
  5. $Soups["Thursday"] = "Chicken Noodle";
    $Soups["Friday"] = "Tomato";
    $Soups["Saturday"] = "Cream of Broccoli";
  6. Count 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 the browser (Fig.).

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 an 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 new feature that allows you to add one array to another. This operation can also be called merging or concatenation of arrays. The array_merge() function is called as follows:</p><p>$NewArray = array_merge($OneArray, $TwoArray);</p><p>You can rewrite the soups.php page using this function if you are working on a server that has PHP 4.0 installed.</p> <p>Merging 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 also print the result.</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>Combine two arrays into one.</li>$TheSoups = array_merge($Soups, $Soups2); <p>Make sure that the arrays are arranged in this order ($Soups, then $Soups2), that is, the elements of Thursday and Friday should be added to the elements of Monday of Wednesday, 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 PHP and the HTML document.</li> ?></BODYx/HTML> <li>Save the file (Listing 7.3), upload it to the server and test it in the browser (Fig.).</li> </ol><img src='https://i1.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 features in PHP 4.0 designed to work with arrays. Using arrays you can save 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 an array directly. This is done correctly like this: $Ar ray = "Add This"; iyai$Aggau = "Add This";, but it’s correct like this: $Aggau = "Add This";. If you forget to put the parentheses, 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 discussed 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>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 certain cases.</p><h2>"Operator +"</h2><p>This is a simple but insidious way:</p><p>$c = $a + $b</p><p><b>This way, only those keys are added 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 array $b is not present in the array $a, 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, changing the places of the terms changes the sum: $a + $b != $b + $a - this is worth remembering.</b></p><p>Now here's 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 string ones. Great for concatenating two or more arrays with numeric indexes:</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 added to the end of the array.</p> </blockquote><h2>array_merge_recursive function</h2><p>Does the same thing as array_merge except it recursively goes through each branch of the array and does the same with the children.</p><h2>array_replace() function</h2><p>Replaces array elements with elements of other passed arrays.</p><h2>array_replace_recursive() function</h2><p>Same as array_replace but processes all branches of the array.</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>The operation of assigning a value to an array element is the same as the operation of assigning a value to a variable, except for the square brackets () that are added after the array variable name. The index/key of the element is indicated in square brackets. If no index/key is specified, PHP will automatically select 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 the index/key $my_arr = "four"; $my_arr = "five"; echo " <br>"; var_dump($my_arr); ?></p><p>To change a specific value, you simply assign a new value to an existing element. To remove any element of an array with its index/key or 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 increased by 1. If there are no integer indexes in the array yet, then the key will be 0 (zero).</p> <p>Note that the largest integer value of the key <b>does not necessarily exist in the array at the moment</b>, this may be due to the removal of array elements. After elements have been removed, the array is not reindexed. Let's take 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 the element (note that the new key will be 3 instead of 0). $my_arr = 6; echo " <br>"; print_r($my_arr); // Do reindexing: $my_arr = array_values($my_arr); $my_arr = 7; echo " <br>"; print_r($my_arr); ?></p><p>This example used two new functions, print_r() and array_values(). The array_values() function returns an indexed array (re-indexes the returned array with numeric indices), and the print_r function works like var_dump but outputs arrays in a more readable form.</p> <p>Now we can look at the third way to create arrays:</p><p> <?php // следующая запись создает массив $weekdays = "Понедельник"; $weekdays = "Вторник"; // тоже самое, но с указанием индекса $weekdays = "Понедельник"; $weekdays = "Вторник"; ?> </p><p>The example showed a third way to create an array. If the $weekdays array has not yet been created, 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, it may cause unexpected results from the script.</p> <p>If you are in doubt about whether a variable is an array, use the is_array function. For example, the check can be done 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>array_pad</b></p><p>Adds several elements to the array. <br>Syntax:</p><p>Array array_pad(array input, int pad_size, mixed pad_value)</p><p>The array_pad() function 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 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 value of pad_size 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>Example of using 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>Apply 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 specified arrays after processing by the user callback function. <br>The number of parameters passed to the user-defined function must match the number of arrays passed to array_map().</p><p>Example of using the array_map() function: Processing a single 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>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 given example will output the following:</p><p>// printout of $cArray( <br>=> Number 1 in Spanish - uno <br>=> Number 2 in Spanish - dos <br>=> Number 3 in Spanish - tres <br>=> Number 4 in Spanish - cuatro <br>=> Number 5 in Spanish - 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 used on arrays that have the same size. If 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>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 given 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 pops the last element from the array arr and returns it, removing it afterwards. With this function we can build stack-like structures. If the array arr was empty, or it is not an array, the function returns the empty string NULL.</p><p>After using the array_pop() function, the array cursor is set to the beginning. <br>Example of using 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>Function supported by PHP 4, PHP 5</p><p><b>array_push</b></p><p>Adds one or more elements to the end of the array. <br>Syntax:</p><p>Int array_push(array arr, mixed var1 [, mixed var2, ..])</p><p>The array_push() function adds elements var1, var2, etc. to the array arr. It assigns numeric indexes to them - exactly as it does for standard . <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 thing, but shorter</p><p>Example of using 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>Please note that the array_push() function treats the array as a stack and always adds elements to the end. <br>Function supported by PHP 4, PHP 5</p><p><b>array_shift</b></p><p>Retrieves and removes the first element of an array. <br>Syntax:</p><p>Mixed array_shift(array arr)</p><p>The array_shift() function takes the first element of the array arr and returns it. It is very similar to 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 remaining elements, because all subsequent elements of the array are shifted one position forward. The string array keys do not change. <br>If arr is empty or not an array, the function returns NULL.</p><p>After using this function, the array pointer is moved to the beginning. <br>Example of using 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 $fruit variable will have the value "orange"</p><p>Function supported by PHP 4, PHP 5</p><p><b>array_unshift</b></p><p>Adds one or more values ​​to the beginning of the array. <br>Syntax:</p><p>Int array_unshift(list arr, mixed var1 [,mixed var2, ...])</p><p>The array_unshift() function adds the passed var values ​​to the beginning of the arr array. The order of new elements in the array is preserved. All digital indexes of the array will be changed so that it starts from zero. All string indexes of 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>Function 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>The array_unique() function returns an array composed of all the unique values ​​in the array arr along with their keys, by removing all duplicate values. The first key=>value pairs encountered are placed in the resulting array. The indexes are preserved. <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>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>The array_chunk() function splits the original array arr into several arrays, the length of which is specified by the number size. If the dimension of the original array is not divisible exactly by the size of the parts, then the final array will have a smaller dimension. <br>The array_chunk() function returns a multidimensional array, the indices of which start from 0 to the number of resulting arrays, and the values ​​are the arrays obtained as a result of splitting. <br>The optional preserve_keys parameter specifies whether the keys of the original array should be preserved or not. If this parameter is false (the default value), then the indices of the resulting arrays will be specified by numbers starting from zero. If the parameter is true, then the keys of the original array are preserved. <br>Example of using 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 containing the values ​​specified in the value parameter of size num, starting with the element specified in the start_index parameter. <br>Example of 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 ​​present in the input array, filtered according to the results of the user-defined callback function. <br>If the input array is an associative array, the indices are preserved in the resulting array. <br>Example of using 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 with the array_filter() function, it cannot be changed: add, remove elements or reset the array, because this may lead to incorrect operation of the function. <br>Function supported by PHP 4 >= 4.0.6, PHP 5</p> <script type="text/javascript"> <!-- var _acic={dataProvider:10};(function(){var e=document.createElement("script");e.type="text/javascript";e.async=true;e.src="https://www.acint.net/aci.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})() //--> </script><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="related_block"> <div class="title">Related publications</div> <ul class="recent_ul"> <li> <div class="img_block"> <div class="overlay"></div> <img src="/uploads/db6ca8be8505d7849196b41a0b14d818.jpg" style="width:230px; height:145px;" / loading=lazy loading=lazy></div> <a href="https://storerarity.ru/en/test-i-obzor-smartfona-motorola-moto-g6-plus-gigant-g6-serii-motorola-moto-g--.html">Motorola Moto G - Specifications Motorola Moto G5 Plus Specifications and Test Results</a></li> <li> <div class="img_block"> <div class="overlay"></div> <img src="/uploads/34ae1de6dc558eb702e7fae80b9fe9c2.jpg" style="width:230px; height:145px;" / loading=lazy loading=lazy></div> <a href="https://storerarity.ru/en/interfeis-i-navigaciya-funkcionalnye-vozmozhnosti.html">Functionality</a></li> <li> <div class="img_block"> <div class="overlay"></div> <img src="/uploads/abcfb3f71140ad06551350ad316a8fdc.jpg" style="width:230px; height:145px;" / loading=lazy loading=lazy></div> <a href="https://storerarity.ru/en/izmenit-imya-uchetki-v-skaipe-kak-smenit-login-v-skype-kak-udalit.html">How to change your Skype login</a></li> </ul> </div> </div> <aside id="sidebar"> <div class="block"> <nav class="sidebar_menu"> <div class="menu-sidebar_menu-container"> <ul id="menu-sidebar_menu" class="menu"> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/programs/">Programs</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/windows/">Windows</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/devices/">Devices</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/browsers/">Browsers</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/reviews/">Reviews</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/reviews/">Reviews</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/setup/">Settings</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/torrent/">Torrent</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/editors/">Editors</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/instagram/">Instagram</a></li> </ul> </div> </nav> </div> <div class="block recent_block"> <div class="title">The last notes</div> <ul class="popular"> <li> <div class="img_block"> <div class="overlay"></div> <img width="240" height="145" src="/uploads/c57c2fcd03116e21d413bf0b21d5c91f.jpg" class="attachment-popular_thumb size-popular_thumb wp-post-image" alt="Organization of exchange with the base of a branch (retail store) in a trading network via XML (universal exchange)" / loading=lazy loading=lazy> <span class="cat">Reviews</span></div> <a href="https://storerarity.ru/en/1s-kak-sdelat-obmen-dannymi-organizaciya-obmena-s-bazoi-filiala.html">Organization of exchange with the base of a branch (retail store) in a trading network via XML (universal exchange)</a></li> <li> <div class="img_block"> <div class="overlay"></div> <img width="240" height="145" src="/uploads/15bc70b99a6682c01208b569e7cdb695.jpg" class="attachment-popular_thumb size-popular_thumb wp-post-image" alt="How to remove numbering from individual pages in Word" / loading=lazy loading=lazy> <span class="cat">Reviews</span></div> <a href="https://storerarity.ru/en/kak-ubrat-numeraciyu-stranic-s-titulnogo-lista-kak-ubrat-numeraciyu-s.html">How to remove numbering from individual pages in Word</a></li> <li> <div class="img_block"> <div class="overlay"></div> <img width="240" height="145" src="/uploads/94b4df2eca2f27fc290c84bc05785c19.jpg" class="attachment-popular_thumb size-popular_thumb wp-post-image" alt="Review of the network company NL International: products, marketing plan, reviews" / loading=lazy loading=lazy> <span class="cat">Reviews</span></div> <a href="https://storerarity.ru/en/vhod-v-lichnyi-ofis-nl-obzor-setevoi-kompanii-nl-international-produkty.html">Review of the network company NL International: products, marketing plan, reviews</a></li> </ul> </div> </aside> </div> </div> <div class="clear"></div> <footer id="footer"><div class="wrapper"> <div class="copy">2024 | Computers for everyone - Setup, installation, recovery</div> <nav class="header_menu"> <div class="menu-footer_menu-container"> </div> </nav> </div></footer> <div id="toTop"></div> <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script> <nav class="mobile_menu"> <div class="close_menu"></div> <div class="mob_menu"> <div class="menu-mobile_menu-container"> <ul id="menu-mobile_menu" class="menu"> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/programs/">Programs</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/windows/">Windows</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/devices/">Devices</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/browsers/">Browsers</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/reviews/">Reviews</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/reviews/">Reviews</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/setup/">Settings</a></li> <li id="menu-item-" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-"><a href="https://storerarity.ru/en/category/torrent/">Torrent</a></li> </ul> </div> </div> </nav> <script type="text/javascript"> <!-- var _acic={dataProvider:10};(function(){var e=document.createElement("script");e.type="text/javascript";e.async=true;e.src="https://www.acint.net/aci.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})() //--> </script><br> <br> </body> </html> <script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>