Rayco
Rayco

Reputation: 117

PHP show one select object, from multiple arrays

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)
    );

?>

Upvotes: 0

Views: 69

Answers (5)

Dipen Soni
Dipen Soni

Reputation: 224

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."  ";
}
?>

Upvotes: 1

Vitaliy Ryaboy
Vitaliy Ryaboy

Reputation: 478

<?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/>";
}

Upvotes: 1

Marcovecchio
Marcovecchio

Reputation: 1332

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);

?>

Upvotes: 1

Harish Lalwani
Harish Lalwani

Reputation: 774

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

Upvotes: 1

Azeez Kallayi
Azeez Kallayi

Reputation: 2642

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'].' ';
}

Upvotes: 1

Related Questions