Solved

change two parameters to one

Posted on 2011-09-19
11
221 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now