array_search returns just 1 result - arrays

I´m starting to study PHP and I have a doubt in one lesson about array_search.
The teachers example shows "A peeple who got a 10" and as only one of the people got a 10 everything works fine, showing the person's name.
But I've been trying to use it to fetch more than one result. In this case I created this array, with 3 people taking 10:
$notas2=array(
"Ana"=>4,
"Misca"=>10,
"Chatuba"=>6,
"Jurandir"=>7,
"Musca"=>10,
"Mickey Mouse"=>10,
);
echo array_search(10,$notas2);
This code just returns "Misca". I tried a foreach, but it returned only "MiscaMiscaMiscaMiscaMiscaMisca". lol
foreach(array_search(10,$notas2)as $tirou10){
echo $tirou10;
}
Anyone can help-me?
Tanks.

array_search will export only first corresponding key. If you want to get all people who got 10 use array_keys($notas2, 10) it will return array and instead of echo use var_dump() or var_export()

The PHP function array_search only returns the first entry.

Related

Wordpress Array Conundrum

I have the following array output which I'd like to merge:
004249512651280042495126512800424951265128
There are 3 seperate arrays there, which are outputting data from a database, which has these numbers populated. These are the arrays:
00424951265128
00424951265128
00424951265128
I've tried the array_merge function to merge the data but it doesn't seem to work, I still get the same output.
The array data comprises of the following numbers:
0
0
4249
5126
5128
The code I'm using is as follows:
$player_ids = get_post_meta($post_id,"sp_player", false);
$new_player_ids = array_merge($player_ids);
foreach ( $new_player_ids as $new_player_id ){
print_r ($new_player_id);
Is there another function I've not found yet? Am I doing something wrong here. I'm not sure why one array is being repeated more than once.
Actually guys - figured it out. I had the code within a loop hence it was multiplying the array 3 times.
Fixed it woohoo!!
Thanks for looking

Passing Array data using SESSION

I am trying to pass a Single/Multi Dimensional array to a second page using SESSION. I read and tried all questions and answers but can not get it done. Here is the 1st page:
<?php
// SendIt.php
SESSION_START();
$iA=array('A1','A2','B2','B1','B3','A3');
print_r($iA);
$_session['iA'] = $iA;
echo '<br>Click to send the array.';
?>
Here is the 2nd Page:
<?php
// GetIt.php
SESSION_START();
$iB = $_SESSION['iA'];
print_r($iB);
echo 'I am Here...';
?>
I do not get any of the iA array in the second page.
I must be missing something simple. Please check it out. Thx
I got help from a different PHP forum site and was advised that: Like any other PHP variable, _SESSION is case sensitive. So the problem is fixed by matching _SESSION variables and making them all upper case.

Can't use paramaters in HP Loadrunner 11

I have the following code:
lr_output_message( "We are on iteration #%s", lr_eval_string( "{iteration}" ) );
Return log message:
We are on iteration #{iteration}
Did anyone have the same poblem?
A few hours ago, it was works fine.
The only time you'll see an output like that is if LoadRunner can't find a parameter with that precise name.
Usually this is because you've done something wrong; a typo, etc.
Did you define "iteration" EXACTLY like you are using it? Or did you do something like use a capital 'I' (Iteration), or spell it differently, etc?
Please try this:
lr_output_message( "We are on iteration # %d", atoi(lr_eval_string("{iterationnumber}")));

cakephp find method not working

I know this is very silly question but here I am not getting any error.
Bellow query working fine
$data=$this->Test->query('SELECT * FROM tests where report_id=85');
But same query in find method are not working
$condition=array('Test.report_id'=>85);
$data=$this->Test->find('all',array('condition'=>$condition));
find statement in cakephp should be
$condition=array('Test.report_id'=>85);
$data = $this->Test->find('all',array('conditions'=>$condition));
Cakephp find statement error in 'codintion'
You should try this
$data=$this->Test->find('all',
array('conditions'=>
array('Test.report_id'=>85)
)
);
please replace condition to conditions
You made Typo.And lets understand why 's' is used.
There can be 100 conditions in an array , Not only one , so its conditions
$conditions=array('Test.report_id'=>85);
$data = $this->Test->find('all',array('conditions'=>$conditions));
And another thing -> Always use $conditions as variable naming convention because you never know there are 1,2, or 1000 conditions .

Select random item from an array with certain probabilities and add it to the stage

Its quite a big task but ill try to explain.
I have an array with a list of 200 strings and I want to be able to randomly select one and add it to the stage using code. I have movieclips exported for actionscript with the same class name as the strings in the array. Also, if it is possible, would I be able to select the strings with predictability such as the first has a 0.7 chance the second a 0.1 etc. Here is what i have currently
var nameList:Array=["Jimmy","Bob","Fred"]
var instance:DisplayObject = createRandom(nameList);
addChild(instance);
function createRandom(typeArray:Array):*
{
// Select random String from typeArray.
var selection:String = typeArray[ int(Math.random() * typeArray.length) ];
// Create instance of relevant class.
var Type:Class = getDefinitionByName(selection) as Class;
// Return created instance.
return new Type();
}
All this throws me this error
ReferenceError: Error #1065: Variable [class Jimmy] is not defined.
Ive searched for other threads similar but none combine the three specific tasks of randomisation, predictability and addChild().
I think that you've got two problems: a language problem and a logic problem. In the .fla connected to your code above, in the Library find each symbol representing a name and write into the 'AS linkage' column for that symbol the associated name -- e.g., 'Bob,' 'Fred' -- just the name, no punctuation.
Now getDefinitionByName() will find your 'Class'
If you put a different graphic into each MovieClip -- say, a piece of fruit or a picture of Bob,Jim, Fred -- and run your program you'll get a random something on stage each time.
That should solve your language problem. But the logic problem is a little harder, no?
That's why I pointed you to Mr. Kelly's solution (the first one, which for me is easier to grasp).

Resources