0

I have this PHP array:

Array
(
    [Album Title 1] => Array
        (
            [Track Title 1] => 27
            [Track Title 2] => 18
            [Track Title 3] => 7
        )

    [Album Title 2] => Array
        (
            [Track Title 1] => 41
            [Track Title 2] => 17
            [Track Title 3] => 12
        )

)

How can I echo "Album Title 1", "Track Title 1", and it's corresponding int(27), without using dot notation? (I don't know the exact title of "Album Title 1")

To get "Album Title 1", I've tried:

echo $albums[0];

print_r(array_values($albums));

foreach ($albums as $a){
    print_r($a) . "\n";
}

etc...

I've tried more variations of these for every value I need.

Damien Stewart
  • 143
  • 1
  • 1
  • 10

2 Answers2

1

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

I think that this can be helpfull for you, Pay attention to Example n° 6 https://www.php.net/manual/en/language.types.array.php

What you need is to access to a multidimensional array. In PHP you don't need the "dot notation" (album.title or elem1.sub_elem2) as you called ,to access to elements. You can access to multidimensional array by the key of element, in this way:

$multiarray["element1"]["sub_element2"]["sub_sub_elementX"];

Having said that, I will leave an example here that explains better what I wanted to say.

Example Using Your Data:

<?php
$array = array( "Album Title 1" => array( "Track Title 1" => 27,
                                          "Track Title 2" => 18,
                                          "Track Title 3" => 7 ),
                "Album Title 2" => array( "Track Title 1" => 41,
                                          "Track Title 2"  => 17,
                                          "Track Title 3"  => 12 )
              );

echo($array["Album Title 1"]["Track Title 2"]);
//Output will be 18

echo("<br/><br/>");

foreach($array as $Album => $Track){
    foreach($Track as $Title => $number){
        echo("Album : ".$Album."<br/>");
        echo("Track : ".$Title."<br/>");
        echo("Number : ".$number."<br/>");
    }
}
/*  Output will be:

Album : Album Title 1
Track : Track Title 1
Number : 27
Album : Album Title 1
Track : Track Title 2
Number : 18
Album : Album Title 1
Track : Track Title 3
Number : 7
Album : Album Title 2
Track : Track Title 1
Number : 41
Album : Album Title 2
Track : Track Title 2
Number : 17
Album : Album Title 2
Track : Track Title 3
Number : 12

*/
?>

You can try the code on http://phptester.net/

Yeti82
  • 383
  • 1
  • 6
  • 14
-1

It's not really good part of code, but should works:

$array = [
    'Album Title 1' => [
        'Track Title 1' => 27,
        'Track Title 2' => 18,
        'Track Title 3' => 7
    ]
];

$level1KeyName = array_keys($array)[0]; // Album Title 1
$level2KeyName = array_keys($array[$level1KeyName])[0]; // Track Title 1
$level2KeyValue = $array[$level1KeyName][$level2KeyName]; // 27

echo $level1KeyName . PHP_EOL;
echo $level2KeyName . PHP_EOL;
echo $level2KeyValue . PHP_EOL;

It prints:

Album Title 1

Track Title 1

27

Community
  • 1
  • 1
VirCom
  • 3,414
  • 1
  • 10
  • 10