populating array3 from two other arrays when count(array1) <count(array2)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • clrockwell

    #1

    populating array3 from two other arrays when count(array1) <count(array2)

    Hey all,

    I could use some help with solving problem. Any suggestions are
    welcome and very appreciated.

    I have two arrays, $feeds and $feedDays. If they both have the same
    count, or $feeds has only one value than there is not a problem.

    In this situation

    $feeds = array([0]=>'feed1', [1]=>'feed2');
    $feedDays = array([0]=>'Monday', [1]=>'Tuesday',[2]=>'Wednesday',
    [3]=>'Thursday',[4]=>'Friday');

    I would like to create a third array:
    $feedDays_feeds = array([0]=>0,[1]=>1,[2]=>0,[3]=>1,[4]=>0)

    I am looking for a solution that can be used with any number of feeds,
    but am having problems coming up with the needed logic. I went
    through php.net and read all the array() functions but couldn't really
    point out a solution. I am going to try and use array_pad(), but I
    figured it best if I tried to get some guidance here.

    Thanks for any suggestions.
  • Michael Fesser

    #2
    Re: populating array3 from two other arrays when count(array1) &lt; count(array2)

    ..oO(clrockwell )
    >I could use some help with solving problem. Any suggestions are
    >welcome and very appreciated.
    >
    >I have two arrays, $feeds and $feedDays. If they both have the same
    >count, or $feeds has only one value than there is not a problem.
    >
    >In this situation
    >
    >$feeds = array([0]=>'feed1', [1]=>'feed2');
    >$feedDays = array([0]=>'Monday', [1]=>'Tuesday',[2]=>'Wednesday',
    >[3]=>'Thursday',[4]=>'Friday');
    >
    >I would like to create a third array:
    >$feedDays_feed s = array([0]=>0,[1]=>1,[2]=>0,[3]=>1,[4]=>0)
    What does this array mean? I can't see how it's related to the two
    above. The keys seem to be the keys taken from the $feedDays array,
    but what do the values mean?

    Micha

    Comment

    • clrockwell

      #3
      Re: populating array3 from two other arrays when count(array1) &lt;count(array 2)

      On Jul 23, 11:09 am, Michael Fesser <neti...@gmx.de wrote:
      .oO(clrockwell)
      >
      I could use some help with solving problem. Any suggestions are
      welcome and very appreciated.
      >
      I have two arrays, $feeds and $feedDays. If they both have the same
      count, or $feeds has only one value than there is not a problem.
      >
      In this situation
      >
      $feeds = array([0]=>'feed1', [1]=>'feed2');
      $feedDays = array([0]=>'Monday', [1]=>'Tuesday',[2]=>'Wednesday',
      [3]=>'Thursday',[4]=>'Friday');
      >
      I would like to create a third array:
      $feedDays_feeds = array([0]=>0,[1]=>1,[2]=>0,[3]=>1,[4]=>0)
      >
      What does this array mean? I can't see how it's related to the two
      above. The keys seem to be the keys taken from the $feedDays array,
      but what do the values mean?
      >
      Micha
      Micha,

      I have two tables, 'feeds' and 'feedDays'
      Feeds has columns 'id', 'url',
      FeedDays has columns 'id', 'day', 'feed_id'

      Every Sunday, via cron job, FeedDays get truncated and repopulated by
      picking the last value in a shuffled array that is populated by feeds
      table. I then pop the feeds array and take the last value for the
      next day, and repeat. With 5 or more feeds, no issues.

      I guess it is better shown like this:
      array(
      'monday' ='feed1',
      'tuesday' ='feed2',
      'wednesday' ='feed3',
      'thursday' ='feed4',
      'friday' ='feed5');

      The next week could yield
      array(
      'monday' ='feed5',
      'tuesday' ='feed4',
      'wednesday' ='feed3',
      'thursday' ='feed2',
      'friday' ='feed1');

      But if there is only 4 feeds, using array_pop() will end up with no
      more values before all the feedDays have a feed. I hope this shows
      how I will use it.

      Comment

      • petersprc

        #4
        Re: populating array3 from two other arrays when count(array1) &lt;count(array 2)

        Hi,

        You can use modulo arithmetic to map each element in a to the next
        element in the circular array b:

        $a = array('mon', 'tue', 'wed', 'thu', 'fri');
        $b = array('feed1', 'feed2', 'feed3');
        $c = array();

        for ($i = 0; $i < count($a); $i++) {
        $c[$i] = $b[$i % count($b)];
        }

        Regards,

        John Peters

        On Jul 23, 9:48 am, clrockwell <clrockw...@tru ckingshow.comwr ote:
        Hey all,
        >
        I could use some help with solving problem. Any suggestions are
        welcome and very appreciated.
        >
        I have two arrays, $feeds and $feedDays. If they both have the same
        count, or $feeds has only one value than there is not a problem.
        >
        In this situation
        >
        $feeds = array([0]=>'feed1', [1]=>'feed2');
        $feedDays = array([0]=>'Monday', [1]=>'Tuesday',[2]=>'Wednesday',
        [3]=>'Thursday',[4]=>'Friday');
        >
        I would like to create a third array:
        $feedDays_feeds = array([0]=>0,[1]=>1,[2]=>0,[3]=>1,[4]=>0)
        >
        I am looking for a solution that can be used with any number of feeds,
        but am having problems coming up with the needed logic. I went
        through php.net and read all the array() functions but couldn't really
        point out a solution. I am going to try and use array_pad(), but I
        figured it best if I tried to get some guidance here.
        >
        Thanks for any suggestions.

        Comment

        • clrockwell

          #5
          Re: populating array3 from two other arrays when count(array1) &lt;count(array 2)

          On Jul 23, 3:50 pm, petersprc <peters...@gmai l.comwrote:
          Hi,
          >
          You can use modulo arithmetic to map each element in a to the next
          element in the circular array b:
          >
          $a = array('mon', 'tue', 'wed', 'thu', 'fri');
          $b = array('feed1', 'feed2', 'feed3');
          $c = array();
          >
          for ($i = 0; $i < count($a); $i++) {
          $c[$i] = $b[$i % count($b)];
          }
          >
          Regards,
          >
          John Peters
          >
          On Jul 23, 9:48 am, clrockwell <clrockw...@tru ckingshow.comwr ote:
          >
          Hey all,
          >
          I could use some help with solving problem. Any suggestions are
          welcome and very appreciated.
          >
          I have two arrays, $feeds and $feedDays. If they both have the same
          count, or $feeds has only one value than there is not a problem.
          >
          In this situation
          >
          $feeds = array([0]=>'feed1', [1]=>'feed2');
          $feedDays = array([0]=>'Monday', [1]=>'Tuesday',[2]=>'Wednesday',
          [3]=>'Thursday',[4]=>'Friday');
          >
          I would like to create a third array:
          $feedDays_feeds = array([0]=>0,[1]=>1,[2]=>0,[3]=>1,[4]=>0)
          >
          I am looking for a solution that can be used with any number of feeds,
          but am having problems coming up with the needed logic. I went
          through php.net and read all the array() functions but couldn't really
          point out a solution. I am going to try and use array_pad(), but I
          figured it best if I tried to get some guidance here.
          >
          Thanks for any suggestions.
          That worked great John, thanks for the tip. I have yet to use modulo
          arithmetic so I looked into it. I could not find anything that stated
          that this process does in fact round the number up or down. This is
          not a problem in my situation, but I still would like to know whether
          it is built into php or is based on the system php sits on.

          i.e. 4/3 = 1.333; 5/3 = 1.666

          Thanks again for the tip!

          Comment

          Working...