0

Hello i need help with this code, the problem is that i need to make a callback-function that only shows me the Price of the array
Like this image >>  Like this image [1] this is my code i tried much things but i couldn't figure out help me pls

 $boeken = array( array("titel"=> "Stoner", "auteur" => "John Williams", "genre"=> "Fictie", "prijs"=> 19.99), array("titel"=> "De cirkel", "auteur" => "Dave Eggers", "genre"=> "Fictie", "prijs"=> 22.50), array("titel"=> "Rayuela", "auteur" => "Cortazar", "genre"=> "Fictie", "prijs"=> 25.50) ); function prijslijst(){ array_walk($boeken, "prijslijst"); } ?>``` 

2 Answers 2

0

To change the value of an element from within the callback passed to array_walk() you merely need to pass the value by-reference to the callback. Any modification to the value modifies the original.

Like this:

array_walk($boeken, function (&$v) { $v = $v['prijs']; }); 

Here you're changing the value of each element in the array from an array to the value of $value['prijs'] which would change the first element from ["titel"=> "Stoner", "auteur" => "John Williams", "genre"=> "Fictie", "prijs"=> 19.99] to [19.99], for example.

Sign up to request clarification or add additional context in comments.

1 Comment

You're most welcome and welcome to StackOverflow. If you find the answer satisfies your question please be sure to accept it as this helps others in the community who have similar questions to more easily find the answer. If you find the answer helpful be sure to up vote it as this also helps curating content on the site.
0

you can use array_map

$boeken = array( array("titel"=> "Stoner", "auteur" => "John Williams", "genre"=> "Fictie", "prijs"=> 19.99), array("titel"=> "De cirkel", "auteur" => "Dave Eggers", "genre"=> "Fictie", "prijs"=> 22.50), array("titel"=> "Rayuela", "auteur" => "Cortazar", "genre"=> "Fictie", "prijs"=> 25.50) ); $prijs= array_map(function ($item){ return $item['prijs']; },$boeken); var_dump($prijs); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.