Solved

# change two parameters to one

Posted on 2011-09-19
Medium Priority
227 Views
function weighted_image_array(\$a1Images, \$a2Images, \$num=1)

want
function weighted_image_array(\$AllImages, \$num=1)

and want to call the function

\$AllImages = array(\$a1Images, \$a2Images);

print_r (weighted_image_array(\$AllImages, 6),true);

so the function will have to be changed also because I am getting 'divide by 0' errors
``````<?php
error_reporting(E_ALL);

// SET PERCENTAGES (80% a1 in this case)
\$a1 = 80;
\$a2 = 20;

//a set of images that are used (\$a1/(\$a1+\$a2))% of the time
\$a1Images = array('weight' => \$a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used (\$a2/(\$a1+\$a2))% of the time
\$a2Images = array('weight' => \$a2, 'images' => array('7.jpg', '8.gif', '8.gif'));

echo '<pre>';
// SHOW HOW TO USE THE FUNCTION
print_r(weighted_image_array(\$a1Images, \$a2Images, 10));
echo "</pre>";

// DEFINE THE FUNCTION
function weighted_image_array(\$a1Images, \$a2Images, \$num=1)
{
// UNIQUE NAMES ONLY
\$a1images = array_unique(\$a1Images['images']);
\$a2images = array_unique(\$a2Images['images']);

// # OF IMAGES FROM EACH COLLECTION
\$a1Cnt = round(\$a1Images['weight']/(\$a1Images['weight']+\$a2Images['weight']) * \$num);
\$a2Cnt = \$num - \$a1Cnt;

// GET A RANDOM COLLECTION
\$returnArr = array();

// ADD UP TO \$a1Cnt IMAGES FROM a1 TO THE RETURN ARRAY
shuffle(\$a1images);
\$returnArr = array_merge(\$returnArr, array_slice(\$a1images, 0, \$a1Cnt));

// ADD UP TO \$a2Cnt IMAGES FROM a2 TO THE RETURN ARRAY
shuffle(\$a2images);
\$returnArr = array_merge(\$returnArr, array_slice(\$a2images, 0, \$a2Cnt));

// SHUFFLE THE RESULTANT ARRAY AND RETURN
shuffle(\$returnArr);
return \$returnArr;
}
?>
``````
0
Question by:rgb192
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2

LVL 14

Expert Comment

ID: 36565157
you can use the array_push function to make the single array contaning the values of the both of the array

//a set of images that are used (\$a1/(\$a1+\$a2))% of the time
\$a1Images = array('weight' => \$a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used (\$a2/(\$a1+\$a2))% of the time
\$a2Images = array('weight' => \$a2, 'images' => array('7.jpg', '8.gif', '8.gif'));

\$AllImages = array_push(\$a1Images,\$a2Images);

and than pass the \$AllImages to the function
0

Author Comment

ID: 36565166
but what should change inside the function

because there is no
function weighted_image_array(\$a1Images, \$a2Images, \$num=1)
no \$a1Images no a2Images
0

LVL 14

Expert Comment

ID: 36565211
The updated code will give you the output below....

Array ( [weight] => Array ( [0] => 80 [1] => 20 ) [images] => Array ( [0] => 1.gif [1] => 2.gif [2] => 3.jpg [3] => 4.jpg [4] => 5.gif [5] => 6.jpg [6] => 7.jpg [7] => 8.gif [8] => 8.gif ) )

Now you can manipulate the array output/result with the function
``````// SET PERCENTAGES (80% a1 in this case)
\$a1 = 80;
\$a2 = 20;

//a set of images that are used (\$a1/(\$a1+\$a2))% of the time
\$a1Images = array('weight' => \$a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used (\$a2/(\$a1+\$a2))% of the time
\$a2Images = array('weight' => \$a2, 'images' => array('7.jpg', '8.gif', '8.gif'));

//\$AllImages = array();

\$AllImages = array_merge_recursive(\$a1Images,\$a2Images);
// DEFINE THE FUNCTION
function weighted_image_array(\$AllImages, \$num=1)
{
print_r(\$AllImages);
}
weighted_image_array(\$AllImages, 6);
exit;
``````
0

Author Comment

ID: 36565295
want to chose random images for a banner  with no duplicate images

the higher the weight, the more the set of images will appear.

I do not know how to combine your code with the old code.
0

LVL 7

Expert Comment

ID: 36566418
This should not be a hard change - but how will you list the weighting of each group of images in a single array? do you know what I mean?

like right now you have a1 with a weight of 80% (in the example)
and a2 with a weight of 20%.

If you put a1 and a2 in a single array, how do you define which images have a weight of 80 and which have a weight of 20?
0

Author Comment

ID: 36566806
You are correct.  So more changes need to be made.
0

Author Comment

ID: 36566839
So change inside the function.
0

LVL 7

Accepted Solution

m4trix earned 2000 total points
ID: 36567150
Ok. Here's the modified code. I've made it so you can add any number of image arrays into the "AllImages" array. In this example I've used 3:

``````<?php
error_reporting(E_ALL);

// SET PERCENTAGES (60% a1, 20% a2, 20% a3 in this case)
\$a1 = 60;
\$a2 = 20;
\$a3 = 20;

//a set of images that are used (\$a1/(\$a1+\$a2+\$a3))% of the time
\$a1Images = array('weight' => \$a1, 'images' => array('1.gif', '2.gif', '3.jpg', '4.jpg', '5.gif', '6.jpg'));
//a set of images that are used (\$a2/(\$a1+\$a2+\$a3))% of the time
\$a2Images = array('weight' => \$a2, 'images' => array('7.jpg', '8.gif', '9.gif'));
//a set of images that are used (\$a3/(\$a1+\$a2+\$a3))% of the time
\$a3Images = array('weight' => \$a2, 'images' => array('10.jpg', '11.gif'));

//combine a1 and a2 into a single array:
\$AllImages = array(\$a1Images, \$a2Images, \$a3Images);

echo '<pre>';
// SHOW HOW TO USE THE FUNCTION
print_r(weighted_image_array(\$AllImages, 10));
echo "</pre>";

// DEFINE THE FUNCTION
function weighted_image_array(\$allImages, \$num=1)
{
\$imgData = array('images' => array(), 'imgCnt' => array());
\$weightSum = 0;

// UNIQUE NAMES ONLY
for (\$i=0; \$i<count(\$allImages); \$i++) {
\$imgData['images'][]   = array_unique(\$allImages[\$i]['images']);
\$weightSum += \$allImages[\$i]['weight'];
}

// # OF IMAGES FROM EACH COLLECTION
for (\$i=0; \$i<count(\$allImages); \$i++) {
\$imgData['imgCnt'][] = round(\$allImages[\$i]['weight']/\$weightSum * \$num);
}

// GET A RANDOM COLLECTION
\$returnArr = array();

// ADD UP TO imgCnt IMAGES FROM EACH image ARRAY TO THE RETURN ARRAY
for (\$i=0; \$i<count(\$imgData); \$i++) {
shuffle(\$imgData['images'][\$i]);
\$returnArr = array_merge(\$returnArr, array_slice(\$imgData['images'][\$i], 0, \$imgData['imgCnt'][\$i]));
}

// SHUFFLE THE RESULTANT ARRAY AND RETURN
shuffle(\$returnArr);
return \$returnArr;
}
?>
``````
0

LVL 7

Assisted Solution

m4trix earned 2000 total points
ID: 36567162
``````\$a3Images = array('weight' => \$a3, 'images' => array('10.jpg', '11.gif'));
``````

I copied and pasted from line 12 and forgot to change \$a2 to \$a3 :)
0

Author Comment

ID: 36577972
I selected the wrong answer.  I meant to select m4trix.  I clicked on request support.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and tâ€¦
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will wâ€¦
The viewer will learn how to dynamically set the form action using jQuery.
###### Suggested Courses
Course of the Month8 days, 23 hours left to enroll