Converting array?

Hi guys,

I need a little help here.

How do I convert this array,


Array
(
    [0] => Array
        (
            [pathwayid] => 9
            [namepathway] => path3
            [image] => http://coder9.com/images/buddha.jpg
            [description] => just three
            [error] => edit
            [title] => Edit Pathways
        )
)

To this,


Array
(
    [pathwayid] => 9
    [namepathway] => path3
    [image] => http://coder9.com/images/buddha.jpg
    [description] => just three
    [error] => edit
    [title] => Edit Pathways
)

What php function should I use.
Well I will try to figure this out while I’m waiting for your answer.
Thanks in advance.

Anyway I’ll try this method.


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

Looks like the codes above doesn’t work…

I dissected the codes above.
And modified it a little bit.


	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, $this->array_flatten($value)); 
	    } 
	    else { 
	      $result[$key] = $value; 
	    } 
	  } 
	  return $result; 
	} 	

And boom!!!
It works… lol

Problem solved!

Hmm, your first example worked for me but not the second.

Original

Array
(
    [0] => Array
        (
            [pathwayid] => 9
            [namepathway] => path3
            [image] => http://coder9.com/images/buddha.jpg
            [description] => just three
            [error] => edit
            [title] => Edit Pathways
        )

)

array_flatten 1

Array
(
    [pathwayid] => 9
    [namepathway] => path3
    [image] => http://coder9.com/images/buddha.jpg
    [description] => just three
    [error] => edit
    [title] => Edit Pathways
)

array_flatten 2



Fatal error:  Using $this when not in object context in C:\\Program Files\\............\	est52.php on line 51

Anyway glad you got it working.

@Drummin ;
I use it inside class.

Looks to me that your array is better off converted into an object, if I were you I’d create a class for it rather than using an associative array.

Just this’ll do it:

$your_array = $your_array[0];

Yes. All you have is an array inside of an array, no need to do anything crazy here. Keep in mind that based on this “result” you have, it looks like its a possibility of there being multiple results ($array[0] as well as $array[1])