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
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post

    array manipulation

    I have this routine:

    <?php

    function splitMe2(&$arrs)
    {
    extract($arrs,EXTR_REFS);
    $arrs[0].$arrs[1].$arrs[2];
    }

    $newcart = array(array('Tim', 'Donald', 'Roy'),
    array('John', 'Ronald', 'Troy'),
    array('Mary', 'Kate', 'Martha'));

    echo "Hello, " .array_walk(&$newcart, 'splitMe2'). " welcome to todays class.";
    ?>

    I want it to ouput:
    "Hello, Tim Donald Roy John Ronald Troy Mary Kate Martha welcome to todays class"

    Can anyone help me get the desired results. All help is appreciated.
    I would rather be a lion for a day than a lamb that lives forever.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's an example on how to achieve the result by using array_map() and implode():

    PHP Code:
    function combine($arr) {
        return 
    implode(' '$arr);
    }

    $newcart = array(array('Tim''Donald''Roy'),
    array(
    'John''Ronald''Troy'),
    array(
    'Mary''Kate''Martha'));

    echo 
    "Hello, " implode(' 'array_map('combine'$newcart)). " welcome to todays class."
    De gustibus non est disputandum.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks this worked exactly the way I wanted it to.

    Ok, I have a new issue what if I ony wanted to output array[0] and array[2]?
    To have an output of:

    "Hello, Tim Roy John Troy Mary Martha welcome to todays class"

    Secondly, what if I wanted to Append "Johnson" to everyone's name in the function not in the echo statement, how would I achieve this?

    All help is appreciated.
    Last edited by ecnarongi; 09-24-2004 at 01:41 AM.
    I would rather be a lion for a day than a lamb that lives forever.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Northern NJ
    Posts
    404
    Thanks
    0
    Thanked 1 Time in 1 Post
    I actually answered my own question with

    Code:
       function combine($arr) {
       extract($arr,EXTR_REFS);
       return $arr[0]." Johnson ".$arr[1]." Johnson ".$arr[2]." Johnson";
    }
    
    $newcart = array(array('Tim', 'Donald', 'Roy'),
    array('John', 'Ronald', 'Troy'),
    array('Mary', 'Kate', 'Martha'));
    
    echo "Hello, " . implode(' ', array_map('combine', $newcart)). " welcome to todays class.";
    thanks again
    I would rather be a lion for a day than a lamb that lives forever.


  •  

    Posting Permissions

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