Массивы

На самом деле массив в PHP - это упорядоченное отображение, которое устанавливает соответствие между значением и ключем. Этот тип оптимизирован в нескольких направлениях, поэтому вы можете использовать его как собственно массив, список (вектор), хэш-таблицу (являющуюся реализацией карты), словарь, коллекцию, стэк, очередь или, возможно, как что-то еще. Поскольку вы можете иметь в качестве значения другой массив PHP, вы можете также легко эмулировать деревья.

Объяснение этих структур данных выходит за рамки данного справочного руководства, но вы найдете как минимум один пример каждой из них. За дополнительной информацией вы можете обратиться к соответствующей литературе по этой обширной теме.

Синтаксис

Определение при помощи array()

Массив может быть создан языковой конструкцией array(). В качестве параметров она принимает определенное количество разделенных запятыми пар key => value (ключ => значение).

array( [key =>] value
     , ...
     )
// key может быть integer или string
// value может быть любым значением

<?php
$arr
= array("foo" => "bar", 12 => true);

echo
$arr["foo"]; // bar
echo $arr[12];    // 1
?>

key может быть либо integer, либо string. Если ключ - это стандартное представление integer, он так и будет интерпретироваться (т.е. "8" будет восприниматься как 8, тогда как "08" будет интерпретироваться как "08"). В PHP нет разницы между индексными и ассоциативными массивами; существует только один тип массива, который может содержать и числовые, и строковые индексы.

Значение может быть любого имеющегося в PHP типа.

<?php
$arr
= array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo
$arr["somearray"][6];    // 5
echo $arr["somearray"][13];  // 9
echo $arr["somearray"]["a"];  // 42
?>

Если вы не указываете ключ для приведенного значения, то берется максимальный числовой индекс и новый ключ будет равен этому максимуму + 1. Если вы укажите ключ, которому уже присвоено значение, оно будет перезаписано.

<?php
// Этот массив эквивалентен ...
array(5 => 43, 32, 56, "b" => 12);

// ...этому массиву
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>

Внимание

Начиная с PHP 4.3.0, вышеописанное поведение генерации индекса изменено. Теперь, если вы будете использовать массив, в котором максимальным в настоящий момент является отрицательный ключ, то следующий созданный ключ будет нулевым (0). Раньше новым индексом становился самый большой существующий ключ + 1, так же как и у положительных индексов.

Используя в качестве ключа TRUE вы получите ключ 1 типа integer. Используя в качестве ключа FALSE вы получите ключ 0 типа integer. Используя в качестве ключа NULL, вы получите пустую строку. Использование в качестве ключа пустой строки создаст (или перезапишет) ключ с пустой строкой и его значение; это не то же самое, что использование пустых квадратных скобок.

Вы не можете использовать в качестве ключей массивы или объекты. Это вызовет предупреждение: Illegal offset type ('Недопустимый тип смещения').

Создание/модификация с помощью синтаксиса квадратных скобок

Также вы можете изменять существующий массив, явно устанавливая значения в нем.

Это выполняется присвоением значений массиву при указании в скобках ключа. Кроме того, вы можете опустить ключ, в этом случае добавьте к имени переменной пустую пару скобок ("[]").
$arr[key] = value;
$arr[] = value;
// key может быть integer или string
// value может быть любым значением
Если массив $arr еще не существует, он будет создан. Таким образом, это еще один способ определить массив. Для изменения определенного значения просто присвойте элементу с его ключом новое значение. Если вы хотите удалить пару ключ/значение, вам нужно использовать функцию unset().

<?php
$arr
= array(5 => 1, 12 => 2);

$arr[] = 56;    // В этом месте скрипта это
               // эквивалентно $arr[13] = 56;

$arr["x"] = 42; // Это добавляет к массиву новый
               // элемент с ключом "x"
              
unset($arr[5]); // Это удаляет элемент из массива

unset($arr);    // Это удаляет массив полностью
?>

Замечание: Как уже говорилось выше, если вы не укажите в скобках ключа, то будет взят максимальный из существующих целочисленных индексов, и новым ключом будет это максимальное значение + 1. Если целочисленных индексов еще нет, то ключом будет 0 (ноль). Если вы укажите ключ, которому уже присвоено значение, оно будет перезаписано.

Внимание

Начиная с PHP 4.3.0, вышеописанное поведение генерации индекса изменено. Теперь, если вы будете использовать массив, в котором максимальным в настоящий момент является отрицательный ключ, то следующий созданный ключ будет нулевым (0). Раньше новым индексом становился самый большой существующий ключ + 1, так же как и у положительных индексов.

Обратите внимание, что максимальный числовой ключ, используемый для этого не обязательно должен существовать в массиве в настоящий момент. Он просто должен был существовать с момента последнего переиндексирования массива. Это иллюстрирует следующий пример:

<?php
// Создаем простой массив.
$array = array(1, 2, 3, 4, 5);
print_r($array);

// Теперь удаляем каждый элемент, но сам массив оставляем нетронутым:
foreach ($array as $i => $value) {
   unset(
$array[$i]);
}
print_r($array);

