Accessing a Multidimensional Array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
NewBieMe
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Feb 21, 2021 3:35 am

Sun Feb 21, 2021 4:39 am

Hi,
I'm new to php and learning how to read a multidim. array. I'm facing some issues here whenever I got an "id" (1,2,3 etc)

Code: Select all

$Tab= array(

    1=> array('sex' => 'm', 
		     'title' => 'it'
		     ),
						
    2=> array('sex' => 'f', 
		     'title' => 'med'
                      ),
						
    3=> array('sex' => 'f', 
		     'title' => 'med'
		     ), 
						
    4=> array('sex' => 'f', 
		     'title' => 'it'
		     ),
						
    5=> array('sex' => 'm', 
		     'title' => 'it'
		     ),
);
What I've tried so far:

Code: Select all

// find size of the array
$size = count($Tab);

// array keys
$keys = arra_keys($elts);

// using the for loop
for($i = 0; $i < $size; $i++)
{
    echo $keys[$i]. "\n";
    foreach($elts[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "\n";
    }
    echo "\n";
}
and I got this error

Code: Select all

Fatal error: Uncaught Error: Call to undefined function arra_keys() in C:\xampp\htdocs\xml.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\xml.php on line 5
Thks for help
abdlimran
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Nov 03, 2020 2:54 am

Mon Feb 22, 2021 11:09 pm

Hi, You can try this code :

Code: Select all

foreach($Tab as $tab){
	foreach($tab as $key => $value){
        echo 'key :'.$key.' Value: '.$value."\n";
    }
}
NewBieMe
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Feb 21, 2021 3:35 am

Thu Feb 25, 2021 4:02 am

Thank you
but If I want to echo the id? I've tried this but doesnt work.

foreach($Tab as $id){
foreach($id as $key => $value){
echo $id .':'. $key .':'. $value."<br>"."\n";
}
}
outuput should be:
id:1
sex: m
title: it
id:2
sex:f
title:med
etc

Thanks
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1190
Joined: Mon Feb 22, 2016 5:52 pm

Thu Feb 25, 2021 7:20 am

Fatal error: Uncaught Error: Call to undefined function arra_keys() in C:\xampp\htdocs\xml.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\xml.php on line 5
You need to fix the typo on line 5:

Code: Select all

array_keys()
Post Reply