简体   繁体   中英

Is it ok to use variables at the second level of a multi-dimensional array? Additionally, how do I echo it?

To describe my question a little more in detail, can I use variables (ie $player1 ) at the second level of my multidimentional array?

If so, how do I echo $player1["name"] and $player1["age"] ?

Is it a better practice to use keys in multidimensional arrays (ie "player1" => array();) or variables (ie $player1 = array();) ?

I am very new to php so your answers are very appreciated. Here is my code:

<?php

// list of players

$currentPlayers = array(

    $player1 = array(

        "name" => "Ryan",
        "age" => 26,
        "weight" => 200,

    ),

);



echo $currentPlayers[$player1]["name"];
echo "<br>";
echo $currentPlayers[$player1]["weight"];

What you want is:

$currentPlayers = array(

    "player1" => array(

        "name" => "Ryan",
        "age" => 26,
        "weight" => 200,

    ),

);

$thisPlayer="player1";
echo $currentPlayers[$thisplayer]["name"];

First you are missing => after $player1 in your original array. Second you are not setting value for your variable $player1 ,

<?php
$player1="value1";//declaring the value for $player1 variable
$currentPlayers = array(

    $player1 => array(//replacing = to =>

        "name" => "Ryan",
        "age" => 26,
        "weight" => 200,

    ),

);

echo $currentPlayers[$player1]["name"];
echo "<br>";
echo $currentPlayers[$player1]["weight"];

Output will be

Ryan

200

Explanation

Lets say you have to player one is ryan second is arif. Both ages and wieght are different. and lets say you have form to get the info.

For example if you enter arif. it will onlcy show information about arif. and if you enter ryan it will oncly show info for ryan.

<form action="" method="POST">
    <input type="text" name="player">
    <input type="submit" name="submit">
</form>
<?php

$currentPlayers = array(

    "ryan" => array(//replacing = to =>

        "name" => "Ryan",
        "age" => 26,
        "weight" => 200,

    ),
    "arif" => array(//replacing = to =>

        "name" => "arif",
        "age" => 16,
        "weight" => 100,
        ),

);

$player1=$_POST['player'];//Declaring the value for player1


echo $currentPlayers[$player1]["name"];
echo "<br>";
echo $currentPlayers[$player1]["weight"];

PHP is very flexible.

A Variable can represent any type of data. A variable is some text that starts with a $. $myvariable

Strings are represented by some text within quotes. "this-is-a-string" . Numbers have no quotes. 98 .

Associative arrays use "keys" to organize data in the array. The keys can be strings (or numbers).

$value = "some value";
$key = 'key2';
$myarray = array("key1"=>$value, $key2=>"Some other value");

You can use variables to define either a key or a value when creating an array.

A multidimentional array is simply an array that contains another array.

$key = array("second_level_key"=>"Second level value");
$myarray = array("key1"=>$key);

Or

$array = array(0=>"somevalue", 1=>array("key"=>"value"));

Multidimensional arrays can go as deep as you care to make them, and you can use variables at any depth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM