使用cakephp的哈希将内部数组合并到父数组

时间:2015-09-03 18:03:38

标签: php arrays cakephp multidimensional-array cakephp-3.0

我有以下数组

$array['projects'] = [
    'name1' => [
        'task' => [
            'tags' => ['value1', 'value2'],
            'email' => 'email2',
            'description' => 'mpla'
        ],
        'email' => 'email1',
        'tags' => ['value1', 'value3'],
        'title' => 'mpla'
    ]
];

无论如何我可以使用CakePHP 3的Hash类或者另一类CakePHP框架来实现以下结果:

$array['projects'] = [
    'name1' => [
        'email' => 'email2',
        'tags' => ['value1', 'value2'],
        'title' => 'mpla'
        'desciption' => 'mpla'
    ]
];

如果您还知道任何其他可以处理数组并完成工作的软件包,那么它就可以了。

1 个答案:

答案 0 :(得分:2)

不确定使用Cake的哈希实用程序可以轻松实现这一点。您可以使用combine()轻松提取由title索引的数组项,但不确定如何使用Hash提取Hash::combine($array, 'projects.{s}', 'projects.{s}.task'); 值并将其与其他数组元素组合: -

foreach

也许最简单的解决方案是使用这样的$data = []; foreach ($array['projects'] as $value) { $data['projects'] = $value['task'] + ['title' => $value['title']]; } 循环: -

private static void FtpUpload(String username, String password, String address, 
                              Int32 port, Boolean usePassive, String filePath)
{
    try
    {
        String fileName = "";

        fileName = Path.GetFileName(filePath);
        FtpWebRequest request = null;
        request = (FtpWebRequest)FtpWebRequest.Create(String.Format("ftp://{0}", address));

        request.Credentials = new NetworkCredential(
            username,
            password);
        request.UsePassive = usePassive;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Timeout = -1;
        request.ReadWriteTimeout = -1;
        request.KeepAlive = false;
        request.Proxy = null;

        Console.WriteLine(String.Format("Uploading {0}...", fileName));

        Stream ftpStream = null;

        using (ftpStream = request.GetRequestStream())
        {
            ftpStream.WriteTimeout = -1;
            ftpStream.ReadTimeout = -1;

            using (FileStream file = File.OpenRead(filePath))
            {
                file.CopyTo(ftpStream);
            }
        }
    }
    catch
    {
        throw;
    }            
}