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.

39,870 questions

51,793 answers

573 users

How to print associative (key value) 2D array (matrix) of in PHP

2 Answers

0 votes
$arr = array(
    0 => array(
        'name' => 'Obi-Wan Kenobi',
        'age' => '104',
    ),
    1 => array(
        'name' => 'R2-D2',
        'age' => 89
    ),
    2 => array(
        'name' => 'C-3PO',
        'age' => 80
    ),
    3 => array(
        'name' => 'Chewbacca',
        'age' => 124
    ),
);
   
foreach ($arr as $arr=>$row) {
  foreach ($row as $key=>$value) {
    echo $key . " - " . $value . "\n"; 
  }
}




/*
run:

name - Obi-Wan Kenobi
age - 104
name - R2-D2
age - 89
name - C-3PO
age - 80
name - Chewbacca
age - 124

*/

 



answered Feb 21, 2021 by avibootz
0 votes
$arr = array(
    0 => array(
        'name' => 'Obi-Wan Kenobi',
        'age' => '104',
    ),
    1 => array(
        'name' => 'R2-D2',
        'age' => 89
    ),
    2 => array(
        'name' => 'C-3PO',
        'age' => 80
    ),
    3 => array(
        'name' => 'Chewbacca',
        'age' => 124
    ),
);

    
$rows = count($arr);  

for ($i = 0; $i < $rows; $i++) {
     echo "row " . $i . ": " . $arr[$i]['name'] . " - " . $arr[$i]['age'] . "\n";
}
   




/*
run:

row 0: Obi-Wan Kenobi - 104
row 1: R2-D2 - 89
row 2: C-3PO - 80
row 3: Chewbacca - 124

*/

 



answered Feb 21, 2021 by avibootz
...