Reputation:
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?.
Upvotes: -1
Views: 105
Reputation: 861
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.
Upvotes: 1