array_key_exist() with array as key - php
I'm trying to check if in an array there is any value of another array. The function array_key_exist() looks like what I'm searching for, but I don't understand how to give the key value at the function as an array. Here's the code:
$risultato_query_controllo_numero = mysql_query($query_controllo_numero);
$voucher_esistenti = array();
while(($row = mysql_fetch_assoc($risultato_query_controllo_numero))) {
$voucher_esistenti[] = $row['numero'];
}
Which populates the first array with numbers:
$voucher = range($numero, $numero + $quantita);
Which populates the second array with numbers.
What I need to do now is to check if any of the value in $voucher is present in $voucher_presenti.
You can use the array_intersect function:
$overlap = array_intersect($voucher, $voucher_presenti);
You can find more examples in the documentation.
You could use the in_array() function to get the result you are looking for.
$arrayOne = range(1, 10);
$arrayTwo = range(5, 15);
foreach ($arrayOne as $value) {
if (in_array($value, $arrayTwo)) {
echo 'value '.$value.' is in the first and second array.<br />';
}
}
Resources
in_array() - Manual
in_array could be a good solution for your need, for example you can assign $voucher_esistenti only when you have a new value in the sql row.
$risultato_query_controllo_numero=mysql_query($query_controllo_numero);
$voucher_esistenti=array();
while(($row = mysql_fetch_assoc($risultato_query_controllo_numero))){
if(!in_array($row['numero'], $voucher_esistenti) {
$voucher_esistenti[] = $row['numero'];
}
} // this solution isn't optimal, because you will check subarrays with each new value
There's a better way to achieve that, by using a hashmap which has a complexity of O(1) ( best complexity :) )
$risultato_query_controllo_numero=mysql_query($query_controllo_numero);
$voucher_esistenti=array();
while(($row = mysql_fetch_assoc($risultato_query_controllo_numero))){
// here is what we changed, instead of key = value, we actually append keys
if(!isset($voucher_esistenti[$row['numero']]) {
$voucher_esistenti[$row['numero']] = true;
}
}
/*
The second implementation is a lot faster due to the algorithm, but you will have to change the reading of $voucher_esistenti array. */
Related
Looking for an element in an array in PHP
I don't know if I'm managing this array in the best way. The array I have is this: $bass = $_POST['bass']; $selected_scale = $_POST['scale']; $major_scales = array ( array("C","D","E","F","G","A","B","C","D","E","F","G","A","B",), array("C#","D#","E#","F#","G#","A#","B#","C#","D#","E#","F#","G#","A#","B#",), array("Db","Eb","F","Gb","Ab","Bb","C","Db","Eb","F","Gb","Ab","Bb","C",), array("D","E","F#","G","A","B","C#","D","E","F#","G","A","B","C#"), array("D#","E#","F##","G#","A#","B#","C##","D#","E#","F##","G#","A#","B#","C##"), array("Eb","F","G","Ab","Bb","C","D","Eb","F","G","Ab","Bb","C","D"), array("E","F#","G#","A","B","C#","D#","E","F#","G#","A","B","C#","D#"), array("E#","F##","G##","A#","B#","C##","D##","E#","F##","G##","A#","B#","C##","D##"), array("Fb","Gb","Ab","Bbb","Cb","Db","Eb","Fb","Gb","Ab","Bbb","Cb","Db","Eb"), array("F","G","A","Bb","C","D","E","F","G","A","Bb","C","D","E"), array("F#","G#","A#","B","C#","D#","E#","F#","G#","A#","B","C#","D#","E#"), array("Gb","Ab","Bb","Cb","Db","Eb","F","Gb","Ab","Bb","Cb","Db","Eb","F"), array("G","A","B","C","D","E","F#","G","A","B","C","D","E","F#"), array("G#","A#","B#","C#","D#","E#","F##","G#","A#","B#","C#","D#","E#","F##"), array("Ab","Bb","C","Db","Eb","F","G","Ab","Bb","C","Db","Eb","F","G"), array("A","B","C#","D","E","F#","G#","A","B","C#","D","E","F#","G#"), array("A#","B#","C##","D#","E#","F##","G##","A#","B#","C##","D#","E#","F##","G##"), array("Bb","C","D","Eb","F","G","A","Bb","C","D","Eb","F","G","A"), array("B","C#","D#","E","F#","G#","A#","B","C#","D#","E","F#","G#","A#"), array("B#","C##","D##","E#","F##","G##","A##","B#","C##","D##","E#","F##","G##","A##"), array("Cb","Db","Eb","Fb","Gb","Ab","Bb","Cb","Db","Eb","Fb","Gb","Ab","Bb") ); $bass is a string, like the one inside the arrays. The $selected_scale is just a number. What I'm trying to do is to find the $bass in one of those array in the position of $selected_scale. Basically, $bass = $major_scales[$selected_scale]. Therefore I want to create a loop in order to get the elements after that. But I don't know how to manage in this case the situation. I've looked everything in internet and try various solutions without success. I'd like to know how can I do it. Thanks
Try to use next loop: // if value exists in mentioned index if (in_array($bass,$major_scales[$selected_scale])){ // index of that value in that array $tmp_ind = array_search($bass,$major_scales[$selected_scale]); // length of the array $len = count($major_scales[$selected_scale]); // store values after this value $res = []; for ($i=$tmp_ind;$i<$len;$i++){ $res[$i] = $major_scales[$selected_scale][$i]; } } print_r($res); Demo1 If you need to find value by index $selected_scale in one of these arrays and also store values after this position: foreach($major_scales as $ar){ if ($ar[$selected_scale] == $bass){ // length of the array $len = count($ar); // store values after this value $res = []; for ($i=$selected_scale;$i<$len;$i++){ $res[$i] = $ar[$i]; } } } print_r($res); Demo2
How to put the result of foreach loop in an Array and get the highest value
in this foreach loop, i put a function returning the room quantity, each $sumqty['room_qty'] has different value, my question is, i want to get the highest value of $sumqty['room_qty'], do i put it in an array? how? any help will be appreciated. <?php foreach($reservation as $res){ $sumqty = sumqty($res['arrival'],$res['departure'],$res['room_id']); $sumqty['room_qty']; } $highest = ???; ?> i want to display the highest value by echo $highest
Yes, you can use an array. Provided $sumqty['room_qty'] is a numeric value the following will work: $allquantities=array(); foreach($reservation as $res){ $sumqty = sumqty($res['arrival'],$res['departure'],$res['room_id']); $allquantities[]=$sumqty['room_qty']; } $highest = max($allquantities);
Assuming that sumqty takes an 'arrival', 'departure' and 'room id' and returns an array with key 'room_qty' mapped to some integer: <?php $highest = -1000000000; //some really, really small number foreach ($reservation as $res) { $sumqty = sumqty($res['arrival'],$res['departure'],$res['room_id']); if ($sumqty['room_qty'] > $highest) { $highest = $sumqty['room_qty']; } } echo $highest; The general idea is that each time you find a higher value, you replace the previously highest value with the new higher value. No extra array is necessary to accomplish this! Solutions that use an array to solve this problem are unnecessarily complicated. (edit: "complicated" is a poor choice of words; if an extra array is used than the program is slightly less efficient)
Get an array of all the values of $sumqty['room_qty'], $room_qty[] as in this case. <?php $room_qty = array(); foreach($reservation as $res){ $sumqty = sumqty($res['arrival'],$res['departure'],$res['room_id']); $room_qty[] = $sumqty['room_qty']; } $highest = max($room_qty); ?> Once you have the array you could use max() function to find the maximum value in the given array. Go through this manual you will get be clear about it.
PHP array copy certain keys, built-in functions? Nested loop performance?
I have a PHP array that I'd like to duplicate but only copy elements from the array whose keys appear in another array. Here are my arrays: $data[123] = 'aaa'; $data[423] = 'bbb'; $data[543] = 'ccc'; $data[231] = 'ddd'; $data[642] = 'eee'; $data[643] = 'fff'; $data[712] = 'ggg'; $data[777] = 'hhh'; $keys_to_copy[] = '123'; $keys_to_copy[] = '231'; $keys_to_copy[] = '643'; $keys_to_copy[] = '712'; $keys_to_copy[] = '777'; $copied_data[123] = 'aaa'; $copied_data[231] = 'ddd'; $copied_data[643] = 'fff'; $copied_data[712] = 'ggg'; $copied_data[777] = 'hhh'; I could just loop through the data array like this: foreach ($data as $key => $value) { if ( in_array($key, $keys_to_copy)) { $copied_data[$key] = $value; } } But this will be happening inside a loop which is retrieving data from a MySQL result set. So it would be a loop nested within a MySQL data loop. I normally try and avoid nested loops unless there's no way of using PHP's built-in array functions to get the result I'm looking for. But I'm also weary of having a nested loop within a MySQL data loop, I don't want to keep MySQL hanging around. I'm probably worrying about nested loop performance unnecessarily as I'll never be doing this for more than a couple of hundred rows of data and maybe 10 keys. But I'd like to know if there's a way of doing this with built-in PHP functions. I had a look at array_intesect_key() but that doesn't quite do it, because my $keys_to_copy array has my desired keys as array values rather than keys. Anyone got any ideas? Cheers, B
I worked it out - I almost had it above.I thought I'd post the answer anyway for completeness. Hope this helps someone out! array_intersect_key($data, array_flip($keys_to_copy)) Use array_flip() to switch $keys_to_copy so it can be used within array_intersect_keys() I'll run some tests to compare performance between the manual loop above, to this answer. I would expect the built-in functions to be faster but they might be pretty equal. I know arrays are heavily optimised so I'm sure it will be close. EDIT: I have run some benchmarks using PHP CLI to compare the foreach() code in my question with the code in my answer above. The results are quite astounding. Here's the code I used to benchmark, which I think is valid: <?php ini_set('max_execution_time', 0);//NOT NEEDED FOR CLI // BUILD RANDOM DATA ARRAY $data = array(); while ( count($data) <= 200000) { $data[rand(0, 500000)] = rand(0, 500000); } $keys_to_copy = array_rand($data, 100000); // FOREACH $timer_start = microtime(TRUE); foreach ($data as $key => $value) { if ( in_array($key, $keys_to_copy)) { $copied_data[$key] = $value; } } echo 'foreach: '.(microtime(TRUE) - $timer_start)."s\r\n"; // BUILT-IN ARRAY FUNCTIONS $timer_start = microtime(TRUE); $copied_data = array_intersect_key($data, array_flip($keys_to_copy)); echo 'built-in: '.(microtime(TRUE) - $timer_start)."s\r\n"; ?> And the results... foreach: 662.217s array_intersect_key: 0.099s So it's much faster over loads of array elements to use the PHP array functions rather than foreach. I thought it would be faster but not by that much!
Why not load the entire result set into an array, then begin processing with nested loops? $query_result = mysql_query($my_query) or die(mysql_error()); $query_rows = mysql_num_rows($query_result); for ($i = 0; $i < $query_rows; $i++) { $row = mysql_fetch_assoc($query_result); // 'key' is the name of the column containing the data key (123) // 'value' is the name of the column containing the value (aaa) $data[$row['key']] = $row['value']; } foreach ($data as $key => $value) { if ( in_array($key, $keys_to_copy)) { $copied_data[$key] = $value; } }
Make 1d Array from 1st member of each value in 2d Array | PHP
How can you do this? My code seen here doesn't work for($i=0;i<count($cond);$i++){ $cond[$i] = $cond[$i][0]; }
It can be as simple as this: $array = array_map('reset', $array);
There could be problems if the source array isn't numerically index. Try this instead: $destinationArray = array(); for ($sourceArray as $key=>$value) { $destinationArray[] = $value[0]; //you may want to use a different index than '0' }
// Make sure you have your first array initialised here! $array2 = array(); foreach ($array AS $item) { $array2[] = $item[0]; } Assuming you want to have the same variable name afterwards, you can re-assign the new array back to the old one. $array = $array2; unset($array2); // Not needed, but helps with keeping memory down Also, you might be able to, dependant on what is in the array, do something like. $array = array_merge(array_values($array));
As previously stated, your code will not work properly in various situation. Try to initialize your array with this values: $cond = array(5=>array('4','3'),9=>array('3','4')); A solution, to me better readable also is the following code: //explain what to do to every single line of the 2d array function reduceRowToFirstItem($x) { return $x[0]; } // apply the trasnformation to the array $a=array_map('reduceRowTofirstItem',$cond); You can read the reference for array map for a thorough explanation. You can opt also for a slight variation using array_walk (it operate on the array "in place"). Note that the function doesn't return a value and that his parameter is passed by reference. function reduceToFirstItem(&$x) { $x=$x[0]; } array_walk($cond, 'reduceToFirstItem');
That should work. Why does it not work? what error message do you get? This is the code I would use: $inArr;//This is the 2D array $outArr = array(); for($i=0;$i<count($inArr);$i++){ $outArr[$i] = $inArr[$i][0]; }
Keeping an array sorted in PHP
I have a PHP script which reads a large CSV and performs certain actions, but only if the "username" field is unique. The CSV is used in more than one script, so changing the input from the CSV to only contain unique usernames is not an option. The very basic program flow (which I'm wondering about) goes like this: $allUsernames = array(); while($row = fgetcsv($fp)) { $username = $row[0]; if (in_array($username, $allUsernames)) continue; $allUsernames[] = $username; // process this row } Since this CSV could actually be quite large, it's that in_array bit which has got me thinking. The most ideal situation when searching through an array for a member is if it is already sorted, so how would you build up an array from scratch, keeping it in order? Once it is in order, would there be a more efficient way to search it than using in_array(), considering that it probably doesn't know the array is sorted?
Not keeping the array in order, but how about this kind of optimization? I'm guessing isset() for an array key should be faster than in_array() search. $allUsernames = array(); while($row = fgetcsv($fp)) { $username = $row[0]; if (isset($allUsernames[$username])) { continue; } else { $allUsernames[$username] = true; // do stuff } }
The way to build up an array from scratch in sorted order is an insertion sort. In PHP-ish pseudocode: $list = [] for ($element in $elems_to_insert) { $index = binary_search($element, $list); insert_into_list($element, $list, $index); } Although, it might actually turn out to be faster to just create the array in unsorted order and then use quicksort (PHP's builtin sort functions use quicksort) And to find an element in a sorted list: function binary_search($list, $element) { $start = 0; $end = count($list); while ($end - $start > 1) { $mid = ($start + $end) / 2; if ($list[$mid] < $element){ $start = $mid; } else{ $end = $mid; } } return $end; } With this implementation you'd have to test $list[$end] to see if it is the element you want, since if the element isn't in the array, this will find the point where it should be inserted. I did it that way so it'd be consistent with the previous code sample. If you want, you could check $list[$end] === $element in the function itself.
The array type in php is an ordered map (php array type). If you pass in either ints or strings as keys, you will have an ordered map... Please review item #6 in the above link.
in_array() does not benefit from having a sorted array. PHP just walks along the whole array as if it were a linked list.