// Создаем элемент (обратите внимание, что новым ключом будет 5,
// а не 0, как вы возможно ожидали).
$array[] = 6;
print_r($array);

// Переиндексация:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>

Вышеприведенный пример выведет следующее:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
Array
(
)
Array
(
    [5] => 6
)
Array
(
    [0] => 6
    [1] => 7
)

Полезные функции

Для работы с массивами существует достаточное количество полезных функций. Смотрите раздел функции для работы с массивами.

Замечание: Функция unset() позволяет удалять ключи массива. Обратите внимание, что массив НЕ будет переиндексирован. Если вы использовали только "обычные числовые индексы" (увеличивающиеся на единицу, начиная с нуля), вы можете переиндексировать массив используя array_values().

<?php
$a
= array(1 => 'один', 2 => 'два', 3 => 'три');
unset(
$a[2]);
/* даст массив, представленный так:
   $a = array(1 => 'один', 3 => 'три');
   а НЕ так:
   $a = array(1 => 'один', 2 =>'три');
*/

$b = array_values($a);
// Теперь $b это array(0 => 'один', 1 =>'три')
?>

Управляющая конструкция foreach существует специально для массивов. Она предоставляет возможность легко пройтись по массиву.

Массив делает и не делает

Почему $foo[bar] это неверно?

Вы всегда должны заключать индекс ассоциативного массива в кавычки. К примеру, пишите $foo['bar'], а не $foo[bar]. Но почему $foo[bar] это неверно? Возможно, вы встречали в старых скриптах следующий синтаксис:

<?php
$foo
[bar] = 'враг';
echo
$foo[bar];
// и т. д.
?>

Это неверно, хотя и работает. Тогда почему же это неверно? Причина в том, что этот код содержит неопределенную константу (bar), а не строку ('bar' - обратите внимание на кавычки), и PHP в будущем может определить константу, которая к несчастью для вашего кода будет иметь то же самое имя. Это работает, потому что PHP автоматически преобразует голую строку (не заключенную в кавычки строку, которая не соответствует ни одному из известных символов) в строку, которая содержит голую строку. Например, если константа с именем bar не определена, то PHP заменит bar на строку 'bar' и использует ее.

Замечание: Это не означает, что нужно всегда заключать ключ в кавычки. Нет необходимости заключать в кавычки константы или переменные, поскольку это помешает PHP обрабатывать их.

<?php
error_reporting
(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Простой массив:
$array = array(1, 2);
$count = count($array);
for (
$i = 0; $i < $count; $i++) {
   echo
"\nПроверяем $i: \n";
   echo
"Плохо: " . $array['$i'] . "\n";
   echo
"Хорошо: " . $array[$i] . "\n";
   echo
"Плохо: {$array['$i']}\n";
   echo
"Хорошо: {$array[$i]}\n";
}
?>

Замечание: Вышеприведенный код выведет следующее:
Проверяем 0: 
Notice: Undefined index:  $i in /path/to/script.html on line 9
Плохо: 
Хорошо: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
Плохо: 
Хорошо: 1

Проверяем 1: 
Notice: Undefined index:  $i in /path/to/script.html on line 9
Плохо: 
Хорошо: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
Плохо: 
Хорошо: 2

Дополнительные примеры, демонстрирующие этот факт:

<?php
// Давайте покажем все ошибки
error_reporting(E_ALL);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Верно
print $arr['fruit'];  // apple
print $arr['veggie']; // carrot

// Неверно. Это работает, но из-за неопределенной константы с
// именем fruit также вызывает ошибку PHP уровня E_NOTICE
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit];    // apple

// Давайте определим константу, чтобы продемонстрировать, что
// происходит. Мы присвоим константе с именем fruit значение 'veggie'.
define('fruit', 'veggie');

// Теперь обратите внимание на разницу
print $arr['fruit'];  // apple
print $arr[fruit];    // carrot

// Внутри строки это нормально. Внутри строк константы не
// рассматриваются, так что ошибки E_NOTICE здесь не произойдет
print "Hello $arr[fruit]";      // Hello apple

// С одним исключением: фигурные скобки вокруг массивов внутри
// строк позволяют константам находится там
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}"// Hello apple

// Это не будет работать и вызовет ошибку обработки, такую как:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// Это, конечно, также приложимо и к использованию в строках автоглобальных переменных
print "Hello $arr['fruit']";
print
"Hello $_GET['foo']";

// Еще одна возможность - конкатенация
print "Hello " . $arr['fruit']; // Hello apple
?>

Когда вы переведете error_reporting() в режим отображения ошибок уровня E_NOTICE (такой как E_ALL), вы увидите эти ошибки. По умолчанию error_reporting установлена их не отображать.

Как указано в разделе синтаксис, внутри квадратных скобок ('[' и ']') должно быть выражение. Это означает, что вы можете писать подобно этому:

<?php
echo $arr[somefunc($bar)];
?>

Это пример использования возвращаемого функцией значения в качестве индекса массива. PHP известны также и константы, как вы, возможно, видели упоминание E_* раньше.

<?php
$error_descriptions
[E_ERROR]  = "Произошла фатальная ошибка";
$error_descriptions[E_WARNING] = "PHP сообщает предупреждение";
$error_descriptions[E_NOTICE]  = "Это лишь неофициальное замечание";
?>

Обратите внимание, что E_ERROR - это такой же верный идентификатор, как и bar в первом примере. Но последний пример по сути эквивалентен такой записи:

<?php
$error_descriptions
[1] = "Произошла фатальная ошибка";
$error_descriptions[2] = "PHP сообщает предупреждение";
$error_descriptions[8] = "Это лишь неофициальное замечание";
?>

поскольку E_ERROR соответствует 1 и т. д.

Как мы уже объяснили в вышеприведенных примерах, $foo[bar] по-прежнему работает, но это неверно. Это работает, поскольку в соответствии со своим синтаксисом bar ожидается как константа. Однако, в данном случае константы с именем bar не существует. В таком случае PHP предполагает, что, написав bar, вы имели ввиду строку "bar", но забыли указать кавычки.

Так что же в этом плохого?

Когда-нибудь в будущем команда разработчиков PHP возможно пожелает добавить еще одну константу или ключевое слово, либо вы можете ввести в ваше приложение еще одну константу и тогда у вас могут возникнуть проблемы. Например, вы уже не можете использовать таким образом слова empty и default, поскольку они являются зарезервированными ключевыми словами.

Замечание: Повторим, внутри строки (string), заключенной в двойные кавычки правильным является не окружать индексы массива кавычками, поэтому "$foo[bar]" является верным. Более подробно почему - смотрите вышеприведенные примеры, а также раздел обработка переменных в строках.

Преобразование в массив

Для любого из типов: integer, float, string, boolean и resource, если вы преобразуете значение в массив, вы получите массив с одним элементом (с индексом 0), являющимся скалярным значением, с которого вы начали.

Если вы преобразуете в массив объект (object), вы получите в качестве элементов массива свойства (переменные-члены) этого объекта. Ключами будут имена переменных-членов.

Если вы преобразуете в массив значение NULL, вы получите пустой массив.

Сравнение

Массивы можно сравнивать при помощи функции array_diff() и Операторов массивов.

Примеры

Тип массив в PHP является очень гибким, поэтому мы приведем здесь несколько примеров, чтобы продемонстрировать вам все возможности массивов.

<?php
// это
$a = array( 'color' => 'red',
          
'taste' => 'sweet',
          
'shape' => 'round',
          
'name'  => 'apple',
                      
4        // ключом будет 0
        
);

// полностью соответствует
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name']  = 'apple';
$a[]        = 4;        // ключом будет 0

$b[] = 'a';
$b[] = 'b';
$b[] = 'c';
// создаст массив array(0 => 'a' , 1 => 'b' , 2 => 'c'),
// или просто array('a', 'b', 'c')
?>

Пример 11-4. Использование array()

<?php
// Массив как карта (свойств)
$map = array( 'version'    => 4,
            
'OS'        => 'Linux',
            
'lang'      => 'english',
            
'short_tags' => true
          
);
          
// исключительно числовые ключи
$array = array( 7,
              
8,
              
0,
              
156,
               -
10
            
);
// это то же самое, что и array(0 => 7, 1 => 8, ...)

$switching = array(        10, // ключ = 0
                  
5    =>  6,
                  
3    =>  7,
                  
'a'  =>  4,
                          
11, // ключ = 6 (максимальным числовым индексом был 5)
                  
'8'  =>  2, // ключ = 8 (число!)
                  
'02' => 77, // ключ = '02'
                  
0    => 12  // значение 10 будет перезаписано на 12
                
);
                
// пустой массив
$empty = array();
?>

Пример 11-5. Коллекция

<?php
$colors
= array('красный', 'синий', 'зеленый', 'желтый');

foreach (
$colors as $color) {
   echo
"Вам нравится $color?\n";
}
?>

Результат работы приведенного скрипта будет следующий:

Вам нравится красный?
Вам нравится синий?
Вам нравится зеленый?
Вам нравится желтый?

Обратите внимание, что в настоящее время невозможно изменять значения массива в таком цикле напрямую. Однако можно сделать так:

Пример 11-6. Коллекция

<?php
foreach ($colors as $key => $color) {
  
// не будет работать:
   //$color = strtoupper($color);
  
   // работает:
  
$colors[$key] = strtoupper($color);
}
print_r($colors);

?>

Результат работы приведенного скрипта будет следующий:

Array
(
    [0] => КРАСНЫЙ
    [1] => СИНИЙ
    [2] => ЗЕЛЕНЫЙ
    [3] => ЖЕЛТЫЙ
)

Следующий пример создает начинающийся с единицы массив.

Пример 11-7. Индекс, начинающийся с единицы

<?php
$firstquarter 
= array(1 => 'Январь', 'Февраль', 'Март');
print_r($firstquarter);

?>

Результат работы приведенного скрипта будет следующий:

Array 
(
    [1] => 'Январь'
    [2] => 'Февраль'
    [3] => 'Март'
)

