Trouble using merge array in foreach loop

Hi Im struggling with the merge command while looping through image data.

I am trying to extract the ‘tags’ associated with each image (which I have managed - converting from a string into an array) then add all these ‘tags’ together in one ‘big’ array which I want to used to display a list of ALL the tags used.

Here’s what I have.

foreach($all_images as $image) {

	  /// string onto array

			$str = $tags;
			$seperators = array('" "','" ',' "');
			$str = str_replace($seperators,'_',$str);
			$array = explode('_',$str);
			for($i=0;$i<count($array);$i++)
				$array[$i] = str_replace('"','',$array[$i]);
			
			echo '<pre>';
			print_r($array);
			echo '</pre>';
			
			/// add to master array
			$array_big = array_merge ($array_big, $array);
			
	  echo '<br />';
   }
   echo 'Total list!<pre>';
			print_r($array_big);
			echo '</pre>';

Any feedback would be much appreciated!

Where does $tags come from and what does it contain? (example data please)

Also, what exactly is going wrong? :slight_smile:

Sorry.

$tags is output from an api. It is in the form of a string with the elements separated by a space - example below.

green coat snow

The problem is I cant add the tags together into on big array containing all the tags.

Thanks

There’s not enough code there to know what is wrong. For one, you need to have $array_big defined as an array before using it in array_merge. Also what is the point of the foreach when you just do $str=$tags on each iteration.

Ok. I think I need to break it down. Here is were I am. This connects to the API (Slideshow Pro Director API) then requests info for all images. I then loop through each image to display the data.



<?php require_once('api_cnx.php'); ?>

<?php  /// get ALL content images

	echo '<div class="box">All files:<br />';

   $all_images = $director->content->all();

   foreach($all_images as $image) {
      echo  'file url: '.$image->original->url.'<br />';
	  echo  'file size (kb): '.$image->filesize.'<br />';
	  echo  'file ID: '.$image->id.'<br />';
	  echo  'file tags: '.$image->tags.'<br />';
	  echo '<br />';
   }

   echo '</div>';


?>

see here http://bit.ly/bDByPC

What I want to do is add all these tags together in one big list that I can then remove duplicates from. I hope this makes sense!

I don’t know - that was my best guess and why Im asking for help! :slight_smile:

Well foreach makes sense if it’s $images->tags. I don’t know what you’re doing wrong, or even what your error/problem is, but this works, hope it helps


$tags = array('one two three', 'four five six');
$big_array = array(); // needs to be defined
foreach($tags as $t) {
	$array = explode(' ', $t);
	$big_array = array_merge($big_array, $array);
}
/*
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
)
*/

Thanks Hash. I now have one big array!

Just need to remove the empties and duplicates now.

Thanks again!

Hmmm. I thought it would be easy to not add to the array if tags was empty but this doesn’t seem to work - where am i going wrong?

foreach($all_images as $image) {
      echo  'file url: '.$image->original->url.'<br />';
	  echo  'file size (kb): '.$image->filesize.'<br />';
	  echo  'file ID: '.$image->id.'<br />';
	  echo  'file tags: '.$image->tags.'<br />';
	  echo '<br />';
	  
	  	 /// get tags from content
		 $tags = $image->tags;
		 
		 if(!empty($tags))/// check if empty
			{
				foreach($tags as $t) {/// get tags into the all_tags array
				 $array = explode(' ', $t);
				 $all_tags = array_merge($all_tags, $array);
					}
			}
			
				 

   }

Returns : Array (
[0] => green [1] => coat [2] => snow [3] => green [4] => coat [5] => snow [6] => green [7] => coat [8] => snow [9] => green [10] => coat [11] => snow [12] => [13] => [14] => [15] => [16] => [17] => [18] => tayler [19] => pink [20] => stripes
)


$tag_collection = array();

$string = 'foo bar foo  bar      foo   bar chicken soup';

foreach(array_filter(explode(' ', $string)) as $tag){
    if(false === in_array($tag, $tag_collection)){
        array_push($tag_collection, $tag);
    }
}

print_r($tag_collection);

/*
    Array
    (
        [0] => foo
        [1] => bar
        [2] => chicken
        [3] => soup
    )
*/

[fphp]in_array/fphp, [fphp]array_filter/fphp.

:slight_smile:

That kills 2 birds with on stone (once I renamed $tag_collection to $all_tags)

  • Thank you!
false ===

is this checking to see if its empty?

No problem. :slight_smile:

We eliminate all empty array members with array_filter ;). That particular line ensures that the tag in question is not already in our collection, thereby ensuring unique entries.

Brilliant. Thanks for the explanation too.