Prashanth Prashanth - 5 months ago 15
SQL Question

How to display multiple array value in PHP?

I am having an trouble in displaying values in PHP.

Value pass through URL

career.php?mode=1,2,3


Here is my code

$id = $_GET['mode']; // Id get from URL
//echo $id;
$query = "SELECT `job_title` FROM `job` WHERE `job_id` IN ($id)";
$res = mysqli_query($conn, $query);

foreach(($row = mysqli_fetch_array($res)) as $key1){
$key[] = $key1;
}


PHP Code:

<?php echo $key;?>


Hence it is in looping so it shows the last looped value. Is it possible to display the all values through loop.

Help me out guys!!

Answer

You have multiple options to output an array.

Echo and foreach

You c an loop through your array and echo each $key and $value

foreach ($keys as $key => $value) {
    echo $key." : ".$value."<br />";
}

print_r and var_dump

This is mostly used in debugging.

print_r($keys);

var_dump($keys);

Imploding

You can implode your array to echo the concatted values.

// The first parameter is the devider or separator
echo implode('', $keys);

Resources