Jump to content

Archived

This topic is now archived and is closed to further replies.

peaforabrain

merge two arrays

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.

Share this post


Link to post
Share on other sites
Not totally sure what you want to achive....

The 2nd array seems to have all teh info you need - merging it with an array that simply has a list of values that are the same as the keys of array 2 seems pointless.

Share this post


Link to post
Share on other sites
read up on arrays on http://php.net/arrays

look at array_diff and array_merge.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[code=php:0]foreach($array as $key => $val) {
    echo "<option $sel value=\"".htmlentities($val)."\">".htmlentities($key).'. '.htmlentities($val)."</option>";
}[/code]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Which 'number'? The index is specified by the $key variable, do you mean something else, like item ID or similar?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
$a is an array, where is this declared and defined? (i.e. can you post the loop that creates this array)

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Well you've got your $result array returned by the function (which you've stowed away in var $a)

so with $a, assign to the template.

the template then iterates and decorates the data, ala my previous example where I assumed Smarty.

Share this post


Link to post
Share on other sites

×

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.