Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to create and use an array in PHP

9 Answers

0 votes
$numbers = array(1, 2, 3, 4, 5);

print_r($numbers);

/*
run:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 

*/

 



answered Jul 2, 2015 by avibootz
0 votes
$arr = array("a" => "PHP", "b" => "C++", "c" => "Java");

print_r($arr);

/*
run:

Array ( [a] => PHP [b] => C++ [c] => Java ) 

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = array("PHP", 3 => "Java", "C#");

print_r($arr);

/*
run:

Array ( [0] => PHP [3] => Java [4] => C# ) 

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = array(3, 3, 3, 3, 9 => 900, 3 => 100, 27, 3 => 13);

print_r($arr);

/*
run:

Array ( [0] => 3 [1] => 3 [2] => 3 [3] => 13 [9] => 900 [10] => 27 ) 

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = array(1 => 'Sunday', 'Monday', 'Tuesday');

print_r($arr);

/*
run:

Array ( [1] => Sunday [2] => Monday [3] => Tuesday ) 

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = array('name' => 'iq');
echo $arr['name'];

/*
run:

iq

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = [1, 2, 3, 4, 5];
    
print_r($arr);

/*
run:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$numbers = [1, 2, 3, 4, 5];
    
foreach ($numbers as $n)     
        echo $n . "<br />";
/*
run:

1
2
3
4
5

*/

 



answered Jul 3, 2015 by avibootz
0 votes
$arr = array("a" => "PHP", "b" => "C++", "c" => "Java");
    
foreach($arr as $key=>$value)       
        echo $key . " " . $value . "<br />";
/*
run:

a PHP
b C++
c Java

*/

 



answered Jul 3, 2015 by avibootz
edited Jul 23, 2015 by avibootz
...