Jump to content


Photo

merge two arrays


  • Please log in to reply
12 replies to this topic

#1 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 08:57 AM

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.

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 04 September 2006 - 09:00 AM

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.
follow me on twitter @PHPsycho

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 09:07 AM

read up on arrays on http://php.net/arrays

look at array_diff and array_merge.

#4 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 09:23 AM

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

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


This stops the duplication of elemnts, but I need to put the number next to the appropriate listing.

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 09:28 AM

foreach($array as $key => $val) {
    echo "<option $sel value=\"".htmlentities($val)."\">".htmlentities($key).'. '.htmlentities($val)."</option>";
}


#6 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 09:52 AM

Thanks Jenk,

Here is where I am up to.

$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)."";
}

Could you point me in the direction of getting the number into the array?
Thanks again.


#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 10:12 AM

Which 'number'? The index is specified by the $key variable, do you mean something else, like item ID or similar?

#8 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 10:19 AM

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

      $count=$OneCount['name'];
      $num=$OneCount['num'];
$arr2[]="$count";
$arr3[]="$num";


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.

#9 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 10:34 AM

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

#10 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 10:40 AM

$a is defined from a function;

[

The other array is gathered from this;


#11 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 11:41 AM

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.

#12 peaforabrain

peaforabrain
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 September 2006 - 11:55 AM

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

#13 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 12:05 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users