从生成POST数据数组与参数地图 [英] Generating data array from POST data with a param-map

查看:82
本文介绍了从生成POST数据数组与参数地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个方面:


  1. $ _ POST阵列发布的数据

  2. $ PARAMS与用于所希望的数据阵列中的每个参数的路径阵列

      $ _ POST =阵列(
    '名'=> 马库斯,
    '发表'=> '今天​​',
    'URL'=>的http :: //示例.com,
    '布局'=> 一些信息......,
    );$ PARAMS =阵列(
    '名'=> Invoice.name',
    '发表'=> Page.published',
    'URL'=> Page.Data.url',
    '布局'=> Page.Data.layout',
    );


我想生成如下面的例子中,$ data数组。
我该怎么做?

请注意如何从$ params数组中的路径是用于构建数据阵列的钥匙,从$ _POST数组中的数据填充它。

  $数据=阵列(
    '用户'=>阵列(
        '名'=> 马库斯,
    )
    页= GT;阵列(
        '发表'=> '今天​​',
        '数据'=>阵列(
            'URL'=>的http :: //示例.com,
            '布局'=> 一些信息......,
        )
    )
);


解决方案

我会用引用的变量:

  $ =后阵列(//我给它改名
  '名'=> 马库斯,
  '发表'=> '今天​​',
  'URL'=>的http :: //示例.com,
  '布局'=> 一些信息......,
);$ PARAMS =阵列(
  '名'=> Invoice.name',
  '发表'=> Page.published',
  'URL'=> Page.Data.url',
  '布局'=> Page.Data.layout',
  );呼应'< pre>'; //只为的var_dump()
的foreach($岗位作为重点$ = GT; $ VAR){//把每$ _ POST变量
  $参数= $ PARAMS [$关键]; //拿方案领域
  $ PATH =爆炸(,$参数'。'); //拿方案领域的数组
  $ TEMP =阵列(); //临时数组操作
  $ temp_original =安培; $温度; //我们需要这个一样,我们要忘记临时
  的foreach($路径$ pathvar){//把每场计划
    $ TEMP =&放大器; $临时[$ pathvar]; //走向深入
    }
  $ TEMP = $ VAR; //这是最后一个,将其插入
  后续代码var_dump($ temp_original); //仔细analize输出
}

所有你现在要做的就是将它们都结合起来,他们不正是你想要的,但是这将是容易的。

和请注意,每个 $ temp_original 字段在 $岗位可变数据指点! (&放大器;字符串而不是字符串)。您可能需要以某种方式克隆。

I have the following two things:

  1. $_POST array with posted data
  2. $params array with a path for each param in the desired data array.

    $_POST = array(
    'name'      => 'Marcus',
    'published' => 'Today',
    'url'       => 'http:://example.com',
    'layout'    => 'Some info...',
    );
    
    $params = array(
    'name'      => 'Invoice.name',
    'published' => 'Page.published',
    'url'       => 'Page.Data.url',
    'layout'    => 'Page.Data.layout',
    );
    

I would like to generate the $data array like the example below. How can I do that? Notice how the "paths" from the $params array are used to build the keys for the data array, filling it with the data from the $_POST array.

$data = array(
    'User' => array(
        'name' => 'Marcus',
    ),
    'Page' => array(
        'published' => 'Today',
        'Data' => array(
            'url' => 'http:://example.com',
            'layout' => 'Some info...',
        ),
    ),
);

解决方案

I would use referenced variables:

$post = array( // I renamed it
  'name'      => 'Marcus',
  'published' => 'Today',
  'url'       => 'http:://example.com',
  'layout'    => 'Some info...',
);

$params = array(
  'name'      => 'Invoice.name',
  'published' => 'Page.published',
  'url'       => 'Page.Data.url',
  'layout'    => 'Page.Data.layout',
  );

echo '<pre>'; // just for var_dump()
foreach($post as $key=>$var){ // take each $_POST variable
  $param=$params[$key]; // take the scheme fields
  $path=explode('.',$param); // take scheme fields as an array
  $temp=array(); // temporary array to manipulate
  $temp_original=&$temp; // we need this the same as we're going to "forget" temp
  foreach($path as $pathvar){ // take each scheme fields
    $temp=&$temp[$pathvar]; // go deeper
    }
  $temp=$var; // that was the last one, insert it
  var_dump($temp_original); // analize carefully the output
}

All you have to do now is to combine them all, they are not exactly what you want, but this will be easy.

And please note, that each $temp_original fields are pointing at $post variable data! (&string instead of string). You may want to clone it somehow.

这篇关于从生成POST数据数组与参数地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