在PHP多维数组中搜索部分字符串,并返回包含该字符串的特定数组

问题描述 投票:0回答:2

我有一个多维数组。我有一个日期(例如:2021-06-11),需要在该多维数组中的SESSION数组(例如:2021-06-11T18:00:00Z)中的特定键(startTime)中进行搜索。如果找到匹配项(部分匹配。此处将2021-06-11T18:00:00Z中的2021-06-11视为匹配项),那么我需要在该特定会话中返回另一个键(seatsAvailable) array()。

在下面的示例中,我正在下面的多维数组中搜索“ 2021-06-11”,我需要在此处返回“ seatsAvailable”键的值为“ 8”。

我尝试了其他各种解决方案,这些解决方案在与多维数组有关的各种SO问题中都有规定,但没有成功。任何帮助或想法将不胜感激。预先感谢。

    Array
(
    [requestStatus] => Array
        (
            [success] => 1
            [version] => v1
        )

    [sessions] => Array
        (
            [0] => Array
                (
                    [id] => 163728043
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-01T18:00:00Z
                    [endTime] => 2021-06-01T23:00:00Z
                    [startTimeLocal] => 2021-06-01 20:00:00
                    [endTimeLocal] => 2021-06-02 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 163728047
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-02T18:00:00Z
                    [endTime] => 2021-06-02T23:00:00Z
                    [startTimeLocal] => 2021-06-02 20:00:00
                    [endTimeLocal] => 2021-06-03 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [2] => Array
                (
                    [id] => 163728055
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-03T18:00:00Z
                    [endTime] => 2021-06-03T23:00:00Z
                    [startTimeLocal] => 2021-06-03 20:00:00
                    [endTimeLocal] => 2021-06-04 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [3] => Array
                (
                    [id] => 163728098
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-04T18:00:00Z
                    [endTime] => 2021-06-04T23:00:00Z
                    [startTimeLocal] => 2021-06-04 20:00:00
                    [endTimeLocal] => 2021-06-05 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [4] => Array
                (
                    [id] => 163728074
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-05T18:00:00Z
                    [endTime] => 2021-06-05T23:00:00Z
                    [startTimeLocal] => 2021-06-05 20:00:00
                    [endTimeLocal] => 2021-06-06 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [5] => Array
                (
                    [id] => 163728100
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-06T18:00:00Z
                    [endTime] => 2021-06-06T23:00:00Z
                    [startTimeLocal] => 2021-06-06 20:00:00
                    [endTimeLocal] => 2021-06-07 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [6] => Array
                (
                    [id] => 163728057
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-07T18:00:00Z
                    [endTime] => 2021-06-07T23:00:00Z
                    [startTimeLocal] => 2021-06-07 20:00:00
                    [endTimeLocal] => 2021-06-08 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [7] => Array
                (
                    [id] => 163728116
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-08T18:00:00Z
                    [endTime] => 2021-06-08T23:00:00Z
                    [startTimeLocal] => 2021-06-08 20:00:00
                    [endTimeLocal] => 2021-06-09 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [8] => Array
                (
                    [id] => 163728097
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-09T18:00:00Z
                    [endTime] => 2021-06-09T23:00:00Z
                    [startTimeLocal] => 2021-06-09 20:00:00
                    [endTimeLocal] => 2021-06-10 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [9] => Array
                (
                    [id] => 163728052
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-10T18:00:00Z
                    [endTime] => 2021-06-10T23:00:00Z
                    [startTimeLocal] => 2021-06-10 20:00:00
                    [endTimeLocal] => 2021-06-11 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )

            [10] => Array
                (
                    [id] => 163728080
                    [productCode] => PWQRX6
                    [startTime] => 2021-06-11T18:00:00Z
                    [endTime] => 2021-06-11T23:00:00Z
                    [startTimeLocal] => 2021-06-11 20:00:00
                    [endTimeLocal] => 2021-06-12 01:00:00
                    [allDay] => 
                    [seats] => 8
                    [seatsAvailable] => 8
                    [priceOptions] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 1100
                                    [label] => Adult
                                    [id] => 4000244
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                            [1] => Array
                                (
                                    [price] => 1000
                                    [label] => Child
                                    [id] => 4000243
                                    [seatsUsed] => 1
                                    [productCode] => PWQRX6
                                )

                        )

                )


        )

)

我尝试了递归函数,并使用preg_grep,但没有用。$ sessionresult是我的数组,$ keyword是需要搜索的数组。我不确定这些是否是解决我问题的正确方法选项1:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

    $keyword = "2021-06-11";
echo in_array_r($keyword, $sessionresult) ? 'found' : 'not found';  

Option2:

$result_array = preg_grep("/{$keyword}/i",$sessionresult);
php arrays regex multidimensional-array preg-match
2个回答
1
投票

由于您要获取的数据与值相关,而不与值本身相关,因此写一些更具体的内容可能会更好。您还实现了递归in_array()而不是搜索。

而是在sessions数组中查找具有您要查找的值的条目,它在每个条目stripos()上使用startTime,如果找到任何内容,则返回该条目。然后,您可以从条目中提取任何详细信息。如果它搜索所有条目但没有找到任何东西,则仅返回false ...

function searchSessions( string $keyword, array $data )   {
    foreach ( $data as $key => $entry ) {
        if ( stripos($entry['startTime'], $keyword) !== false )    {
            return $entry;
        }
    }

    return false;
}

$keyword = "2021-06-02";
if ( $session = searchSessions( $keyword, $sessionresult['sessions'] ))    {
    echo $session['seats'];   
}
else    {
    echo "not found";
}

0
投票

您可以尝试使用以下脚本在提供的多维数组中的“ 2021-06-11”中查找值。

<?php
// PHP program to carry out search in multidimensional array
function arr_custom_search($array, $key, $value)
{
    $results = array();

    // if it is array
    if (is_array($array))
    {

        // if array has required key and value
        // matched store result
        if (isset($array[$key]) && substr($array[$key], 0, 10) == $value)
        {
            $results[] = $array;
        }

        // Iterate for each element in array
        foreach ($array as $subarray)
        {

            // recur through each element and append result
            $results = array_merge($results, arr_custom_search($subarray, $key, $value));
        }
    }

    return $results;
}

// INPUT: Multidimensional array
$arr_input = array(
    "requestStatus" => array(
        'success' => 1,
        'version' => 'v1'
    ) ,
    "sessions" => array(
        array(
            'id' => '163728041',
            'productCode' => 'PWQRX6',
            'startTime' => '2021-06-01T18:00:00Z',
            'endTime' => '2021-06-01T23:00:00Z',
            'startTimeLocal' => '2021-06-01 20:00:00',
            'endTimeLocal' => '2021-06-02 01:00:00',
            'allDay' => '',
            'seats' => 8,
            'seatsAvailable' => 8,
            'priceOptions' => array(
                array(
                    'price' => 1100,
                    'label' => 'Adult',
                    'id' => 4000244,
                    'seatsUsed' => 1,
                    'productCode' => 'PWQRX6'
                ) ,
                array(
                    'price' => 1000,
                    'label' => 'Child',
                    'id' => 4000245,
                    'seatsUsed' => 1,
                    'productCode' => 'PWQRX6'
                )
            )
        ) ,
        array(
            'id' => '163728042',
            'productCode' => 'PWQRX6',
            'startTime' => '2021-06-01T18:00:00Z',
            'endTime' => '2021-06-01T23:00:00Z',
            'startTimeLocal' => '2021-06-01 20:00:00',
            'endTimeLocal' => '2021-06-02 01:00:00',
            'allDay' => '',
            'seats' => 8,
            'seatsAvailable' => 8
        ) ,
        array(
            'id' => '163728043',
            'productCode' => 'PWQRX6',
            'startTime' => '2021-06-11T18:00:00Z',
            'endTime' => '2021-06-01T23:00:00Z',
            'startTimeLocal' => '2021-06-01 20:00:00',
            'endTimeLocal' => '2021-06-02 01:00:00',
            'allDay' => '',
            'seats' => 8,
            'seatsAvailable' => 8
        ) ,
        array(
            'id' => '163728044',
            'productCode' => 'PWQRX6',
            'startTime' => '2021-06-01T18:00:00Z',
            'endTime' => '2021-06-01T23:00:00Z',
            'startTimeLocal' => '2021-06-01 20:00:00',
            'endTimeLocal' => '2021-06-02 01:00:00',
            'allDay' => '',
            'seats' => 8,
            'seatsAvailable' => 8
        ) ,
        array(
            'id' => '163728045',
            'productCode' => 'PWQRX6',
            'startTime' => '2021-06-11T18:00:00Z',
            'endTime' => '2021-06-01T23:00:00Z',
            'startTimeLocal' => '2021-06-01 20:00:00',
            'endTimeLocal' => '2021-06-02 01:00:00',
            'allDay' => '',
            'seats' => 8,
            'seatsAvailable' => 8
        )
    )
);

$search_key = 'startTime';
$search_value = '2021-06-11';
$arr_output = arr_custom_search($arr_input, $search_key, $search_value);

echo '<pre>';
print_r($arr_output);
?>

它将在输出下面打印:

Array
(
    [0] => Array
        (
            [id] => 163728043
            [productCode] => PWQRX6
            [startTime] => 2021-06-11T18:00:00Z
            [endTime] => 2021-06-01T23:00:00Z
            [startTimeLocal] => 2021-06-01 20:00:00
            [endTimeLocal] => 2021-06-02 01:00:00
            [allDay] => 
            [seats] => 8
            [seatsAvailable] => 8
        )

    [1] => Array
        (
            [id] => 163728045
            [productCode] => PWQRX6
            [startTime] => 2021-06-11T18:00:00Z
            [endTime] => 2021-06-01T23:00:00Z
            [startTimeLocal] => 2021-06-01 20:00:00
            [endTimeLocal] => 2021-06-02 01:00:00
            [allDay] => 
            [seats] => 8
            [seatsAvailable] => 8
        )
)

希望这会对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.