Jump to content

merge two arrays


peaforabrain

Recommended Posts

Hi,

I wonder if anyone could help me with this;

I have two arrays, Array 1 is a list from A to Z, Array 2 is list of elements that contain data for a specific letter with a number indicating the amount of. So if 'C' for instance has two amounts of data and 'E' has one, then the list will show;

            A
            B
            C (2)
            D
            E (1)
            etc.

I cannot get this to work correctly, please someone help.
Link to comment
Share on other sites

Thanks for the reply, but I have read a lot about arrays, and have tried various things.

[quote]          $result = array_merge($arr1, $arr2);
  $f = array_unique($result);
  while (list($indexValue, $fi) = each ($f))
  echo"<option $sel value=\"".$fi."\">".$fi."";[/quote]

This stops the duplication of elemnts, but I need to put the number next to the appropriate listing.
Link to comment
Share on other sites

Thanks Jenk,

Here is where I am up to.

[code=php:0] $f = htmlspecialchars($f);
$arr1[]= "$f";
    }
}
    {
if (!empty($a))
          while(list(,$OneCount)=each($a))  
        {
      $count=$OneCount['name'];
      $num=$OneCount['num'];
$arr2[]="$count";
$arr3[]="$num";
    }
{  
          $result = array_merge($arr1, $arr2);
  $f = array_unique($result);
  while (list($indexValue, $fi) = each ($f))
foreach($fi as $key => $val)
{
    echo "<option $sel value=\"".htmlentities($val)."\">".htmlentities($key).'. '.htmlentities($val)."";
}[/code]

Could you point me in the direction of getting the number into the array?
Thanks again.
Link to comment
Share on other sites

Thanks, sorry for not being specific enough.

The number I refer to is the amount of elements that are associated to a given field as extracted from the mysql db
[quote]       $count=$OneCount['name'];
      $num=$OneCount['num'];
$arr2[]="$count";
$arr3[]="$num";[/quote]

I havn't really shown it in this script as I could not get it to work, but I am presuming that making the number "$arr3" loop, then it should work.
Link to comment
Share on other sites

ok, well it looks like you are going majorly overkill with your arrays then.

Instead of creating so many arrays and doing all the shennanigans with them, just extract from the DB what you need - the image source link and the text by the looks of it.

Then assign that directly to your template engine, then have the template iterate over the values outputting the HTML.
Link to comment
Share on other sites

I know what you mean, and it is a lot easier to just extract from one list as mentioned by toonmariner.

But I have a special need for this particular function and I actually do have it working (after a fashion because its random in its operation, not displaying the numbers as it should) using a lot of if else commands.

If you can supply me with any poiters that would be great.

thanks
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.