0

how can i show "prijs" with the number behind it, and leave the rest out. I tried to do this for an experimental school project. It should display:

prijs: 19.99

prijs: 22.5

prijs: 25.5

<?php
function prijsLijst($titel, $auteur, $genre, $prijs) {
echo "$prijs de prijs.<br>";
}

$boeken = array (
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 19.99),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 22.50),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 25.50)
    );

?>

5 Answers 5

1

You can use foreach and echo the element you want. please see the below code, it may help you

  $boeken = array (
  array("titel"=> "Stoner", "auteur" => "John Williams",
 "genre" => "fictie", "prijs" => 19.99),
  array("titel"=> "Stoner", "auteur" => "John Williams",
  "genre" => "fictie", "prijs" => 22.50),
  array("titel"=> "Stoner", "auteur" => "John Williams",
 "genre" => "fictie", "prijs" => 25.50)
);

foreach($boeken as $single)
{
    echo "prijs ".$single['prijs'].' ';
}
Sign up to request clarification or add additional context in comments.

Comments

1
 array_walk_recursive($boeken, 'prijsLijst');
 function prijsLijst($item) {
    echo "prijs: ".$item['prijs']." ";
 }

Comments

1

I hope this is what you need. Just iterate on the outer array and get the prijs key on each one:

<?php
function prijsLijst($array) {
  $result = '';
  foreach($array as $item)
  {
    $result .= "prijs: ".$item['prijs']." ";
  }
  return $result;
}

$boeken = array (
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 19.99),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 22.50),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 25.50)
    );

echo prijsLijst($boeken);

?>

Comments

1
<?php

$boeken = array (
  array("titel"=> "Stoner", "auteur" => "John Williams","genre" => "fictie", "prijs" => 19.99),
  array("titel"=> "Stoner", "auteur" => "John Williams","genre" => "fictie", "prijs" => 22.50),
  array("titel"=> "Stoner", "auteur" => "John Williams","genre" => "fictie", "prijs" => 25.50)
);

foreach($boeken as $item){
  echo "prijs: ".$item['prijs']."<br/>";
}

Comments

1

you can take count of array and use loop to count and echo value this is perfect way to echo your value how you want.

<?php 
$boeken = array (
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 19.99),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 22.50),
array("titel"=> "Stoner", "auteur" => "John Williams",
    "genre" => "fictie", "prijs" => 25.50)
    );
 $count= count($boeken);
for($i=0;$i < $count;$i++)
{
    $value= $boeken[$i]['prijs'];
    $name= array_search("".$value."",$boeken[$i]);
    echo $name.": ".$value."  ";
}
?>

1 Comment

you can perfectly display your data how you want thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.