Поиск хеша в массиве значением

Это рабочий пример.

Форма HTML:

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

Код PHP:

<?php

    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename(

Это рабочий пример.

Форма HTML:

[110]

Код PHP:

[111]FILES['userfile']['name']); echo "<p>"; if (move_uploaded_file(

Это рабочий пример.

Форма HTML:

[110]

Код PHP:

[111]FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Upload failed"; } echo "</p>"; echo '<pre>'; echo 'Here is some more debugging info:'; print_r(

Это рабочий пример.

Форма HTML:

[110]

Код PHP:

[111]FILES); print "</pre>"; ?>
6
задан Axeman 8 October 2010 в 21:07
поделиться

2 ответа

With the power of grep

my @matching_items = grep {
  $_->{id} == 21
} @array_data_or;

If you know there will be only one item returned you can just do this:

my ($item) = grep {
  $_->{id} == 21
} @array_data_or;

(Untested, and I haven't written one of these in a while, but this should work)

20
ответ дан 8 December 2019 в 04:31
поделиться

If you're sure that the search always returns only one occurence or if you're interested in only the first match then you could use the 'first' subroutine found in List::Util

use List::Util;

my %matching_hash = %{ first { $_->{id} == 21 } @array_data_or };

I enclosed the subroutine call in the %{ } block to ensure that the RHS evaluates to a hash.

5
ответ дан 8 December 2019 в 04:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: