SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Mar 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Mind Block With Arrays

    Ok guys have got a mind block, lost it completley and wonder if anyone out there go throw me a line. So here goes.

    I have got an array of information like this

    PHP Code:
        20-10-2009 00:00:00|1
        21
    -10-2009 00:00:00|1
        23
    -10-2009 00:00:00|1
        24
    -10-2009 00:00:00|4
        25
    -10-2009 00:00:00|
    What I am doing is using the explode to seperate the pipe delimited values whilst looping through the array

    PHP Code:
       $split explode("|"$dates);
       
    $id$split[1];
       
    $date $split[0]; 
    Ok Here is where either I am complltey over complicating things here or have just overlooked it.

    What I want to achieve is to have an array for each $id with the $date(s) for that $id in their own arrays which will intail reside in if you like a master array.

    Can anyone help. What I want to end up with is something like this:

    PHP Code:

    array(
      [
    1] => 
           array([
    0] = > "20-10-2009",  [1] => "21-10-2009", [2] => "23-10-2009")
      [
    4] =>
          array([
    0] => " 24-10-2009", [1] => "25-10-2009")

    The master array i want to refrence by the $id and its array values to be the dates.

    I hope it make sense.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($datesarray as $dates) {
      
    $split explode("|"$dates);
      
    $id $split[1];
      
    $date $split[0];

      
    $newarray[$id][] = substr($date0strpos($date' '));


  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Like this?

    PHP Code:
    <?php
    $aSource 
    = array(
        
    '20-10-2009 00:00:00|1',
        
    '21-10-2009 00:00:00|1',
        
    '23-10-2009 00:00:00|1',
        
    '24-10-2009 00:00:00|4',
        
    '25-10-2009 00:00:00|4'
    );

    foreach(
    $aSource as $sEntry)
    {
        
    $aEntry explode('|'$sEntry);
        
    $aFinal[$aEntry[1]][] = current(explode(' '$aEntry[0]));
    }

    print_r($aFinal);
    /*
    Array
    (
        [1] => Array
            (
                [0] => 20-10-2009
                [1] => 21-10-2009
                [2] => 23-10-2009
            )

        [4] => Array
            (
                [0] => 24-10-2009
                [1] => 25-10-2009
            )
    )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    You could this with a SQL GROUP_CONCAT() so that your don't need to worry about arranging the columns.

    table
    - id
    - created

    SELECT id,GROUP_CONCAT(created) as created FROM table GROUP by id

    Then run a loop and impode each created index by ',';

    If your using PDO you could also group the information after your receive the result from mysql using your previous method.

    Code:
    <?php
    $sql = 'SELECT id,created FROM table';
    if($stmt = $db->prepare($sql)) {
      if($stmt->execute()) {
         $result = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
      }
    }
    ?>
    Example 3

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    He has a pipe delimited set of strings, not a database with id and date column, so he can't query it


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
  •