Пример 11-8. Заполнение массива

<?php
// заполняет массив всеми элементами директории
$handle = opendir('.');
while (
false !== ($file = readdir($handle))) {
  
$files[] = $file;
}
closedir($handle);
?>

Массивы упорядочены. Вы можете изменять порядок элементов, используя различные функции сортировки. Для дополнительной информации смотрите раздел функции для работы с массивами. Вы можете подсчитать количество элементов в массиве, используя функцию count().

Пример 11-9. Сортировка массива

<?php
sort
($files);
print_r($files);
?>

Поскольку значение массива может быть чем угодно, им также может быть другой массив. Таким образом вы можете создавать рекурсивные и многомерные массивы.

Пример 11-10. Рекурсивные и многомерные массивы

<?php
$fruits
= array ( "фрукты" => array ( "a" => "апельсин",
                                    
"b" => "банан",
                                    
"c" => "яблоко"
                                  
),
                
"числа"  => array ( 1,
                                    
2,
                                    
3,
                                    
4,
                                    
5,
                                    
6
                                  
),
                
"дырки"  => array (      "первая",
                                    
5 => "вторая",
                                          
"третья"
                                  
)
               );

// Несколько примеров доступа к значениям предыдущего массива
echo $fruits["дырки"][5];    // напечатает "вторая"
echo $fruits["фрукты"]["a"]; // напечатает "апельсин"
unset($fruits["дырки"][0]);  // удалит "первая"

// Создаст новый многомерный массив
$juices["яблоко"]["зеленое"] = "хорошее";
?>

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

<?php
$arr1
= array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 изменился,
             // $arr1 по прежнему array(2,3)
            
$arr3 = &$arr1;
$arr3[] = 4; // теперь $arr1 и $arr3 эквивалентны
?>



Массивы
ch dot martin at gmail dot com
08-Jun-2006 11:40
Extremely irritating quirk regarding the variable types of array keys:

<?php
$r
= array('05' => "abc", '35' => "def");
foreach (
$r as $key=>$value)
  
var_dump($key);
?>

The first var_dump for '05' is:
   string(2) "05"
as expected.  But the second, '35', turns out as:
   int(35)

Php apparently decided to make the 35 became an int, but not the 05 (presumably because it leads with a zero).  As far as I can see, there is absolutely no way of making string(2) "35" an array key.
anghuda(at)gmail(dot)com
25-May-2006 06:52
this is simpler tha function display_angka_bilangan by ktaufik(at)gmail(dot)com (16-Feb-2005 12:40)

<?

/*
*
* Class : Terbilang
* Spell quantity numbers in Indonesian or Malay Language
*
*
* author: huda m elmatsani
* 21 September 2004
* freeware
*
* example:
* $bilangan = new Terbilang;
* echo $bilangan -> eja(137);
* result: seratus tiga puluh tujuh
*
*
*/

Class Terbilang {

   function
terbilang() {
      
$this->dasar = array(1=>'satu','dua','tiga','empat','lima','enam',
      
'tujuh','delapan','sembilan');

      
$this->angka = array(1000000000,1000000,1000,100,10,1);
      
$this->satuan = array('milyar','juta','ribu','ratus','puluh','');
   }

   function
eja($n) {

  
$i=0;
   while(
$n!=0){

      
$count = (int)($n/$this->angka[$i]);

       if(
$count>=10) $str .= $this->eja($count). " ".$this->satuan[$i]." ";
       else if(
$count > 0 && $count < 10)
          
$str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";

          
$n -= $this->angka[$i] * $count;
          
$i++;
       }
      
$str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str);
      
$str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str);

       return
$str;
   }
}

?>
benjcarson at digitaljunkies dot ca
09-May-2006 01:46
phoenixbytes: The regex you have posted for matching email addresses is incorrect.  Among other things, it does not allow '+' before the '@' (which is perfectly valid and can be quite useful to separate extensions of a single address).  RFC 822 [1] defines the grammar for valid email addresses, and (the extemely long) regex implementing can be found at [2].  Even the "Add Note" page here at php.net says:

[quote]
And if you're posting an example of validating email addresses, please don't bother. Your example is almost certainly wrong for some small subset of cases. See this information from O'Reilly Mastering Regular Expressions book for the gory details.
[/quote]

A note to others: please do your homework before writing another email-matching regex.

[1] http://www.ietf.org/rfc/rfc0822.txt?number=822
[2] http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html.
phoenixbytes at o2 dot co dot uk
16-Apr-2006 05:10
i use the array() function for deciding upon an email address's validity, i have a 'wap stalker' of my site that loves to exploit every hole i leave, so i used the following script to avoid being email bombed with my own file sender script, the array() is used to filter out undesirable email providers and, of course, any and all of my own addresses. before all that i used a REGEX to make sure it's an actual email address before going any further.

$mailto = "mail.domain.org"; // the input to be tested

