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

如何解决PHP - 使用递归将“已检查”连接到多维数组中的每个字符串值

我在 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,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,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,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

文档可以在 here

,

有一个内置函数,您可以使用它来实现您想要的。 如果您按如下方式使用 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>';

函数通过引用接收数组,因此会直接修改数组作为函数的副作用。这是需要注意的一件重要事情,它不返回数组,而只返回函数是否​​在您提供的数组上成功执行。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-