SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    implode multidimensional array

    Hi Guys

    I need to implode a multidimensional array. The require output is the value separated by commas. For example, I have the following array:

    Code:
    Array
    (
        [0] => Array
            (
                [job_type] => Administrative / Clerical Jobs
            )
    
        [1] => Array
            (
                [job_type] => Accounting / Finance jobs
            )
    
        [2] => Array
            (
                [job_type] => Architecture Jobs
            )
    
        [3] => Array
            (
                [job_type] => Art and Design Jobs
            )
    
    )
    I would like to show the job_type value separated by a comma.

    I.e.

    Administrative / Clerical Jobs, Accounting / Finance jobs, Architecture Jobs, Art and Design Jobs

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is possibly an amateurish solution, but it does what you want

    PHP Code:
    <?php     

    $array 
    = array(array('job_type'=>'Administrative / Clerical Jobs'),
            array(
    'job_type' => 'Accounting / Finance jobs'),
            array(
    'job_type' => 'Architecture Jobs'),
            array(
    'job_type' => 'Art and Design Jobs'));

    echo 
    '<pre>';
    print_r($array);
    echo 
    '</pre>';

    foreach(
    $array as $val){
        if(
    is_array($val)){
           
    $t .= $val['job_type'].',';        
        }
    }
    print 
    substr $t strlen($t) -);
      
    ?>
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but was looking for something a little cleaner :-) Any ideas how to do it using array_map?

  4. #4
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array_map returns another array - so that won't put you any further forward.

    I don't know (yet) if there's anything cleaner than my first effort??
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't using array_map present this
    PHP Code:
    function GetJobType($e)
    {
        return 
    $e['job_type'];
    }

    echo 
    implode(', 'array_map('GetJobType'$array)); 

  6. #6
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that works, but is it cleaner? Not sure what people mean when they say 'cleaner' :0(
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  7. #7
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Why implode? Why not serialize or json_encode? Implode isn't the only way to get a string representation of an array that can be converted back to an array.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    PHP 5.5.0 might get a new function, array_column(), that would make life easier here:

    echo implode(', ', array_column($array, 'job_type'));



    Quote Originally Posted by Michael Morris View Post
    Implode isn't the only way to get a string representation of an array that can be converted back to an array.
    Who said anything about converting the string back to an array?
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,026
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Off Topic:

    PHP 5.5.0 might get a new function, array_column(), that would make life easier here:

    echo implode(', ', array_column($array, 'job_type'));

    Off Topic:

    Yes, indeed, though he might be simplifying the example some here. In any event, if those strings contain commas (and they're free form it seems so they could) implode isn't the right tool, even with array_column




    Who said anything about converting the string back to an array?
    That more often going to be the case than not. He hasn't mentioned why he's doing this, so I have no point of reference here.

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Off Topic:

    Yes, indeed, though he might be simplifying the example some here. In any event, if those strings contain commas (and they're free form it seems so they could) implode isn't the right tool, even with array_column


    That more often going to be the case than not. He hasn't mentioned why he's doing this, so I have no point of reference here.
    array_column doesn't provide a comma delimited output either, so at best you could replace array_map with array_column, but you can't remove implode...unless I am missing something.

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    That more often going to be the case than not. He hasn't mentioned why he's doing this, so I have no point of reference here.
    My take was that he just wants to print the strings separated by some commas. As you say, he hasn't mentioned why he's doing this so why second guess? At the very least, let's ask Zaggs about his other requirements, if any, before jumping onto the this needs to be reversible, or not-only-comma separated, horse!

    Quote Originally Posted by cpradio View Post
    array_column doesn't provide a comma delimited output either, so at best you could replace array_map with array_column, but you can't remove implode...unless I am missing something.
    Who said anything about removing array_column()? (I'm seeing a pattern emerging! )
    Salathe
    Software Developer and PHP Manual Author.

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Who said anything about removing array_column()? (I'm seeing a pattern emerging! )
    On the contrary, I like array_column, that is what I was originally searching for when I looked through the php docs to begin with, but when I didn't find it, I resorted to array_map. What I meant to imply is you can use array_column and array_map interchangeably in this endeavor, but implode() to my knowledge is the only way to take an array and turn it into a delimited string (apart from coding a loop).

    Off Topic:

    It's been a long day, my brain has started to shut down...


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
  •