Advanced Loop problem

Discussion in 'PHP' started by kevin_peters, Oct 3, 2009.

  1. #1
    Hey guys,

    First time poster and hoping somebody could help me out.

    Im having major headaches over this, im trying to get a loop to accomplish this structure


    Putting this array

    (
        [0] => Array
            (
                [cat_id] => 1
                [sub_cat_name] => Apples
                [sub_cat_id] => 1
            )
     
        [1] => Array
            (
                [cat_id] => 1
                [sub_cat_name] => Oranges
                [sub_cat_id] => 2
            )
    
          [3] => Array
            (
                [cat_id] => 2
                [sub_cat_name] => Bike
                [sub_cat_id] => 3
            )
     
           [4] => Array
            (
                [cat_id] => 2
                [sub_cat_name] => Boat
                [sub_cat_id] => 4
            )
    
     
    
    
    )
    
    Code (markup):
    INTO this;---->

    [ ['cat_id_1' , [ ['sub_cat_name_1' , 'sub_cat_id' ] , ['sub_cat_name_2','sub_cat_id'] ] ] ,
    [ ['cat_id_2' , [ ['sub_cat_name_2' , 'sub_cat_id' ] , ['sub_cat_name_2','sub_cat_id'] ] ]
    
    Code (markup):
    and so on


    any help much appreciated.

    cheers,

    kevin
     
    kevin_peters, Oct 3, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Assume your array is $array
    
    $result = array();
    foreach ($array as $value){
    	$result[$value['cat_id']][] = array(
    		$value['sub_cat_name'],$value['sub_cat_id']
    	);
    }
    
    PHP:
    May be this would help.
     
    AsHinE, Oct 3, 2009 IP
  3. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you want the result to display array like this string
     
    lmao, Oct 3, 2009 IP
  4. kevin_peters

    kevin_peters Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    AsHinE: Thanks for trying but afraid its still not working for me.

    lmao: Yes, Im trying to get the array to echo onto a page in this structure

    [ ['cat_id_1' , [ ['sub_cat_name_1' , 'sub_cat_id' ] , ['sub_cat_name_2','sub_cat_id'] ] ] ,
    [ ['cat_id_2' , [ ['sub_cat_name_2' , 'sub_cat_id' ] , ['sub_cat_name_2','sub_cat_id'] ] ]
    
    Code (markup):

    So for the above array it would be
    [ ['1' , [ ['Apples'  , '1'  ] , ['Oranges' , '2' ] ] ]   ,  
     [ ['1' , [ ['Bike' , '3' ] , ['Boat','4'] ] ]
    
    Code (markup):

    Still no breakthrough unfortunately :(
     
    Last edited: Oct 3, 2009
    kevin_peters, Oct 3, 2009 IP
  5. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    your this array output does not make sense

    (
    [0] => Array
    (
    [cat_id] => 1
    [sub_cat_name] => Apples
    [sub_cat_id] => 1
    )

    [1] => Array
    (
    [cat_id] => 1
    [sub_cat_name] => Oranges
    [sub_cat_id] => 2
    )

    [3] => Array
    (
    [cat_id] => 2
    [sub_cat_name] => Bike
    [sub_cat_id] => 3
    )

    [4] => Array
    (
    [cat_id] => 2
    [sub_cat_name] => Boat
    [sub_cat_id] => 4
    )




    )
     
    lmao, Oct 3, 2009 IP
  6. kevin_peters

    kevin_peters Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi lmao

    This is the same way ive always taken mysql query results and put into an array:


    
    
    function get_list(){
    	 // query database 
         {
       $conn = db_connect();
       $query = "select cat_id, sub_cat_name, sub_cat_id from listings";
       $result = @$conn->query($query);
       if (!$result)
         return false;
       $result = db_result_to_array($result);
         return $result;
       
    	 }
    
    Code (markup):

    Put into


    function db_result_to_array($result)
    {
       $res_array = array();
    
       for ($count=0; $row = $result->fetch_assoc(); $count++)
         $res_array[$count] = $row;
    
       return $res_array;
    }
    Code (markup):

    Any ideas?

    Cheers,

    Kevin
     
    kevin_peters, Oct 3, 2009 IP
  7. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    your this output is wrong

    it should be

    according to array
     
    lmao, Oct 3, 2009 IP
  8. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i created this code

    in this code frst array $arr has same structure as u printed the array above
     
    lmao, Oct 3, 2009 IP
  9. kevin_peters

    kevin_peters Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yes, sorry my bad... This problem is affecting my brain :)

    Cheers for the code, will try put it in and let you know how I get on. Really appreciate the help:)

    Kevin
     
    kevin_peters, Oct 3, 2009 IP
  10. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    use the code above i created
     
    lmao, Oct 3, 2009 IP
  11. kevin_peters

    kevin_peters Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Your a legend, works great.. Sorry to trouble you for one last question :)

    Just wondering how I would use the mysql query i have

    function get_list(){
    	 // query database 
         {
       $conn = db_connect();
       $query = "select cat_id, sub_cat_name, sub_cat_id from listings";
       $result = @$conn->query($query);
       if (!$result)
         return false;
       $result = db_result_to_array($result);
         return $result;
       
    	 }
    Code (markup):
    to transfer into your code

    $arr[0]=array('cat_id'=>1,'sub_cat_name'=>"Apples","sub_cat_id"=>1); 
    $arr[1]=array('cat_id'=>1,'sub_cat_name'=>"Oranges","sub_cat_id"=>2);
    $arr[3]=array('cat_id'=>2,'sub_cat_name'=>"Bike","sub_cat_id"=>3);
    $arr[4]=array('cat_id'=>2,'sub_cat_name'=>"Boat","sub_cat_id"=>4);
    Code (markup):

    Sorry for asking but so close to finally getting it!

    Cheers again
     
    kevin_peters, Oct 3, 2009 IP
  12. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    you can use like this

     
    lmao, Oct 3, 2009 IP
  13. kevin_peters

    kevin_peters Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Fantastic, thanks very much!!
     
    kevin_peters, Oct 3, 2009 IP