Welcome Guest, Not a member yet? Register   Sign In
Help me..
#1

someone help me.. i just stucked and cant get any idea how too do it.


Source array:
 

     ["data"] => array(10) {
         ["type"] => string(10) "controller"
         ["name"] => string(3) "eg1"
         ["description"] => string(5) "desc1"
         ["type2"] => string(8) "function"
         ["name2"] => string(2) "c2"
         ["controller2"] => string(4) "welcome"
         ["description2"] => string(6) "desc 2"
         ["type3"] => string(10) "controller"
         ["name3"] => string(2) "c3"
         ["description3"] => string(8) "c3 descr"
       }


from the above array how can i get new array like:

      array(
        array(
          'type' => 'controller',
          'name' => 'eg1',
          'description' => 'desc1'
        ),
         array(
          'type' => 'function',
          'controller' => 'welcome',
          'name' => 'c2',
          'description' => 'desc 2'
        ),
        array(
          'type' => 'controller',
          'name' => 'eg1',
          'description' => 'desc1'
        ),
        array(
          'type' => 'controller',
          'name' => 'c3',
          'description' => 'c3 descr'
        )
      );


data is submitted through a https://github.com/tristandenyer/Clone-s...ing-jQuery


thanks for help Shy
Reply
#2

Why not use array input fileds like <input type="text" name="type[]" />? Then you will get post data wich hold 4 arrays, one for each fieldname (type,name etc).
The number of entries in those arrays is the same as the amount of times the form hs been cloned and the keys between the arrays will corresponed so you can link them easely to create the array type you need.
Reply
#3

(01-04-2016, 03:52 PM)Diederik Wrote: Why not use array input fileds like <input type="text" name="type[]" />? Then you will get post data wich hold 4 arrays, one for each fieldname (type,name etc).
The number of entries in those arrays is the same as the amount of times the form hs been cloned and the keys between the arrays will corresponed so you can link them easely to create the array type you need.

Do you have an exemple for me Smile
Reply
#4

Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>
Reply
#5

(01-05-2016, 06:50 AM)PaulD Wrote: Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>

Sorry but this is a horrible idea.
Reply
#6

Why? It turns his array into the array he wanted?

I was just trying to help... but out of interest, why is it horrible?

Paul.
Reply
#7

(01-05-2016, 08:35 AM)PaulD Wrote: Why? It turns his array into the array he wanted?

I was just trying to help... but out of interest, why is it horrible?

Paul.

It's a lot of code for something can that be fixed by using POST arrays.
Reply
#8

(01-05-2016, 06:50 AM)PaulD Wrote: Or loop through your data in php. Use a counter to add the 1,2,3 to the end of the default type, name, description variables, and create your array from the data you have.
PHP Code:
<?php
     $source_data 
= array (
 
         "type1" => "controller",
 
         "name1" => "eg1",
 
         "description1" => "desc1",
 
         "type2" => "function",
 
         "name2" => "c2",
 
         "description2" => "desc 2",
 
         "type3" => "controller",
 
         "name3" => "c3",
 
         "description3" => "c3 descr",
 
    );

 
    $count 1;
 
    $checking TRUE;
 
    $output_data = array();
 
    
     
while ($checking)
 
        
          $temp_data 
= array();
 
         
          if 
(isset($source_data['type'.$count]))
 
         {
 
              $temp_data['type'] = $source_data['type'.$count];     
          
         
          if 
(isset($source_data['name'.$count]))
 
         {
 
              $temp_data['name'] = $source_data['name'.$count];
 
                  
          if 
(isset($source_data['description'.$count]))
 
         {
 
              $temp_data['description'] = $source_data['description'.$count];
 
         }
 
         
          if 
(count($temp_data) > 0)
 
         {
 
              $output_data[] = $temp_data;
 
         }
 
         else
          
{
 
              $checking FALSE;
 
         }
 
         
          
++$count;
 
         
     
}
 
    
     
echo print_r($output_data);
 
    
?>

many many thanks.....
Reply
#9

Ah, ok, it is, and I would not have a clunky solution like that in my code.

But I got the impression from the question that he was using an external system to generate the array, that I guessed he was having trouble with as Diederik's answer was good.

So, if you have a messy array, and need it in another format, and was not sure how to do it, or alter how you are getting the array in the first place, I think my answer is a good possible solution for the problem described.

Yes it is a lot of code, with flags, little error checking etc. But if a rejig of an array gets you out of a tricky corner how else can you do it.

Anyway, I think 'horrible' made me think you thought I had done something wrong in the code. Perhaps next time you call someones code 'horrible' you could either temper your vocabulary or add some qualification to it.

As I said, was just offering an answer to someone who said they were stuck, in the hope of helping, not proposing super efficient power coding as an entry into a coding competition.

Best wishes,

Paul :-(
Reply
#10

(01-05-2016, 08:49 AM)ngangchill Wrote: many many thanks.....

You are welcome :-)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB