有条件地替换多维PHP数组中的值

问题描述 投票:-1回答:1

这里还有其他一些关于替换多维数组中的值的问题,但是我没有发现关于我正试图做的事情的确切信息。

我有一个从API获得的数组,在将API有效负载发送到浏览器之前,我需要根据数组树中的其他值更新一些值。

在数组中,当sale[0] === true时,我正在寻找某种逻辑,然后将[price][0]值替换为相应的新售价。

使用foreach,我可以轻松地遍历数组中的每个节点,但是我不确定一旦遍历数组后,如果/当销售节点==时,如何用新价格更新原始数组= true。

Array
(
    [response] => Array
        (
            [0] => Array
                (
                    [results] => Array
                        (
                            [0] => Array
                                (
                                    [items] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => Array
                                                        (
                                                            [0] => 846471605959
                                                        )

                                                    [title] => Array
                                                        (
                                                            [0] => Test Item 846471605959
                                                        )

                                                    [imageURL] => Array
                                                        (
                                                            [0] => https://foo/bar/images/846471605959.jpg
                                                        )

                                                    [itemURL] => Array
                                                        (
                                                            [0] => https://foo/bar/item/846471605959
                                                        )


                                                    [price] => Array
                                                        (
                                                            [0] => 799.00
                                                        )

                                                    [sale] => Array
                                                        (
                                                            [0] => true
                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [id] => Array
                                                        (
                                                            [0] => 414953260545
                                                        )

                                                    [title] => Array
                                                        (
                                                            [0] => Test Item 414953260545
                                                        )

                                                    [imageURL] => Array
                                                        (
                                                            [0] => https://foo/bar/images/414953260545.jpg
                                                        )

                                                    [itemURL] => Array
                                                        (
                                                            [0] => https://foo/bar/item/414953260545
                                                        )


                                                    [price] => Array
                                                        (
                                                            [0] => 199.00
                                                        )

                                                    [sale] => Array
                                                        (
                                                            [0] => false
                                                        )

                                                )

                                            [2] => Array
                                                (
                                                    [id] => Array
                                                        (
                                                            [0] => 684865199812
                                                        )

                                                    [title] => Array
                                                        (
                                                            [0] => Test Item 684865199812
                                                        )

                                                    [imageURL] => Array
                                                        (
                                                            [0] => https://foo/bar/images/684865199812.jpg
                                                        )

                                                    [itemURL] => Array
                                                        (
                                                            [0] => https://foo/bar/item/684865199812
                                                        )


                                                    [price] => Array
                                                        (
                                                            [0] => 699.00
                                                        )

                                                    [sale] => Array
                                                        (
                                                            [0] => false
                                                        )

                                                )

                                            [3] => Array

                                                (
                                                    [id] => Array
                                                        (
                                                            [0] => 987800965761
                                                        )

                                                    [title] => Array
                                                        (
                                                            [0] => Test Item 987800965761
                                                        )

                                                    [imageURL] => Array
                                                        (
                                                            [0] => https://foo/bar/images/987800965761.jpg
                                                        )

                                                    [itemURL] => Array
                                                        (
                                                            [0] => https://foo/bar/item/987800965761
                                                        )

                                                    [price] => Array
                                                        (
                                                            [0] => 499.00
                                                        )

                                                    [sale] => Array
                                                        (
                                                            [0] => true
                                                        )

                                                )

                                            [4] => Array

                                                (
                                                    [id] => Array
                                                        (
                                                            [0] => 005457536677
                                                        )

                                                    [title] => Array
                                                        (
                                                            [0] => Test Item 005457536677
                                                        )

                                                    [imageURL] => Array
                                                        (
                                                            [0] => https://foo/bar/images/005457536677.jpg
                                                        )

                                                    [itemURL] => Array
                                                        (
                                                            [0] => https://foo/bar/item/005457536677
                                                        )

                                                    [price] => Array
                                                        (
                                                            [0] => 99.00
                                                        )

                                                    [sale] => Array
                                                        (
                                                            [0] => false
                                                        )

                                                )

                                        )

                                )

                        )

                }

        )

)
php arrays multidimensional-array
1个回答
1
投票

将参考变量用于foreach迭代变量,然后可以就地更新元素。

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