SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to convert this array to string

    PHP Code:
    array(3) {
    [
    0]=> array(1) {
    [
    "Event"]=> array(5) {
    [
    "id"]=> string(3"102"
    ["headline"]=> string(31"Concert in the Park"
    ["message"]=> string(44"The Band will be playing in the park on Saturday night"
    ["close_date"]=> string(10"2012-06-17"
    ["start_date"]=> string(10"2012-06-16"
    } }

    [
    1]=> array(1) {
    [
    "Event"]=> array(5) {
    [
    "id"]=> string(3"102"
    ["headline"]=> string(31"Comedy Night"
    ["message"]=> string(44"Friday night comedy at Downtown Club"
    ["close_date"]=> string(10"2012-06-15"
    ["start_date"]=> string(10"2012-06-15"
    } }

    [
    2]=> array(1) {
    [
    "Event"]=> array(5) {
    [
    "id"]=> string(3"102"
    ["headline"]=> string(31"Fireworks Show"
    ["message"]=> string(44"There will be a fireworks show after the baseball game"
    ["close_date"]=> string(10"2012-07-4"
    ["start_date"]=> string(10"2012-07-4"
    } }

    I need $event['Event']['message'] from each array above into a single string, $my_events
    Any assistance is appreciated. I've seen implode as well as foreach ($array as $key->$value) but don't understand those implementations.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are many ways, but the simplest to understand is a foreach combined with an implode. Don't worry, I'll show you how it works.

    PHP Code:
    <?php
    $eventMessages 
    = array();
    foreach(
    $event as $eventItem){
        
    $eventMessages[] = $eventItem['Event']['message'];
    }
    $my_events implode(', '$eventMessages);
    The foreach() loops through the items in $event. So $event is an array with 3 items in; The loop sets $eventItem to each successive item every time it runs (which is equal to how many items are in the $event array). All I've done above is built up the $eventMessages array from this loop - each time the loop progresses, it adds the next $event's message.

    The implode() then glues all of the $eventMessages together with a comma and space inbetween each.

    The foreach, if you're not familiar with it, is not a scary thing. The above code is the same as below when there are 3 items in the array:
    PHP Code:
    $eventMessages = array();
    $eventItem $events[0];
    $eventMessages[] = $eventItem['Event']['message'];
    $eventItem $events[1];
    $eventMessages[] = $eventItem['Event']['message'];
    $eventItem $events[2];
    $eventMessages[] = $eventItem['Event']['message'];
    $my_events implode(', '$eventMessages); 
    Of course when there are 4 items, it runs 4 times instead of 3 and so on.

    Hope that helps.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes! That's super helpful. Thank you very much.


Tags for this Thread

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
  •