Уроки PHP — массивы (array) — удаление данных из массива

Сегодня вечером мы рассмотрим возможность как удаления данных из массива, так и массива целиком.

Все просто:

Конструкция

<code class="php"><span class="preprocessor">&lt;?php</span>
<span class="variable">$array</span> = <span class="keyword">array</span>(<span class="string">"red"</span>, <span class="string">"blue"</span>, <span class="string">"green"</span>);
<span class="keyword">unset</span>(<span class="variable">$array</span>[<span class="number">2</span>]);
<span class="preprocessor">?&gt;
позволяет удалить из массива значение "green"
тогда как код</span></code>
<code class="php"><span class="preprocessor">&lt;?php</span>
<span class="keyword">unset</span>(<span class="variable">$array</span>);
<span class="preprocessor">?&gt;
удаляет массив целиком. В целом (тут я повторяюсь, но что делать, если так оно и есть) - все просто, важно понять логику и опробовать свежеузнанный код на практике ;) 
Итак, у нас имеется следующий код: 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Modifying Elements&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;?php
$languages = array("HTML/CSS",
"JavaScript", "PHP", "Python", "Ruby");
// Write the code to remove Python here!
// Write your code above this line. Don't
// worry about the code below just yet; we're
// using it to print the new array out for you!
foreach($languages as $lang) {
print "&lt;p&gt;$lang&lt;/p&gt;";
}
?&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
из которого с помощью команды unset нужно удалить значение "</span></code><code class="php"><span class="preprocessor">Python</span></code><code class="php"><span class="preprocessor">".
Для начала вспомним, каким образом задается массив вообще: 
</span></code>
<code class="php"><span class="preprocessor"> $languages = array("HTML/CSS",
"JavaScript", "PHP", "Python", "Ruby");</span></code>
Таким образом, команда unset ($название-массива[поле]) позволяет удалить нужное значение. Повторю, что отсчет в массивах идет с нуля, таким образом наша команда будет выглядеть следующим образом: 
$unset ($languages[3]);
а код в полном виде выглядит: 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Modifying Elements&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;?php
$languages = array("HTML/CSS",
"JavaScript", "PHP", "Python", "Ruby");
// Write the code to remove Python here!
unset ($languages [3]);
// Write your code above this line. Don't
// worry about the code below just yet; we're
// using it to print the new array out for you!
foreach($languages as $lang) {
print "&lt;p&gt;$lang&lt;/p&gt;";
}
?&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
<code class="php"><span class="preprocessor"><a href="https://lavrynenko.com/wp-content/uploads/2015/06/php-lesson-array-1-del-data-1-1.jpg"><img class="alignleft size-medium wp-image-1217" src="https://lavrynenko.com/wp-content/uploads/2015/06/php-lesson-array-1-del-data-1-300x181.jpg" alt="php-lesson-array-del-data" width="300" height="181" /></a>Спасибо за внимание! 
Если вам понравится этот урок - сделайте репост к себе на страницу ;) 
</span></code>

Support the Blog!

Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️

PayPal Logo Donate via PayPal

Revolut Logo Donate via Revolut