使用foreach生成数组

时间:2014-02-25 09:20:54

标签: php arrays foreach

我有一个$_POST数组,看起来像这样

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
)

我有一个看起来像这样的数组

"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",
     )),

已发布的数组由此变量$posted['partner_crud']保留,我正在尝试使用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",
         )));
         }

以我上面显示的格式生成四个数组。问题是我只能生成一个只有第一个值的数组。如何生成所有四个数组?。

1 个答案:

答案 0 :(得分:1)

拥有这样的帖子数据是非常不寻常的。它使事情变得容易。

如果那就是你所拥有的,你可以尝试这样的事情:

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);

你应该确定你的帖子数据格式正确,但没有任何问题。