0

I am getting result set rows from the database in the following way.

N/w_id   N/W_name   N/W_contact   C_id    C_name   Seconds
  1         x           e1         1        abc     2000
  1         x           e1         1        abc     3450
  1         x           e1         2        def     2300
  1         x           e1         3        ghi     800
  1         x           e1         3        ghi     5678
  2         y           e2         12       abcd    2000
  2         y           e2         12       abcd    3450
  2         y           e2         21       defg    2300
  2         y           e2         32       ghij    800
  2         y           e2         32       ghij    5678

When i extracted all these rows in an array using $this->model->function_name() into an array called $result. All these rows are come as different arrays in one array but i want a single array from the $result in the below format.

array   =>  [0] =>  [N/W_id]        =>  1
                    [N/W_name]      =>  x
                    [N/W_contact]   =>  e1
                    [cha]           =>  [0] =>  [C_id]      =>  1
                                                [C_name]    =>  abc
                                                [Seconds]   =>  5450
                                        [1] =>  [C_id]      =>  2
                                                [C_name]    =>  def
                                                [Seconds]   =>  2300
                                        [2] =>  [C_id]      =>  3
                                                [C_name]    =>  ghi
                                                [Seconds]   =>  6478
             [1]   =>   [N/W_id]        =>  2
                        [N/W_name]      =>  y
                        [N/W_contact]   =>  e2
                        [cha]           =>  [0] =>  [C_id]      =>  12
                                                    [C_name]    =>  abcd
                                                    [Seconds]   =>  5450
                                            [1] =>  [C_id]      =>  21
                                                    [C_name]    =>  defg
                                                    [Seconds]   =>  2300
                                            [2] =>  [C_id]      =>  32
                                                    [C_name]    =>  ghij
                                                    [Seconds]   =>  6478
2
  • 1
    What is your question? Its confusing me. Commented Dec 28, 2012 at 6:29
  • I need ideas how to start............ Commented Dec 28, 2012 at 6:33

1 Answer 1

1

write a function like this for creating single array from multidimensional array

function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
} 
Sign up to request clarification or add additional context in comments.

3 Comments

this function create only single array but i don't want single array. I want it in a specific format.
@Venkat but i want a single array from the $result this is what you have written in your question.
I mentioned it in the way i wrote it and all the arrays should be in the single array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.