Home > Net >  How to echo all values from a specific array?
How to echo all values from a specific array?

Time:11-12

This is what I'm doing:

for($i = 0; $i <= $max; $i  ) {
    if(isset($media[$i])) {
        $combined[] = ["type" => "media", "value" => $media[$i]];
    }
    if(isset($content[$i])) {
        $combined[] = ["type" => "content", "value" => $content[$i]];
    }
    if(isset($yt[$i])) {
        $combined[] = ["type" => "youtube", "value" => $yt[$i]];
    } 
}

echo implode(', ', array_column($combined, 'media'));

Basically I need to echo all values of "media" as a single string with value separated commas.

Tried this too:

echo implode(', ', array_map(function ($entry) {
  return $entry['media'];
}, $combined));

CodePudding user response:

$string = '';
foreach ( $combined as $com ) {
    if ( $com['type'] === 'media' ) {
        $string .= $com['value'] . ',';
    }
}
$string = rtrim( $string, ',');  // remove trailing comma
echo $string;
  •  Tags:  
  • php
  • Related