PHP echo array repetition displays only the first letter of each value

This is a simple rating system. There are 3 values ​​that contain a percentage with an accompanying description / name. I just want to show a description / name.

For some reason, it displays:

AND

T

I

This is my script:

if(isset($_POST['analyze'])) {

    // get total value of "Time Management" section
    for($i = 0; $i < 19; $i++) {
        $time_management_total += $_POST["time_management_Q$i"];
    }


    // get total value of "Attract Clients" section
    for($i = 0; $i < 16; $i++) {
        $attract_clients_total += $_POST["attract_clients_Q$i"];
    }

    // get total value of "Internet Marketing" section
    for($i = 0; $i < 21; $i++) {
        $internet_marketing_total += $_POST["internet_marketing_Q$i"];
    }


    // calculate user personal "Time Management" score
    $time_management_rating = ($time_management_total / 19) / 5;

    // calculate user personal "Attract Clients" score
    $attract_clients_rating = ($attract_clients_total / 16) / 5;

    // calculate user personal "Internet Marketing" score
    $internet_marketing_rating = ($internet_marketing_total / 21) / 5;


    // add user ratings to array
    $user_rating[0] = array('percent' => $time_management_rating, 'description' => 'Time Management');
    $user_rating[1] = array('percent' => $attract_clients_rating, 'description' => 'Attract Clients');
    $user_rating[2] = array('percent' => $internet_marketing_rating, 'description' => 'Internet Marketing');


    // reverse sort the array - highest percentage first
    rsort($user_rating);


    // echo the description
    foreach($user_rating as $category) {
        foreach($category as $description) {
            echo $description[description] . "<br />";
        }
    }
}
?>
+3
source share
3 answers

You need to do:

foreach($user_rating as $category) {
        echo $category['description'] . "<br />";
}

Code Explanation:

Your array is like

$user_rating = array(array('percent' => ..., 'description' => ...), array(...));

This means that you have a two-dimensional array.

When you execute foreach($user_rating as $category), you iterate over the external array. This means it $categorywill be an array, namely array('percent' => ..., 'description' => ...).

, , , $description percent description.

PHP , . description , , , 0, .

$foo = 'bar';
echo $foo[0];
// echos 'b'

echo s, , :

$user_rating[0] = array('description' => 'Time Management');
$user_rating[1] = array('description' => 'Attract Clients');
$user_rating[2] = array('description' => 'Internet Marketing');

foreach($user_rating as $category) {
    echo '$category: ' . $category . PHP_EOL;
    foreach($category as $description) {
        echo '$description: '. $description . PHP_EOL;
        echo $description[description] . "<br />" .PHP_EOL;
    }
}

$category: Array
$description: Time Management
T<br />
$category: Array
$description: Attract Clients
A<br />
$category: Array
$description: Internet Marketing
I<br />
+6

echo $description[description]

to

echo $description['description']
+1

to try

foreach($user_rating as $category) {
            echo $category['description'] . "<br />";
    }
0
source

Source: https://habr.com/ru/post/1764826/


All Articles