Results 1 to 9 of 9

Thread: how to loop this echo

  1. #1
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default how to loop this echo

    I have an array that is being populated into a multidimensional array and I ahve spent hours trying to figure out how to echo the array. I have tried many foreach things and for() also but with no luck.
    Here is what the array looks like -
    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    brand_id] => Array
                    (
                        [
    0] => 2
                        
    [1] => 35
                        
    [2] => 5
                        
    [3] => 6
                        
    [4] => 32
                        
    [5] => 7
                        
    [6] => 8
                        
    [7] => 37
                        
    [8] => 36
                        
    [9] => 38
                        
    [10] => 40
                        
    [11] => 39
                        
    [12] => 12
                        
    [13] => 14
                        
    [14] => 16
                        
    [15] => 17
                        
    [16] => 18
                        
    [17] => 33
                        
    [18] => 34
                        
    [19] => 21
                        
    [20] => 27
                        
    [21] => 22
                        
    [22] => 23
                        
    [23] => 24
                        
    [24] => 25
                        
    [25] => 26
                        
    [26] => 28
                    
    )

                [
    brand_name] => Array
                    (
                        [
    0] => Black Ice Wheels
                        
    [1] => Bonetti Wheels
                        
    [2] => **** Cepek Wheels
                        
    [3] => Dolce Wheels
                        
    [4] => Effen Wheels
                        
    [5] => Focal Wheels
                        
    [6] => Forza Wheels
                        
    [7] => G1R Wheels
                        
    [8] => Gino Wheels
                        
    [9] => II Crave
                        
    [10] => II Crave Black Diamond
                        
    [11] => II Crave Offroad
                        
    [12] => Katana Wheels
                        
    [13] => Mamba Wheels
                        
    [14] => Milanni Wheels
                        
    [15] => Ninja Wheels
                        
    [16] => Platinum Wheels
                        
    [17] => Rockstar Wheels
                        
    [18] => Starr Wheels
                        
    [19] => Ultra Wheels
                        
    [20] => V-Tec Wheels
                        
    [21] => V360 Wheels
                        
    [22] => Verde Wheels
                        
    [23] => Versante Wheels
                        
    [24] => Vision Wheels
                        
    [25] => VRock Wheels
                        
    [26] => Wheel Replicas Wheels
                    
    )

                [
    brand_logo] => Array
                    (
                        [
    0] => http://wheelsdirectmn.com/images/manu-logos/black-ice-logo.jpg
                        
    [1] => http://wheelsdirectmn.com/images/manu-logos/bonetti-logo.jpg
                        
    [2] => http://wheelsdirectmn.com/images/manu-logos/****-cepek-logo.jpg
                        
    [3] => http://wheelsdirectmn.com/images/manu-logos/dolce-logo.jpg
                        
    [4] => http://wheelsdirectmn.com/images/manu-logos/effen-logo.jpg
                        
    [5] => http://wheelsdirectmn.com/images/manu-logos/focal-logo.jpg
                        
    [6] => http://wheelsdirectmn.com/images/manu-logos/forza-logo.jpg
                        
    [7] => http://wheelsdirectmn.com/images/manu-logos/g1r-logo.jpg
                        
    [8] => http://wheelsdirectmn.com/images/manu-logos/gino-logo.jpg
                        
    [9] => http://wheelsdirectmn.com/images/manu-logos/ii-crave-logo.jpg
                        
    [10] => http://wheelsdirectmn.com/images/manu-logos/ii-crave-bd-logo.jpg
                        
    [11] => http://wheelsdirectmn.com/images/manu-logos/ii-crave-offroad-logo.jpg
                        
    [12] => http://wheelsdirectmn.com/images/manu-logos/katana-logo.jpg
                        
    [13] => http://wheelsdirectmn.com/images/manu-logos/mamba-logo.jpg
                        
    [14] => http://wheelsdirectmn.com/images/manu-logos/milanni-logo.jpg
                        
    [15] => http://wheelsdirectmn.com/images/manu-logos/ninja-logo.jpg
                        
    [16] => http://wheelsdirectmn.com/images/manu-logos/platinum-logo.jpg
                        
    [17] => http://wheelsdirectmn.com/images/manu-logos/rockstar-logo.jpg
                        
    [18] => http://wheelsdirectmn.com/images/manu-logos/starr-logo.jpg
                        
    [19] => http://wheelsdirectmn.com/images/manu-logos/ultra-logo.jpg
                        
    [20] => http://wheelsdirectmn.com/images/manu-logos/v-tec-logo.jpg
                        
    [21] => http://wheelsdirectmn.com/images/manu-logos/v360-logo.jpg
                        
    [22] => http://wheelsdirectmn.com/images/manu-logos/verde-logo.jpg
                        
    [23] => http://wheelsdirectmn.com/images/manu-logos/versante-logo.jpg
                        
    [24] => http://wheelsdirectmn.com/images/manu-logos/vision-logo.jpg
                        
    [25] => http://wheelsdirectmn.com/images/manu-logos/vrock-logo.jpg
                        
    [26] => http://wheelsdirectmn.com/images/manu-logos/wheel-replicas-logo.jpg
                    
    )

            )


    I seem to think it needs to be done with more than on loop but not sure how.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    First, you can't echo an array. You can echo the parts of an array (if they are strings, not if they are arrays themselves).

    If you want to display an array, use print_r(). But it looks like you have already done that to post the code above. That's not PHP code. That's output from print_r() or a similar function. Is that not what you want?

    How do you want to display it?

    The basic problem is that you need a dynamic number of loops. For this, it's best to use a recursive function: a function that calls itself when needed, as many times as needed.

    Something like this:
    PHP Code:
    function echo_array($ar) {
       foreach(
    $ar as $a) {
          if (
    is_array($a)) { echo_array($a); } //recursive loop
          
    else { echo $a."<br />\n"; } //echo the string along with a line break
       
    }
    }
    echo_array($my_array); //execute 
    Note that the function only deals with strings and arrays inside the array. If there are other types PHP may try to error correct (numbers for example) or may have trouble, such as with objects.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Yes this is the output from the print_r(). I am getting this array from a json_decode for an API I am trying to make. What i need is for it basically to output like I was getting the info from a query and using a while loop, so the corresponding keys of each array go with each other (ie. [brand_id][0] goes with [brand_name][0] and so on).

    I really don't want to use it to work the way it's coming across now but it seems as thought the only thing json_decode outputs is arrays. On the responding side of the API I have the functions just echo out how and what I want it to display but then it doesn't come across the json cause it's not returned as an array but rather an echo.

    Is there a way around this? I will be happy to post all the code for the api, it's not that much, if anyone can help more with understanding a json.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Does the function I posted above work? It should be fine.

    You can add the keys to it like this:
    PHP Code:
    function echo_array($ar) {
       foreach(
    $ar as $key=>$a) {
          if (
    is_array($a)) { echo_array($a); } //recursive loop
          
    else { echo $key.': '.$a."<br />\n"; } //echo the string along with a line break
       
    }
    }
    echo_array($my_array); //execute 
    All you need to change is the echo line to have it work as you'd like. That will be like a "while" loop for a database query, more or less.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you might try something like this:
    PHP Code:
    $A /* the immediate parent of the arrays you want */;
    /* in your first post, it would be Array[0] */

    // count the entries in one (could be any, but using brand_id)
    $c count($A['brand_id']);
    // loop through $c times
    for($x=0$x<$c$x++){
       
    // and build a new array containing the matching ordinal entry from each child array
       
    $brand[] = array(
          
    'id' => $A['brand_id'][$x]
         ,
    'name' => $A['brand_name'][$x]
         ,
    'logo' => $A['brand_logo'][$x]
       );
    }

    foreach(
    $brand as $b){
       
    // you can echo it like this
       
    print 'id: '.$b['id'].', name: '.$b['name'].', logo: '.$b['logo'].'<br>';


  6. #6
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    It does as it should based on what you know of my issue. I have changed the array to what i think is more appropriate but the code you gave I can't figure out how to make it do the final product I need.

    Here is the new array -
    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    0] => Array
                    (
                        [
    0] => 391
                        
    [1] => Agressor
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Aggressor.jpg
                    
    )

                [
    1] => Array
                    (
                        [
    0] => 392
                        
    [1] => Bullet
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Bullet.jpg
                    
    )

                [
    2] => Array
                    (
                        [
    0] => 393
                        
    [1] => Driver
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Driver.jpg
                    
    )

                [
    3] => Array
                    (
                        [
    0] => 394
                        
    [1] => Edge
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Edge.jpg
                    
    )

                [
    4] => Array
                    (
                        [
    0] => 395
                        
    [1] => Epic
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Epic.jpg
                    
    )

                [
    5] => Array
                    (
                        [
    0] => 396
                        
    [1] => Hazard
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Hazard.jpg
                    
    )

                [
    6] => Array
                    (
                        [
    0] => 398
                        
    [1] => Illusion Black
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Illusion.jpg
                    
    )

                [
    7] => Array
                    (
                        [
    0] => 397
                        
    [1] => Illusion Chrome
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Illusion-(chrome).jpg
                    
    )

                [
    8] => Array
                    (
                        [
    0] => 399
                        
    [1] => Marshall
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Marshall.jpg
                    
    )

                [
    9] => Array
                    (
                        [
    0] => 400
                        
    [1] => Platinum
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Platinum.jpg
                    
    )

                [
    10] => Array
                    (
                        [
    0] => 401
                        
    [1] => Prestige
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Prestige.jpg
                    
    )

                [
    11] => Array
                    (
                        [
    0] => 402
                        
    [1] => Renegade
                        
    [2] => http://wheelsdirectmn.com/images/wheels/fairway/Renegade.jpg
                    
    )

            )


    What I need to do is something like this -
    Code:
    <a href=\"wheel-info.php?wheel_id=$a[0]\"><img src=\"$a[2]\" alt=\"$a[1]\"/></a><br/>
    I don't understand how to create a code block like that from a foreach loop when I need all 3 values from the array at one time.

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I'm not sure why you're nesting the arrays so deep - does the top-level array hold anything besides the [0] array? I'd get rid of it, if you don't need it. but here:
    PHP Code:
    $A $array[0]; // get the immediate parent array

    // then do like you were
    foreach($A as $a){
       print 
    '<a href="wheel-info.php?wheel_id='.$a[0].'"><img src="'.$a[2].'" alt="'.$a[1].'"/></a><br/>';

    Edit:

    can you post your actual array (not the print_r() version), either the php code or the json code? that would make it much easier to create a working example.


  8. #8
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Traq your previous post was perfect, you are a genius. Thank you so much, I have been fighting this for hours and hours. This is what I ended up with and I also changed the array back so that it wasn't so nested. I tried nesting it that way to kind of get each set of values together but that didn't make it easier anyway.
    PHP Code:
    $get wheelsdirect::wdmn_wheels($_GET['brand_id']);

    // count the entries in one (could be any, but using brand_id)
    $c count($get[0]['wheel_id']);
    echo 
    $c;
    // loop through $c times
    for($x=0$x<$c$x++){
       
    // and build a new array containing the matching ordinal entry from each child array
       
    $brand[] = array(
          
    'id' => $get[0]['wheel_id'][$x]
         ,
    'name' => $get[0]['wheel_name'][$x]
         ,
    'logo' => $get[0]['wheel_image'][$x]
         ,
    'pnum' => $get[0]['wheel_part_num'][$x]
       );
    }

    foreach(
    $brand as $b){
       
    // you can echo it like this
       
    echo "<a href=\"wheel-info.php?wheel_id=$w_id&amp;prod_num={$b[pnum]}\"><img src=\"{$b[logo]}\" alt=\"{$b[name]}\"/></a><br/><a href=\"wheel-info.php?wheel_id=$w_id&amp;prod_num={$b[pnum]}\">{$b[name]}</a><br/>";


  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

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
  •