Solved

change two parameters to one

Posted on 2011-09-19
11
226 Views
Last Modified: 2012-05-12
instead of the function
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;
}
?>

Open in new window

0
Comment
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
  • Learn & ask questions
  • 5
  • 3
  • 2
11 Comments
 
LVL 14

Expert Comment

by:Kalpan
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

by:rgb192
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

by:Kalpan
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;

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:rgb192
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

by:m4trix
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

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

Author Comment

by:rgb192
ID: 36566839
So change inside the function.
0
 
LVL 7

Accepted Solution

by:
m4trix earned 500 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;
}
?>

Open in new window

0
 
LVL 7

Assisted Solution

by:m4trix
m4trix earned 500 total points
ID: 36567162
CORRECTION: line 14 should read:
$a3Images = array('weight' => $a3, 'images' => array('10.jpg', '11.gif'));

Open in new window


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

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question