Jump to content

[HELP] Arrays problem.


KanaTronix
Go to solution Solved by KanaTronix,

Recommended Posts

Hey guys. I am creating an application that require dynamic fields generated by javascript. I have managed to create the forms the database and pretty much may things. I come to hit a roadblock with this array problem. I am trying to get the data from the browser by checking which one contain more than one value and then transverses through another array this one

$nyingi = [ 'location_' => ['text' => 'loc_txt','priority' => 'loc_order'], 
'photo_' => ['path' => 'p_txt'], 
'video_' => ['path' => 'v_txt'], 
'product_' => ['p_id' => 'pt_q', 'text' => 'pt_txt', 'expire' => 'pt_xpr', 'title' => 'pt_tt'], 
'service_' => ['p_id' => 'st_q', 'text' => 'st_txt', 'expire' => 'st_xpr', 'title' => 'st_tt'], 
'hours_' => ['time1' => 't1', 'time2' => 't2', 'type' => 'tt'], 
'map_' => ['text' => 'm_txt']
];

use this loop


foreach($nyingi as $ins => $vl){

foreach($vl as $fld => $box){
$uwazi = $this->input->post($box);
if(is_array($uwazi) && 1<count($uwazi)){

foreach($uwazi as $bb){

$one[$ins][$fld][] = $bb;


}
}
}
} 

and get this result

Array
(
[location_] => Array
(
[text] => Array
(
[0] => Main Office
[1] => Branch 1
[2] => Branch 2
)

[priority] => Array
(
[0] => 1
[1] => 2
[2] => 3
)

)

[photo_] => Array
(
[path] => Array
(
[0] => Photo 1
[1] => Photo 2
[2] => Photo 3
)

)

[video_] => Array
(
[path] => Array
(
[0] => Video 1
[1] => Video 2
[2] => Video 3
)

)

what I want to get is this

Array
(
[location_] => Array
(
[text] => Main Office
[priority] => 1
)

[photo_] => Array
(
[path] => Photo 1
)

[video_] => Array
(
[path] => Video 1
)
[product_] => Array
(
[p_id] =>Product 2 photo
[text] => Product 2 Description
[expire] => Product 2 expire
[title] => Product 2
)

)
 

)

for each one of those. Any ideas where I am getting wrong.

Link to comment
Share on other sites

As Barand said - this is very confusing and makes no sense to an outsider.

 

That said let me ask.  How did you get the first array?  Seems to be very organized and makes sense, but then you have that crazy code trying to manipulate it (I think) and you ask how to morph it into a second array structure.  Why do you wish to do this?

 

IMHO - people rely on arrays way too much.  And structures this deep are just begging to be re-analyzed and avoided.  Again - IMHO.

Link to comment
Share on other sites

Any input is deeply appreciated. What I want to get is what's important if you have an idea just write man.

 

The content of the output produced by your code bears no relation to the content of what you claim is the input array. So I am not going to struggle with trying to work out what you are trying to do and how the data should be manipulated.

Link to comment
Share on other sites

  • Solution

Thanks for the replies. But I have managed to fix it. it was so simple and make me wonder how stupid I am.

I fix the loop and be like this

                        foreach($nyingi as $ins => $vl){
				foreach($vl as $fld => $box){
					$uwazi = $this->input->post($box);
					if(is_array($uwazi) && 0<count($uwazi)){
						$q=0;
						foreach($uwazi as $bb){
							$one[$ins][$q][$fld] = $bb; $q++;
						}
					}
				}
			}

The inputs are from a dynamic form form with multiple dynamic fields.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.