Solved

change two parameters to one

Posted on 2011-09-19
11
220 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now