如果值存在,如何获取数组

时间:2017-02-20 08:02:32

标签: php arrays

我有一个像这样的PHP数组和我放在循环上的数组。

Array
(
    [0] => Array
        (
            [topic_id] => 973
            [reply_id] => 3549
            [user_id] => 1
        )

    [1] => Array
        (
            [topic_id] => 973
            [reply_id] => 3551
            [user_id] => 1
        )

    [2] => Array
        (
            [topic_id] => 973
            [reply_id] => 3553
            [user_id] => 1
            [status] => 1
        )

    [3] => Array
        (
            [topic_id] => 973
            [reply_id] => 3555
            [user_id] => 1
            [status] => 1
        )

    [4] => Array
        (
            [topic_id] => 973
            [reply_id] => 4401
            [reply_author] => 28
            [user_id] => 1
            [status] => 1
        )

    [5] => Array
        (
            [topic_id] => 232
            [reply_id] => 4405
            [reply_author] => 28
            [user_id] => 1
            [status] => 1
        )

)

现在循环项目具有唯一ID ID与数组中的reply_id 相同。我希望根据reply_id 拆分每个数组

所以,我想要的最终结果如下:

循环项目1(身份证号码:3549)

Array
(
    [topic_id] => 973
    [reply_id] => 3549
    [user_id] => 1
)

循环项目2(id:3551)

Array
(
    [topic_id] => 973
    [reply_id] => 3551
    [user_id] => 1
)

还有更多......

如何制作,请帮助。

1 个答案:

答案 0 :(得分:0)

编辑:在第一次回复后,我现在认为我理解你的意思。

$myListWithIds = array('3549','3551');
$idListCount = 2;

for($x = 0; $x < $idListCount; $x++)
{
    $key = array_search($myListWithIds[$x], array_column($myArray, 'reply_id'));
    echo "<pre>";
    print_r($myArray[$key]);
    echo "</pre>";
}

假设您的数组存储在变量$ myArray。