SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Convert Multidimensional array to single array

    i want to convert multidimensional array to single array



    I am having this array

    PHP Code:
    array ( 
    => array ( 'sno' => 'q3''result' => '15', ),
    => array ( 'sno' => 'q1''result' => '5', ), 
    => array ( 'sno' => 'q2''result' => '10', ),

    i want this resulting array
    PHP Code:
    array ( 'q3' => '15''q1' => '5','q2' =>'10' 
    i tried foreach, but it gives the values (not an array ) like q3 15 q1 5 q2 10

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    317
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Try:

    PHP Code:
    $newArray = array();
    foreach(
    $multi as $array) {
     foreach(
    $array as $k=>$v) {
      
    $newArray[$k] = $v;
     }

    This loops through your multidimensional array and stores the results in the new array variable $newArray.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •