-2

i got this Code:

$Wanteds[] = array(
                'WantedLevel' => 1,
                'WantedName' => "Nichtbefolgen",
                'WantedPreis' => 300
            );
$Wanteds[] = array(
                'WantedLevel' => 1,
                'WantedName' => "Beihilfe",
                'WantedPreis' => 200
            );
$Wanteds[] = array(
                'WantedLevel' => 2,
                'WantedName' => "Dealen",
                'WantedPreis' => 500
            );

Now, i tryed to get it into a variable:

echo "TESTING: $Wanteds[0][WantedName] <br />";

All i got it: TESTING: [WantedName] instead of: TESTING: Nichtbefolgen

Please keep in mind: I'm new to arrays and programming. :)

LuckyLuke
  • 13
  • 2
  • 7

5 Answers5

2

Depends on what you want.

Echo 1 value

echo "TESTING: " . $Wanteds[0]['WantedName'] . "<br />"; // possibility 1
echo "TESTING: {$Wanteds[0]['WantedName']} <br />"; // possibility 2

Print entire array

print_r($Wanteds); // Usefull for debugging and value checking
Thaillie
  • 1,362
  • 3
  • 17
  • 31
0

you have to warb the variable in strings with curly brackets

echo "TESTING: {$Wanteds[0]['WantedName']} <br />";
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
0

No need to print variables in double quotes.

You can print it and concatenate it to string like this:

echo "TESTING: " .$Wanteds[0]['WantedName'] ."<br />";

Putting variables cause extra load on server as he needs to parse everything inside the string.

Above phenomena is called variable interpolation.

You can use strings without double quotes and that is faster than embedding them in double quotes.

See it live here

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Try this

echo "TESTING: ".$Wanteds[0]['WantedName']." <br />";   
Suyog
  • 2,472
  • 1
  • 14
  • 27
0

To return the name that you want you can do the following :

echo "TESTING: " .$Wanteds[0]['WantedName'] ."<br />";

You could also display the whole array with :

print_r($Wanteds);

Or even display each sub-array separatly by doing :

foreach($Wanteds as $wanted){
 print_r($wanted);
}
Nirnae
  • 1,315
  • 11
  • 23