douchilian1009 2014-02-25 09:20
浏览 48

使用foreach生成数组

I have a $_POST array that looks like this

Array
(
    [0] => ed
    [1] => smith.co.uk
    [2] => http://edsmith.co.uk/smith.jpg
    [3] => Published
    [4] => ford attenborough
    [5] => ford.co.uk
    [6] => http://fordattenborough.co.uk/ford.jpg
    [7] => Pending Approval
    [8] => greg tolkenworth
    [9] => greg.co.uk
    [10] => http://greg.co.uk/greg.jpg
    [11] => In Future
)

I have an array that looks like this

"unique_id()" => array(
     "partner_name" => array(
         "id" => $shortname."_partner_name",
         "name" => "the_partner_name",
         "desc" => "The partner's name",
         "type" => "text",
         "value" => "",
         "placeholder" => "partner name",
     ),
    "partner_url" => array(
         "id" => $shortname."_partner_url",
         "name" => "the_partner_url",
         "desc" => "Url of the partner",
         "type" => "text",
         "value" => "",
         "placeholder" => "partner url",
     ),
     "partner_logo" => array(
         "id" => $shortname."_partner_logo",
         "name" => "the_partner_logo",
         "desc" => "Logo of the partner",
         "type" => "text",
         "value" => "",
         "placeholder" => "partner logo",
     ),
     "partner_status" => array(
         "id" => $shortname."_partner_status",
         "name" => "the_partner_status",
         "desc" => "The status of the partner",
         "type" => "select",
         "options" => array("Select Option","Publish", "Pending Approval", "In Future"),
         "std" => "Select Option",
     )),

The posted array is held by this variable $posted['partner_crud'] and i am trying using foreach

$u = uniqid();

foreach($posted['partner_crud'] as $key => $value){

$add_this_array = array($u => array(
         "partner_name" => array(
             "id" => $shortname."_partner_name",
             "name" => "the_partner_name",
             "desc" => "The partner's name",
             "type" => "text",
             "value" => $value,
             "placeholder" => "partner name",
         ),
        "partner_url" => array(
             "id" => $shortname."_partner_url",
             "name" => "the_partner_url",
             "desc" => "Url of the partner",
             "type" => "text",
             "value" => $value,
             "placeholder" => "partner url",
         ),
         "partner_logo" => array(
             "id" => $shortname."_partner_logo",
             "name" => "the_partner_logo",
             "desc" => "Logo of the partner",
             "type" => "text",
             "value" => $value,
             "placeholder" => "partner logo",
         ),
         "partner_status" => array(
             "id" => $shortname."_partner_status",
             "name" => "the_partner_status",
             "desc" => "The status of the partner",
             "type" => "select",
             "value" => $value,
             "options" => array("Select Option","Publish", "Pending Approval", "In Future"),
             "std" => "Select Option",
         )));
         }

to generate four arrays in the format as i have shown above.The problem is i am only able to produce an array with only the first value.How can i produce all the four arrays?.

  • 写回答

1条回答 默认 最新

  • douxing2156 2014-02-25 09:54
    关注

    It is very unusual to have post data like that. It makes something easy hard.

    If thats what you have however, you can try something like this:

    if (!is_int(count($posted['partner_crud']) / 4))
        throw new Exception("Invalid Post data");
    
    $result = array();
    foreach($posted['partner_crud'] as $key=>$array) {
       $dataset = floor($key / 4);
       $infoType = $key % 4;
       switch ($infoType) {
           case 0: 
               $result[$dataset]['partner_name'] = array(
                    'id'=> ...
                    'value'=>$value,
                );
                break;
           case 1: 
               $result[$dataset]['partner_url'] = array(
                    'id'=> ...
                    'value'=>$value,
                );
                break;
            case 2:
                 ....
            case 3:
                  ....
       }
    }
    
    return array($uid=>$result);
    

    You should make really sure that your post data has the right format however, and nothing gets out of order.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线