SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: array question

  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array question

    Hello

    I have a clients.txt file containing this

    name:surname
    name2:surname2
    name3:surname3

    then I have this code

    PHP Code:
    $filetrue=file("/clients.txt");
    $newtrue=array_unique($filetrue);
    foreach(
    $newtrue as $row){
    $rowar=explode (":",$row);
    $clientname[]=$rowar[0];
    $clientsurname[]=$rowar[1];


    It creates 2 arrays $clientname and $clientsurname .
    How to create a single array which contains name and surname , so that
    I can echo all the list using a foreach ?

    Thanks !

  2. #2
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can make array with two dimention,
    try to use this code

    Code:
    $filetrue=file("/clients.txt"); 
    $newtrue=array_unique($filetrue); 
    foreach($newtrue as $row){ 
        $rowar=explode (":",$row); 
        $client['name'][]=$rowar[0]; 
        $client['surname'][]=$rowar[1]; 
    } 
    print_r($client);
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you bounded to use foreach? Why don't you echo like this?
    PHP Code:
    if(count($clientname) == count($clientsurname)){
    for(
    $i 0$i count($clientname);$i++){
    echo 
    "Name=" $clientname[$i] . " Surname=" $clientsurname[$i];
    }

    Does this work?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by graziano68 View Post
    Hello

    I have a clients.txt file containing this

    name:surname
    name2:surname2
    name3:surname3

    then I have this code

    PHP Code:
    $filetrue=file("/clients.txt");
    $newtrue=array_unique($filetrue);
    foreach(
    $newtrue as $row){
    $rowar=explode (":",$row);
    $clientname[]=$rowar[0];
    $clientsurname[]=$rowar[1];


    It creates 2 arrays $clientname and $clientsurname .
    How to create a single array which contains name and surname , so that
    I can echo all the list using a foreach ?

    Thanks !

    Thank you , but if I need to put client and name on a table , how to echo for example the name on array 0 and the surname on the array 0 , using this bidimensional array ?


    Thanks

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Are you bounded to use foreach? Why don't you echo like this?
    PHP Code:
    if(count($clientname) == count($clientsurname)){
    for(
    $i 0$i count($clientname);$i++){
    echo 
    "Name=" $clientname[$i] . " Surname=" $clientsurname[$i];
    }

    Does this work?

    Works really fine , good idea (without using bid dimensional array)

    Thanks


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
  •