Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Arrays - making one big array from 4

    Hi all

    I have 4 arrays:

    PHP Code:
    $link 
    $title 
    $description 
    $pubDate 
    They each contain 10 items and as their names woudl suggest, contain links, titles descriptions and published dates.

    Each of the four arrays contain information relating to the other,

    I.e.

    $link[0] is the link for $title[0] which is the title for $description[0] which is the description for $pubDate[0]
    and
    $link[1] is the link for $title[1] which is the title for $description[1] which is the description for $pubDate[1]

    and so on....


    Now, I want to put these into a MySQL table so that each row in the MySQL table contains the correct information for each item

    I know that if I only had one array (e.g. $link) i would use

    PHP Code:
    foreach( $link as $item )
    {
    /* ... MySQL INSERT command     */

    BUT, how would I go about dealing with the 4 arrays?

    I'm guessing I need to make a huge array of the type:

    PHP Code:
    $each_item = array(=>$link[0], $title[0], $description[0], $pubDate[0]); 
    and then i have one array which i can loop through and insert into the MySQL table

    I've looked at the combine_array function - I do not think this is what I require here...

    Your comments will be gratefully receievd

    Many thanks

    k
    Last edited by theside; 11-13-2010 at 10:36 PM. Reason: missed a ; !

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    foreach( $link as $key => $_link ) {
       echo 
    $title[$key] . $_link $description[$key];
    }

    or

    for(
    $x=0$x count($link); $x++){
        echo 
    $title[$x] . $link[$x] . $description[$x];

    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    theside (11-13-2010)

  • #3
    New Coder
    Join Date
    Oct 2010
    Location
    United Kingdom
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Phil - THANK YOU very much!

    I was getting there slowly, but wouldn't have made it tonight that's for sure!

    I used your second version as I understand that more so that the first, but I'll study both!

    I also ran your function in your signature - very cool!

    Kind regards

    K

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    the first example is as follows

    array('foo','bar')

    by default, the keys of the array would be 0,1.
    [0] => foo, [1] => bar

    using foreach() and declairing the key within a variable will give you the same value as using $x in the second example.

    no worries anyway, and I like my signature too, had fun writing it!
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 


  •  

    Posting Permissions

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