forked from nubs/php-101
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhere.php
More file actions
27 lines (21 loc) · 832 Bytes
/
where.php
File metadata and controls
27 lines (21 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
$index = isset($argv[1]) ? $argv[1] : die("No input given\n");
$value = isset($argv[2]) ? $argv[2] : die("No input given\n");
$books = require_once __DIR__ . '/books.php';
/**
* Exercise 4 - Write a script that will search the books array and find all entries where the field specified by $index
* has the value of $value. Echo the id and title of each book that matches the given criteria.
*/
$list = [];
foreach ($books as $book) {
foreach ($book as $k => $v) {
$pos = strpos(strtolower($v), strtolower($value));
if ($k == $index && $pos !== false) {
$list[] = $book['id'];
}
}
}
echo "You searched for {$index} and {$value}. We found:" . PHP_EOL;
foreach($list as $hit) {
echo "Book id: " . $books[$hit]['id'] . ", by author " . $books[$hit]['author'] . PHP_EOL;
}