if (preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $mailto)) // see if it's really an email address
{
   $bad = array('mytrashmail.com', 'mymail.ro', 'acasa.ro', 'gala.net', 'phoenixbytes'); // pick out the victims
   foreach($bad as $baddies); // make a collection

   if (preg_match("/$baddies/i", $mailto)) // find a match
   {
   $addrmail = "false";
   }
   else
   {
   $addrmail = "true";
   }

}
else
{
$addrmail = "false";
}

$addrmail can then be used in an argument.
$baddies can be used to give a list, if necessary.

i hope this helps someone.
crozzer
01-Apr-2006 12:54
Passing variables into the array constructor:
Just a NOOB pointer, I couldn't find other examples for this.  If you want to pass the value of an existing variable into the array() constructor, you can quote it or not, both methods are valid.

<?
$foo_value
= 'foo string';
$bar_value = 'bar string';

$myArray = array(
  
'foo_key' => $foo_value,    // not quoted
  
'bar_key' => "$bar_value");  // quoted

foreach ($myArray as $k => $v) {
   echo
"\$myArray[$k] => $v.<br />\n";
}
?>

Both of these will work as expected, but the unqoted $foo_value method above is marginally faster because adding quotes adds an additional string de-reference.
Henry Cesbron Lavau
15-Mar-2006 05:00
even better than $a2 = array_combine($a1, $a1); I finally suggest

   $a2 = unserialize(serialize($a1));

which produces exactly what we want : a new array $a2 of new objects not referenced to the objects of $a1 (as they would with $a2 = $a1).
Henry Cesbron Lavau
14-Mar-2006 02:07
To copy values from an array which contains arrays

db 05-Jan-2005 04:06 said : If you copy (=) an array which contains arrays it will be REFERENCED not COPIED.

   Well, this is only true since PHP 5.
   Under PHP4, it is COPIED.

So be aware of this when porting code from PHP 4 to PHP 5 !

