繁体   English   中英

PHP - 使用递归将“checked”连接到多维数组中的每个字符串值

[英]PHP - concatenate ' checked' to each string value in multidimensional array using recursion

我在 PHP 中有一个多维数组,我想使用递归将一个字符串连接到每个字符串元素上。 数组如下:

$array = Array
(
    [p] => Array
        (
            [0] => This Porsche 993 Carrera Cabriolet represents a great opportunity to acquire an open-top variant of one of the most coveted 911 models.
            [1] => First registered on 5 August 1994, M912 SGY displays 10,630 miles on the odometer with a clock change at 66,244 miles in 2014.
            [2] => The car’s Aventura Green metallic paintwork is reported to be in good condition, presenting well for its age and mileage.
            [3] => The Marble Grey leather interior is believed to be entirely original.
            [4] => Serviced by Porsche specialist Portiacraft in July 2020 at 76,598 miles, this consisted of an annual oil and filter service.
            [5] => The last MOT was undertaken on 6 July 2020 at 76,598 miles.
            [6] => It is supplied with a Porsche Club Great Britain folder with records of main dealer and specialist service history.
            [7] => This Porsche 911 Carrera Cabriolet presents in highly original and well-maintained condition.
            [8] => Summary of maintenance history:
            [9] => Array
                (
                    [strong] => The description of this auction lot is, to the best of the seller's knowledge, accurate and not misleading.
                )

            [10] => Array
                (
                    [strong] =>  All UK-registered cars and motorbikes on Collecting Cars are run through an online HPI check. This vehicle shows no insurance database markers for damage or theft, and has no finance owing.
                )

        )

    [ul] => Array
        (
            [li] => Array
                (
                    [0] => 04/11/1996 – 16,120 miles
                    [1] => 18/11/1998 – 25,086 miles
                    [2] => 09/09/1999 – 28,769 miles
                    [3] => 21/02/2000 – 31,469 miles
                    [4] => 22/06/2001 – 36,055 miles
                    [5] => 29/10/2002 – 40,781 miles
                    [6] => 02/03/2005 – 46,238 miles
                    [7] => 24/03/2006 – 49,459 miles
                    [8] => 03/07/2007 – 53,051 miles
                    [9] => 17/12/2008 – 56,582 miles
                    [10] => 20/05/2010 – 57,385 miles
                    [11] => 08/06/2011 – 61,653 miles
                    [12] => 15/05/2012 – 64,425 miles
                    [13] => 17/04/2013 – 66,026 miles
                    [14] => 07/06/2014 – 66,244 miles
                    [15] => 14/09/2015 – 68,411 miles
                    [16] => 27/02/2018 – 74,856 miles
                    [17] => 06/08/2019 – ~76,400 miles
                    [18] => 06/07/2020 – 76,598 miles
                )

        )

)

理想情况下,结果应如下所示:

$array = Array
(
    [p] => Array
        (
            [0] => This Porsche 993 Carrera Cabriolet represents a great opportunity to acquire an open-top variant of one of the most coveted 911 models. checked
            [1] => First registered on 5 August 1994, M912 SGY displays 10,630 miles on the odometer with a clock change at 66,244 miles in 2014. checked
            [2] => The car’s Aventura Green metallic paintwork is reported to be in good condition, presenting well for its age and mileage. checked
            [3] => The Marble Grey leather interior is believed to be entirely original. checked
            [4] => Serviced by Porsche specialist Portiacraft in July 2020 at 76,598 miles, this consisted of an annual oil and filter service. checked
            [5] => The last MOT was undertaken on 6 July 2020 at 76,598 miles. checked
            [6] => It is supplied with a Porsche Club Great Britain folder with records of main dealer and specialist service history. checked
            [7] => This Porsche 911 Carrera Cabriolet presents in highly original and well-maintained condition. checked
            [8] => Summary of maintenance history: checked
            [9] => Array
                (
                    [strong] => The description of this auction lot is, to the best of the seller's knowledge, accurate and not misleading. checked
                )

            [10] => Array
                (
                    [strong] =>  All UK-registered cars and motorbikes on Collecting Cars are run through an online HPI check. This vehicle shows no insurance database markers for damage or theft, and has no finance owing. checked
                )

        )

    [ul] => Array
        (
            [li] => Array
                (
                    [0] => 04/11/1996 – 16,120 miles checked
                    [1] => 18/11/1998 – 25,086 miles checked
                    [2] => 09/09/1999 – 28,769 miles checked
                    [3] => 21/02/2000 – 31,469 miles checked
                    [4] => 22/06/2001 – 36,055 miles checked
                    [5] => 29/10/2002 – 40,781 miles checked
                    [6] => 02/03/2005 – 46,238 miles checked
                    [7] => 24/03/2006 – 49,459 miles checked
                    [8] => 03/07/2007 – 53,051 miles checked
                    [9] => 17/12/2008 – 56,582 miles checked
                    [10] => 20/05/2010 – 57,385 miles checked
                    [11] => 08/06/2011 – 61,653 miles checked
                    [12] => 15/05/2012 – 64,425 miles checked
                    [13] => 17/04/2013 – 66,026 miles checked
                    [14] => 07/06/2014 – 66,244 miles checked
                    [15] => 14/09/2015 – 68,411 miles checked
                    [16] => 27/02/2018 – 74,856 miles checked
                    [17] => 06/08/2019 – ~76,400 miles checked
                    [18] => 06/07/2020 – 76,598 miles checked
                )

        )

)

我尝试了以下方法:

$addedChecked = $this->addCheckedRecursive($array);

private function addCheckedRecursive($array)
    {
        if(!is_array($array)) {
            return $array . ' checked';
        }

        foreach($array as $v) {
            $this->addCheckedRecursive($v);
        }
    }

并且

$addedChecked = array_walk_recursive($array, function (&$value) {
            $value .= ' checked';
        });

后者只是返回 true。

对于信息,每个数组的每个元素将始终是一个字符串,我还想保留当前的数组结构。 任何帮助表示赞赏。

像这样尝试:

$addedChecked = $this->addCheckedRecursive($array);

private function addCheckedRecursive($array)
{
    if (!is_array($array)) {
        return $array . ' checked';
    }
    else
    {
        for ($i = 0; $i < count($array); $i++) {
            $array[$i] = $this->addCheckedRecursive($array[$i]);
        }
    
        return $array;
    }
}

你为什么不尝试array_walk_recursivearray_replace_recursive

文档可以在这里找到

有一个内置的 function 可以用来实现你想要的。 如果你使用array_walk_recursive如下:

// Say you have your array $xmlArray
array_walk_recursive($xmlArray, function (&$value) {
    $value .= ' checked';
});

// Since $xmlArray is now modified (in place)
echo '<pre>';
print_r($xmlArray);
echo '</pre>';

如果您不希望 $xmlArray 作为副作用被更改,可以将数组的值副本分配给新变量。

$addedChecked = $xmlArray;
array_walk_recursive($addedChecked, function (&$value) {
    $value .= ' checked';
});

// Since $addedChecked is now modified (in place)
echo '<pre>';
print_r($addedChecked);
echo '</pre>';

function 通过引用接收数组,因此将直接修改数组作为 function 的副作用。这是需要注意的一件重要事情,它不返回数组,而仅返回 function 是否已在您的数组上成功执行给它。

这将遍历每个 key => value 对,如果值是数组类型,则递归执行此操作。 您可以简单地连接到值(我们通过引用传递值的地方)以使用 checked 更新它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM