F
F
Fuckingawesomenigga2019-04-08 13:13:31
PHP
Fuckingawesomenigga, 2019-04-08 13:13:31

How to select an element from an array?

There is an array items.php:

<?PHP
return array(
  array(
    'number'	=> '10',
    'sysname'	=> 'name1',
    'url' 		=> '/',
    'title'		=> 'Title 1',
    'promo'		=> 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'),
  array(
    'number'	=> '20',
    'sysname'	=> 'name2',
    'url' 		=> '/',
    'title' 	=> 'Title 2',
    'promo' 	=> 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'),
);

There is an article.php file, which, through get, should find the desired element
$items = include 'items.php';

echo (isset($_GET["brand"])) ? $_GET["brand"] : "";

$item = array(
  '' => '',
);

How to put the required element in item, by the 'sysname' parameter, ? For example /article.php?brand=name2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
David, 2019-04-08
@fuckingawesomenigga

$foundItem = null;
foreach ($items as $item) {
  if ($item['sysname'] == $_GET['brand']) {
    $foundItem = $item;
    break;
  }
}

if (!is_null($foundItem)) {
  // нашли
} else {
  // не нашли
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question