Now, if you want to copy the values (and not the references),
   (instead of using array_diff as stated below, which won't work either under PHP 5)
just use  :

$a2 = array_combine($a1, $a1);
Simon Rigt at paragi.dk
20-Jan-2006 05:57
Make a real copy of an array.
One simple way is to use array_diff function.

<?php
// Define a multidimentional array
$arr1['color']=array('red','green','blue');
$arr1['number']=array(1,2,3);
  
// Copy all by getting the differance between this and an empty array.
$arr2=array_diff($arr1,array());
  
// To show that its not a reference, delete the original and display the content of the new array.
unset($arr1);
var_dump($arr2);
?>
   
Output:
array(2) {
  ["color"]=> array(3) {
   [0]=> string(3) "red"
   [1]=> string(5) "green"
   [2]=> string(4) "blue"
  }
  ["number"]=> array(3) {
   [0]=> int(1)
   [1]=> int(2)
   [2]=> int(3)
  }
}
sales at maboom dot ch
13-Dec-2005 09:41
if you need to check a multidimensonal array for values it's handy to store it like

$ar['key1'][0]
$ar['key2'][0]
$ar['key3'][0]

$ar['key1'][1]
$ar['key2'][1]
$ar['key3'][1]

and to loop the keys.

Fill the array (from a database-request):

while($rf=mysql_fetch_row($rs))

{
   $nr=$rf[0];
   $channel['nr'][$nr]=$rf[1];
   $channel['chatter'][$nr]=$rf[2];
}

Call the values:

foreach(array_keys($channel['nr']) as $test)
{
print ' nr:'.$test.'<br>';
print 'value nr: '.$channel['nr'][$test].'<br>';
print ' chatter: '.$channel['chatter'][$test].'<br>';
}

This is useful, if you have to look later for an element
inside the array:

if(in_array($new_value,$channel['nr'])) print 'do something.';

Hope this helps someone.
ia [AT] zoznam [DOT] sk
30-Sep-2005 02:55
Regarding the previous comment, beware of the fact that reference to the last value of the array remains stored in $value after the foreach:

<?php
foreach ( $arr as $key => &$value )
{
  
$value = 1;
}

// without next line you can get bad results...
//unset( $value );

$value = 159;
?>

Now the last element of $arr has the value of '159'. If we remove the comment in the unset() line, everything works as expected ($arr has all values of '1').

Bad results can also appear in nested foreach loops (the same reason as above).

So either unset $value after each foreach or better use the longer form:

<?php
foreach ( $arr as $key => $value )
{
  
$arr[ $key ] = 1;
}
?>
stochnagara at hotmail dot com
27-Sep-2005 01:53
Regarding the previous comment, thw following code does the job:

<?php
foreach($arr as $key => &$value) {
  
$value = 1;
}
?>
jazepstein OverAt GeeMail dot com
19-Sep-2005 06:14
Regarding the previous comment, the fact that this code has no effect is perfectly expected:

<?php
foreach($arr as $value) {
  
$value = 1;
}
?>

The reason that this doesn't work, is because each time that PHP goes through the loop, it _copies_ the value of the array element into $value. So if you assign a new value to the data in $value, it has no effect on the actual array, because you only changed the value of the copy that was put in $value.

As was discovered in the previous post, the only way to get around this problem is to change the value in the original array. Hence, a typical foreach should instead look like this:

<?php
foreach($arr as $key => $value) {
  
$arr[$key] = 1;
}
?>
caifara aaaat im dooaat be
28-Aug-2005 02:28
Don't know if this is known or not, but it did eat some of my time and maybe it won't eat your time now...

I tried to add something to a multidimensional array, but that didn't work at first, look at the code below to see what I mean:

<?php

$a1
= array( "a" => 0, "b" => 1 );
$a2 = array( "aa" => 00, "bb" => 11 );

$together = array( $a1, $a2 );

foreach(
$together as $single ) {
  
$single[ "c" ] = 3 ;
}

print_r( $together );
/* nothing changed result is:
Array
(
   [0] => Array
       (
           [a] => 0
           [b] => 1
       )
   [1] => Array
       (
           [aa] => 0
           [bb] => 11
       )
) */

foreach( $together as $key => $value ) {
  
$together[$key]["c"] = 3 ;
}

print_r( $together );

/* now it works, this prints
Array
(
   [0] => Array
       (
           [a] => 0
           [b] => 1
           [c] => 3
       )
   [1] => Array
       (
           [aa] => 0
           [bb] => 11
           [c] => 3
       )
)
*/

?>
uzakufuklar at hotmail dot com
03-Aug-2005 11:24
It is a kind of simple muti-dimensional array list.
 I have made it just to give a simple idea.
<?php
echo "Here we'll see how to create a multi-dimensional array.\n";
$a=array('fruits'=>array('a'=>'orange',
                    
'b'=>'grape',c=>'apple'),
          
'numbers'=>array(1,2,3,4,5,6),
          
'holes'=>array('first',5=>'second',
                                                        
'third')
           );
foreach(
$a as $list=>$things){
   foreach(
$things as $newlist=>$counter){
   echo
$counter;
   }
}
?>
t0russ a t g m a i l d o t c o m
22-Jun-2005 10:27
re: jeff splat codedread splot com
when posting spaces are also translated into underscores,
quotes are translated into \" and \'
so <input name="Bill Gay'ts" value="micro$oft blows"> will be named as $_POST['Bill_Gay\'ts']
z
22-Apr-2005 12:10
Here's a simple function to insert a value into some position in an array

<?php
function array_insert($array,$pos,$val)
{
  
$array2 = array_splice($array,$pos);
  
$array[] = $val;
  
$array = array_merge($array,$array2);
  
   return
$array;
}
?>

and now for example...
<?php
$a
= array("John","Paul","Peter");
$a = array_insert($a,1,"Mike");
?>

Now $a will be "John","Mike","Paul","Peter"
jeff splat codedread splot com
21-Apr-2005 09:16
Beware that if you're using strings as indices in the $_POST array, that periods are transformed into underscores:

<html>
<body>
<?php
   printf
("POST: "); print_r($_POST); printf("<br/>");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="hidden" name="Windows3.1" value="Sux">
   <input type="submit" value="Click" />
</form>
</body>
</html>

Once you click on the button, the page displays the following:

POST: Array ( [Windows3_1] => Sux )
roland dot swingler at transversal dot com
05-Apr-2005 08:24
Something that tripped me up:

If you mix string and integer keys, be careful if you are doing a comparison on the to find if a string key exists.

For example, this will not do what you expect it to do:

<?php
$exampleArray
= array();
$exampleArray['foo'] = 'bar';
$exampleArray[] = 'Will create 0 index';

$keyWeAreLookingFor = "correctKey";

foreach (
$exampleArray as $key => $value){
  if (
$key == $keyWeAreLookingFor){
   print
"Found Key";
  }
}
?>

It will print "Found Key", because (I presume) when PHP makes the comparison between the string "correctKey" and the index 0, it casts the string to an integer, rather than casting 0 to the string "0" and then doing the comparison.

Using === fixes the problem:

<?php
foreach ($exampleArray as $key => $value){
  if (
$key === $keyWeAreLookingFor){
   print
"Found Key";
  }
}
?>
lars-phpcomments at ukmix dot net
28-Mar-2005 08:40
Used to creating arrays like this in Perl?

@array = ("All", "A".."Z");

Looks like we need the range() function in PHP:

<?php
$array
= array_merge(array('All'), range('A', 'Z'));
?>

You don't need to array_merge if it's just one range:

<?php
$array
= range('A', 'Z');
?>
mortoray at ecircle-ag dot com
16-Feb-2005 12:59
On array copying a deep copy is done of elements except those elements which are references, in which case the reference is maintained.  This is a very important thing to understand if you intend on mixing references and recursive arrays.

By Example:
   $a = array( 1 );
   $aref_a = array( &$a );
   $copy_aref_a = $aref_a;
   $acopy_a = array( $a );
   $copy_acopy_a = $acopy_a;

   $a[] = 5;
   $acopy_a[0][] = 6;

   print_r( $aref_a ); //Shows: ( (1,5) )
   print_r( $copy_aref_a ); //Shows: ( (1,5) )
   print_r( $acopy_a ); //Shows: ( (1, 6) )
   print_r( $copy_acopy_a ); //Shows: ( (1) )
ktaufik(at)gmail(dot)com
16-Feb-2005 12:40
For you who works for localized "say" number to letter ( ex , 7=> seven, 8=>eight) for Bahasa Indonesia.

Indonesia "say" or "Terbilang" is based on 3 digit number.
thousands, millions and trillions .... will be based on the 3 digit number.

In Indonesia you say 137 as "Seratus Tiga Puluh Tujuh"

<?php
//build random 3 digit number to be "said" in Bahasa Indonesia
$x=rand(0,9);
$y=rand(0,9);
$z=rand(0,9);

function
display_angka_bilangan($n) {
  
$angka = array(
    
1 => 'satu',
    
2 => 'dua',
    
3 => 'tiga',
    
4 => 'empat',
    
5 => "lima",
    
6 => 'enam',
    
7 => 'tujuh',
    
8 => 'delapan',
    
9 => 'sembilan'
  
);
   return
$angka[$n];
}
// Terbilang X-------Say X
if ($x==1){$terbilangx="seratus ";}
elseif (
$x==0){$terbilangx='';}
else {
$terbilangx=''.display_angka_bilangan($x).' '.'ratus ';}
// Terbilang Y ------Say Y
if ($y==0){$terbilangy='';}
elseif (
$y==1 && $z==1){$terbilangy="sebelas";$terbilangz='';}
elseif (
$y==1 && $z==0){$terbilangy="sepuluh ";$terbilangz='';}
elseif (
$y==1 && $z!==1 &&  $z!==0){$terbilangy=''.display_angka_bilangan($z).' belas ';}
else {
$terbilangy=''.display_angka_bilangan($y).' '.'puluh ';}
// Terbilang z ------Say z
if ($z==0){$terbilangz="";}
elseif (
$z==0 && $y==1){$terbilangz="";}
elseif (
$z==1 && $y==1){$terbilangz="";}
elseif(
$y==0) {$terbilangz="".display_angka_bilangan($z);}
elseif (
$y==1 && $z!==1 &&  $z!==0) {$terbilangz="";}
else {
$terbilangz="".display_angka_bilangan($z);};

$terbilang=$terbilangx.$terbilangy.$terbilangz;
echo
$x.$y.$z." ";
echo
$terbilang;
?>

Hope it is useful
ktaufik(at)gmail(dot)com
db
05-Jan-2005 07:06
Attention with Arrays in Arrays!

If you copy (=) an array which contains arrays it will be REFERENCED not COPIED.

Example:

<?php
/* GOOD ONE */
echo "<b>Here copy (=) works correct:</b><br>";
/* Initialise Array 1 */
$x1 = array(array(10,20),array(30,40));
/* COPY Array */
$x2 = $x1;
/* Change some values in Array 2 */
$x2[0][0]=77;
$x2[1][1]=99;
echo
"<b>Original:</b><pre>";
var_dump($x1);
echo
"</pre><b>Changed Copy:</b><pre>";
var_dump($x2);

/* BAAAAAAAD ONE */
echo "</pre><hr><b>Here copy (=) FAILS:</b><br>";
/* Initialise Array 1 */
$a1[0]->bla[0]->id=10;
$a1[0]->bla[1]->id=20;
$a1[1]->bla[0]->id=30;
$a1[1]->bla[1]->id=40;
/* COPY Array */
$a2 = $a1;
/* Change some values in Array 2 (!) */
$a2[0]->bla[0]->id=77;
$a2[1]->bla[1]->id=99;
echo
"<b>Original:</b><pre>";
var_dump($a1);
echo
"</pre><b>Changed Copy:</b><pre>";
var_dump($a2);
echo
"</pre>";

php?>

The output of $a1 and $a2 will be the same..
Guillaume Beaulieu
30-Dec-2004 10:53
There is no warning nor error if you make something like:

foreach($a as $b => $b) {
   print $b;
}

It is somewhat weird, but in the philosophy of "permit everything" of php.
Joe Morrison <jdm at powerframe dot com>
08-Nov-2004 01:26
Programmers new to PHP may find the following surprising:

<?php

$x
[1] = 'foo';
$x[0] = 'bar';
echo
"Original array:\n";
var_dump($x);

array_pop($x);
echo
"Array after popping last element:\n";
var_dump($x);

?>

The surprise is that element 0 is deleted, not element 1. Apparently the notion of "last element" has more to do with how the array is stored internally than with which element has the highest numeric index. I recently translated a Perl program to PHP and was bitten by this one.

My solution was to identify all the places in my code where I could prove that the array elements were assigned sequentially. In those cases it is safe to use array_pop, array_splice, etc. since the array indices correspond with the array layout. For the other cases, my solution was to write replacements for the built-in array functions such as this one:

<?php

function safe_pop(&$a)
{
  if (!isset(
$a))
   return;

  if (!
is_array($a))
   return;

  if (
count($a) == 0)
   return;

  unset(
$a[max(array_keys($a))]);
}

?>
Cameron Brown
18-Nov-2003 08:51
Negative and positive array indices have different behavior when it comes to string<->int conversion.  1 and "1" are treated as identical indices, -1 and "-1" are not.  So:

$arr["1"] and $arr[1] refer to the same element.
$arr["-1"] and $arr[-1] refer to different elements.

The following code:

<?
  $arr
[1]    = "blue";
 
$arr["1"]  = "red";
 
$arr[-1]  = "blue";
 
$arr["-1"] = "red";

 
var_dump($arr);
?>

produces the output:

array(3) {
  [1]=>
  string(3) "red"
  [-1]=>
  string(4) "blue"
  ["-1"]=>
  string(3) "red"
}

This code should create an array with either two or four elements.  Which one should be the "correct" behavior is an exercise left to the reader....
akamai at veloxmail dot com dot br
16-Jul-2003 04:22
It is quite simple, but don't forget when you'll using foreach with forms arrays.

If your field name is:
<input type="checkbox" name="foo['bar'][]" ...
It doesn't work.

This should work:
<input type="checkbox" name="foo[bar][]" ...
agape_logos at shaw dot ca
11-Jul-2003 04:59
I was having trouble getting javascript arrays and php arrays to work together with a Check All checkboxe.  Here is a simple solution.  Clicking the 'Check All' checkbox will check all checkboxes on the form.

<script language="JavaScript">
function chkAll(frm, arr, mark) {
  for (i = 0; i <= frm.elements.length; i++) {
   try{
     if(frm.elements[i].name == arr) {
       frm.elements[i].checked = mark;
     }
   } catch(er) {}
  }
}
</script>

<form name='foo'>
<input type="checkbox" name="ca" value="1" onClick="chkAll(this.form, 'formVar[chkValue][]', this.checked)">
<?php
for($i = 0; $i < 5; $i++){
echo(
"<input type='checkbox' name='formVar[chkValue][]' value='$i'>");
}
?>
</form>

Dean M.
chroniton .at. gmx .dot. li
26-Mar-2003 10:13
I didn't find this anywhere in the docs and i think it is worth a mention:

$a[] = &$a;
print_r($a);

// will output:

/*
Array
(
   [0] => Array
 *RECURSION*
)

*/

// this means that $a[0] is a reference to $a ( that is detected by print_r() ). I guess this is what the manual calls 'recursive arrays'.
07-Mar-2003 03:28
"Using NULL as a key will evaluate to an empty string. Using an emptry string as key will create (or overwrite) a key with an empty string and its value, it is not the same as using empty brackets."

If you create an array like this:
$foo = array(null => 'bar');
And then want to access 'bar', you must use this syntax:
echo $foo['']; // notice the two single quotes

This will of course cause a fatal error:
echo $foo[];
wmoranATpotentialtechDOTcom
29-Nov-2002 03:10
Dereferencing arrays takes some time, but is not terribly expensive.
I wrote two dummy loops to test performance:
for ($i =0; $i < count($a); $i++) {
 $x = $a[$b[$i]];
 $y = $a[$b[$i]];
 $z = $a[$b[$i]];
}
for ($i =0; $i < count($a); $i++) {
 $q = $b[$i];
 $x = $a[$q];
 $y = $a[$q];
 $z = $a[$q];
}

The first loop is 6.5% slower than the second.  Meaning that dereferencing arrays is not terribly expensive, unless you do it a whole lot. I would expect that each extra reference costs about 3% in speed. The lesson is that if you're going to be using a specific value in an array for a number of operations, you can gain a little speed by assigning it to a temp variable (or creating a reference with $q = &$b[$i]) but it's not worth getting crazy over.
I tested this with iterations of 10,000 and 100,000 on php 4.2 and the results were consistent.
mu at despammed dot com
14-Oct-2002 11:50
Recursive arrays and multi-dimensional arrays are the same thing and completely identical.

The following confirms this:

$fruits1["european"]["green"] = "Apple";
$fruits2 = array ( "european"  => array ( "green" => "Apple"));
print ($fruits1 === $fruits2);

Result: 1 (= true)
hramrach_L at centrum. cz ;-)
11-Jun-2002 05:40
Arrays can be merged using + as discussed in the notes for array_merge.
 manual/en/function.array-merge.php
philip at boone dot at
24-May-2002 06:06
For all of you having problems when using php arrays in an HTML form input field name, and wanting to validate the form using javascript for example, it is much easier to specify an id for the field as well, and use this id for validation.

Example:

<input type="text" id="lastname" name="fields[lastname]">

then in the javascript check:

if(formname.lastname.value == "") {
     alert("please enter a lastname!");
}

This works very well. If you have any problems with it, let me know.
mjp at pilcrow dot madison dot wi dot us
21-Feb-2000 02:18
Those with a perl background may be surprised to learn that the 'thick arrow' and comma operators are not synonymous in array construction.

For example, the following are equivalent:

$ary = array("foo" => "bar", 1, 2);
$ary = array("foo" => "bar", 0 => 1, 1 => 2);

<СтрокиОбъекты>
 Last updated: Tue, 15 Nov 2005