php remove the value from array - php

need help replacing array value
i`m trying check experience and space exists in the below array and replace space with experience max (i,e next array values if its equal null/space replace with experience max),
i have pasted code below which im using to find and replace array value, its replacing the experience to experience max instead of empty to experience max
array[1] => name,
array[2] => Work Experience,
array[3] => ,
array[4] => company,
array[5] => location
o/p :
array[1] => name,
array[2] => Work Experience,
array[3] => Work Experience Max,
array[4] => company,
array[5] => location
$search = "Work Experience";
foreach ($val as $key=> $cnt) {
// echo "inside foreach ".$cnt;
if ($cnt == $search) {
echo $keyvalue = $key;
break;
}
}
if (isset($keyvalue)) {
$firstarryval = array_splice($val, $keyvalue, 1);
}
if ($firstarryval == '') {
array_splice($val, $keyvalue, 0, "Work Experience Max");
} else {
array_splice($val, $keyvalue, 0, "Work Experience Max");
}

Ok, here is the solution explained:
$arr = array( 0=>'name', 1=>'Work Experience', 2=>'', 3=>'company', 4=>'location' );
var_export($arr); echo '<br/>';
$search = "Work Experience";
$arrkey = false;
foreach($arr as $key=>$value){ // set variables and advance internal pointer
echo "inside foreach $key => $value <br/>";
if( $value == $search ) {
echo $key;
echo $arrkey = key($arr); // internal php pointer already points to next array key
break;
}
}
if ($arrkey!=false && empty($arr[$arrkey])) {
$arr[$arrkey] = 'Work Experience Max';
}
echo '<br/>'; var_export($arr);
Useful links: foreach, key, empty. Hope it will help! :-)

Related

Round number up to closest value in array

I've created an array with 'maximum' values in.
$management = array(
'100' => '1',
'500' => '2',
'1000' => '3',
);
And created a loop to find the closest value, rounding it up.
$speed = 10;
$r= '';
sort($management);
foreach($management as $mgmt => $name) {
if($mgmt >= $speed) {
$r= $name;
}
}
$r= end($management);
So, where the $speed is 10, it should pick up the array key 100 and if it was 100 it should still pickup 100 but if the speed was 200, it would pickup 500
The above is picking up 500 when the $speed is 10 though.
Can anyone help please?
You have a couple of problems with your code. Firstly, the call to sort rewrites all the keys of the $management array which you are using for the comparison; you need to use ksort instead to sort by the keys instead of the values. Secondly, since the keys are in ascending order, once one is greater than the $speed value, they all will be, so you need to break from the loop once you find a higher value. Try this instead:
$r= '';
ksort($management);
foreach($management as $mgmt => $name) {
if($mgmt >= $speed) {
$r= $name;
break;
}
}
echo $r;
Demo on 3v4l.org
this is an example on how you can do it :
$array = array(1, 10, 100, 200, 400, 500, 1000);
public function getArrayRoundUp($array, $number) {
sort($array);
foreach ($array as $a) {
if ($a >= $number) return $a;
}
return end($array);
}
$value = 950;
$nearest = getArrayRoundUp($array, $value);
//the expect result will be 1000
echo $nearest;
Use the following function
function find(array $array, $search)
{
$last = null; // return value if $array array is empty
foreach ($array as $key => $value) {
if ($key >= $search) {
return $key; // found it, return quickly
}
$last = $key; // keep the last key thus far
}
return $last;
}
Tested and Working:-
echo find($management, 100); // Will result 100
echo find($management, 200); //Will result 500

PHP Compare integers

I have the following integers
7
77
0
20
in an array. I use them to check from where a call originated.
Numbers like 730010123, 772930013, 20391938.
What I need to do is I need a way to check if the number starts with 7 or 77 for an example.
Is there any way to do this in PHP and avoid a thousand if statements?
One issue I am having is that if I check if the number starts with 7 the numbers that start with 77 are being called as well. Note that 7 numbers are mobile and 77 are shared cost numbers and not equal in any way so I need to separate them.
if (substr($str, 0, 1) == '7') ||{
if (substr($str, 0, 2) == '77'){
//starts with '77'
} else {
//starts with '7'
}
}
I made a little example with a demo array, I hope you can use it:
$array = array(
7 => 'Other',
70 => 'Fryslan!',
20 => 'New York',
21 => 'Dublin',
23 => 'Amsterdam',
);
$number = 70010123;
$place = null;
foreach($array as $possibleMatch => $value) {
if (preg_match('/^' . (string)$possibleMatch . '/', (string)$number))
$place = $value;
}
echo $place;
The answer in this case is "Fryslan". You have to remember that 7 also matches in this case? So you may want to add some metric system in case of two matches.
Is this you want?
<?php
$myarray = array(730010123, 772930013, 20391938);
foreach($myarray as $value){
if(substr($value, 0, 2) == "77"){
echo "Starting With 77: <br/>";
echo $value;
echo "<br>";
}
if((substr($value, 0, 1) == "7")&&(substr($value, 0, 2) != "77")){
echo "Starting With 7: <br/>";
echo $value;
echo "<br>";
}
}
?>
You could use preg_match and array_filter for that
function check_digit($var) {
return preg_match("/^(7|77|0)\d+$/");
}
$array_to_be_check = array("730010123" , "772930013", "20391938");
print_r(array_filter($array_to_be_check, "check_digit"));
A way to do this is to handle the "integer" you received as a number as being a string.
Doing so by something like this:
$number = 772939913;
$filter = array (
'77' => 'type1',
'20' => 'type2',
'7' => 'type3',
'0' => 'type4');
$match = null;
foreach ($filter as $key => $val){
$comp = substr($number, 0, strlen($key));
if ($comp == $key){
$match = $key;
break;
}
}
if ($match !== null){
echo 'the type is: ' . $filter[$match];
//you can proceed with your task
}

php to set order of preference of an Array Sequence using string length

$records = array(
'123PP' => 3.63,
'123DDD' => 9.63,
'123D' => 6.63,
'123PPPP' => 9.63,
'123DD' => 9.63,
'123P' => 2.63,
'123PPP' => 1.53
);
After looping through the records, I have to get only one value
whose key should be 123D because the order of preference is:
123D, 123P, 123DD, 123PP, 123DDD, 123PPP, 123PPPP...
For e.g.:
If 123D is not found in the array, then 123P is the answer.
If 123P is not found in the array, then 123DD is the answer.
And I have found a solution :
foreach ($records as $key => $value) {
if (empty($this->minLength)) {
$this->invoiceTax = $value;
$this->minLength = strlen($key);
}
elseif (strpos($key, 'P') !== false && (strlen($key) < $this->minLength)) {
$this->invoiceTax = $value;
$this->minLength = strlen($key);
}
elseif (strpos($key, 'D') !== false && (strlen($key) <= $this->minLength)) {
$this->invoiceTax = $value;
$this->minLength = strlen($key);
}
But I want to know if this code can be optimised by not storing the string length of every key.
This function could easily be tidied but this is something that could be solved with recursion. This means that if 123D is in the array the code will be highly optimised and only run once, twice for 123P, three times for 123DD, etc.
function GetMostPref($records, $key = "123", $next = "D", $depth = 0)
{
if($depth == count($records))
{
// Hit end of array with nothing found
return false;
}
if(strpos($next, 'D') !== false)
{
// Currently looking at a 'D...' key.
// Next key is the same length as this key just with Ps.
$nextKey = str_repeat('P', strlen($next));
}
else if(strpos($next, 'P') !== false)
{
// Currently looking at a 'P...' key.
// Next key has one extra char and is all Ds.
$nextKey = str_repeat('D', strlen($next)+1);
}
else
{
// Key not valid
return false;
}
if(array_key_exists($key.$next, $records))
{
// Found the key in the array so return it.
return $records[$key.$next];
}
else
{
// Recursive call with the next key and increased depth.
return GetMostPref($records, $key, $nextKey, $depth + 1);
}
}
// Testing
$records = array(
'123PP' => 3.63,
'123DDD' => 9.63,
'123D' => 6.63,
'123PPPP' => 9.63,
'123DD' => 9.63,
'123P' => 2.63,
'123PPP' => 1.53
);
// Finds 123D and returns 6.63
echo GetMostPref($records);
function prepareFunctionCall(){
$records = array('123PP' => 3.63,'123DDD' => 9.63,'123PPPP' => 9.63
,'123DD' => 9.63,'123P' => 2.63,'123PPP' => 1.53);
// '123D' => 6.63,
foreach($records as $key=>$value){
$tmp = strlen($key).$key;
$arTmp[$tmp] = $value;
}
getFirstValue($arTmp);
}
function getFirstValue($pArray){
ksort($pArray);
reset($pArray);
print key($pArray).' = '.current($pArray);
}
This is an alternative for the good solution provided by MatthewMcGovern.
I provide the alternative, because this function makes use of the php functions ksort, reset and current. Those functions are created for this type of situation and if possible then would I advise you to rewrite the keys of the array before finding out which key is the first key to select. That is what I did with the addition of the strlen. But that is suboptimal compared to rewriting the keys at the moment of collecting the data. The core of the function are the calls to the functions ksort, reset and current.

PHP Turn a Numbered String List into Array

With PHP if you have a string which may or may not have spaces after the dot, such as:
"1. one 2.too 3. free 4. for 5.five "
What function can you use to create an array as follows:
array(1 => "one", 2 => "too", 3 => "free", 4 => "for", 5 => "five")
with the key being the list item number (e.g the array above has no 0)
I presume a regular expression is needed and perhaps use of preg_split or similar? I'm terrible at regular expressions so any help would be greatly appreciated.
What about:
$str = "1. one 2.too 3. free 4. for 5.five ";
$arr = preg_split('/\d+\./', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);
I got a quick hack and it seems to be working fine for me
$string = "1. one 2.too 3. free 4. for 5.five ";
$text_only = preg_replace("/[^A-Z,a-z]/",".",$string);
$num_only = preg_replace("/[^0-9]/",".",$string);
$explode_nums = explode('.',$num_only);
$explode_text = explode('.',$text_only);
foreach($explode_text as $key => $value)
{
if($value !== '' && $value !== ' ')
{
$text_array[] = $value;
}
}
foreach($explode_nums as $key => $value)
{
if($value !== '' && $value !== ' ')
{
$num_array[] = $value;
}
}
foreach($num_array as $key => $value)
{
$new_array[$value] = $text_array[$key];
}
print_r($new_array);
Test it out and let me know if works fine

Find the last element of an array while using a foreach loop in PHP

I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.
for(int i=0; i< arr.length;i++){
boolean isLastElem = i== (arr.length -1) ? true : false;
}
In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).
I am sure there must be some standard way of doing this.
How do you solve this in PHP?
It sounds like you want something like this:
$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
if(++$i === $numItems) {
echo "last index!";
}
}
That being said, you don't -have- to iterate over an "array" using foreach in php.
You could get the value of the last key of the array using end(array_keys($array)) and compare it to the current key:
$last_key = end(array_keys($array));
foreach ($array as $key => $value) {
if ($key == $last_key) {
// last element
} else {
// not last element
}
}
Note: This doesn't work because calling next() advances the array pointer, so you're skipping every other element in the loop
why so complicated?
foreach($input as $key => $value) {
$ret .= "$value";
if (next($input)==true) $ret .= ",";
}
This will add a , behind every value except the last one!
When toEnd reaches 0 it means it is at the last iteration of the loop.
$toEnd = count($arr);
foreach($arr as $key=>$value) {
if (0 === --$toEnd) {
echo "last index! $value";
}
}
The last value is still available after the loop, so if you just want to use it for more stuff after the loop this is better:
foreach($arr as $key=>$value) {
//something
}
echo "last index! $key => $value";
If you do not want to treat the last value as special inside loops. This should be faster if you have large arrays. (If you reuse the array after the loop inside the same scope you have to "copy" the array first).
//If you use this in a large global code without namespaces or functions then you can copy the array like this:
//$array = $originalArrayName; //uncomment to copy an array you may use after this loop
//end($array); $lastKey = key($array); //uncomment if you use the keys
$lastValue = array_pop($array);
//do something special with the last value here before you process all the others?
echo "Last is $lastValue", "\n";
foreach ($array as $key => $value) {
//do something with all values before the last value
echo "All except last value: $value", "\n";
}
//do something special with the last value here after you process all the others?
echo "Last is $lastValue", "\n";
And to answer your original question "in my case to append or/and parameter while building query"; this will loop over all the values, then join them together to a string with " and " between them but not before the first value or after the last value:
$params = [];
foreach ($array as $value) {
$params[] = doSomething($value);
}
$parameters = implode(" and ", $params);
There are already many answers, but it's worth to look into iterators as well, especially as it has been asked for a standard way:
$arr = range(1, 3);
$it = new CachingIterator(new ArrayIterator($arr));
foreach($it as $key => $value)
{
if (!$it->hasNext()) echo 'Last:';
echo $value, "\n";
}
You might find something that does work more flexible for other cases, too.
One way could be to detect if the iterator has next. If there is no next attached to the iterator it means you are in the last loop.
foreach ($some_array as $element) {
if(!next($some_array)) {
// This is the last $element
}
}
SINCE PHP 7.3 :
You could get the value of the last key of the array using array_key_last($array) and compare it to the current key:
$last_key = array_key_last($array);
foreach ($array as $key => $value) {
if ($key == $last_key) {
// last element
} else {
// not last element
}
}
to get first and last element from foreach array
foreach($array as $value) {
if ($value === reset($array)) {
echo 'FIRST ELEMENT!';
}
if ($value === end($array)) {
echo 'LAST ITEM!';
}
}
So, if your array has unique array values, then determining last iteration is trivial:
foreach($array as $element) {
if ($element === end($array))
echo 'LAST ELEMENT!';
}
As you see, this works if last element is appearing just once in array, otherwise you get a false alarm. In it is not, you have to compare the keys (which are unique for sure).
foreach($array as $key => $element) {
end($array);
if ($key === key($array))
echo 'LAST ELEMENT!';
}
Also note the strict coparision operator, which is quite important in this case.
Don't add a comma after the last value:
The array:
$data = ['lorem', 'ipsum', 'dolor', 'sit', 'amet'];
The function:
$result = "";
foreach($data as $value) {
$resut .= (next($data)) ? "$value, " : $value;
}
The result:
print $result;
lorem, ipsum, dolor, sit, amet
You can still use that method with associative arrays:
$keys = array_keys($array);
for ($i = 0, $l = count($array); $i < $l; ++$i) {
$key = $array[$i];
$value = $array[$key];
$isLastItem = ($i == ($l - 1));
// do stuff
}
// or this way...
$i = 0;
$l = count($array);
foreach ($array as $key => $value) {
$isLastItem = ($i == ($l - 1));
// do stuff
++$i;
}
Assuming you have the array stored in a variable...
foreach($array as $key=>$value)
{
echo $value;
if($key != count($array)-1) { echo ", "; }
}
If you need to do something for every element except either the first or the last and only if there is more than one element in the array, I prefer the following solution.
I know there are many solutions above and posted months/one year before mine, but this is something I feel is fairly elegant in its own right. The check every loop is also a boolean check as opposed to a numeric "i=(count-1)" check, which may allow for less overhead.
The structure of the loop may feel awkward, but you can compare it to the ordering of thead (beginning), tfoot (end), tbody (current) in HTML table tags.
$first = true;
foreach($array as $key => $value) {
if ($first) {
$first = false;
// Do what you want to do before the first element
echo "List of key, value pairs:\n";
} else {
// Do what you want to do at the end of every element
// except the last, assuming the list has more than one element
echo "\n";
}
// Do what you want to do for the current element
echo $key . ' => ' . $value;
}
For instance, in web development terms, if you want to add a border-bottom to every element except the last in an unordered list (ul), then you can instead add a border-top to every element except the first (the CSS :first-child, supported by IE7+ and Firefox/Webkit supports this logic, whereas :last-child is not supported by IE7).
You can feel free to reuse the $first variable for each and every nested loop as well and things will work just fine since every loop makes $first false during the first process of the first iteration (so breaks/exceptions won't cause issues).
$first = true;
foreach($array as $key => $subArray) {
if ($first) {
$string = "List of key => value array pairs:\n";
$first = false;
} else {
echo "\n";
}
$string .= $key . '=>(';
$first = true;
foreach($subArray as $key => $value) {
if ($first) {
$first = false;
} else {
$string .= ', ';
}
$string .= $key . '=>' . $value;
}
$string .= ')';
}
echo $string;
Example output:
List of key => value array pairs:
key1=>(v1_key1=>v1_val1, v1_key2=>v1_val2)
key2=>(v2_key1=>v2_val1, v2_key2=>v2_val2, v2_key3=>v2_val3)
key3=>(v3_key1=>v3_val1)
This should be the easy way to find the last element:
foreach ( $array as $key => $a ) {
if ( end( array_keys( $array ) ) == $key ) {
echo "Last element";
} else {
echo "Just another element";
}
}
Reference : Link
I have a strong feeling that at the root of this "XY problem" the OP wanted just implode() function.
As your intention of finding the EOF array is just for the glue. Get introduced to the below tactic. You need not require the EOF:
$given_array = array('column1'=>'value1',
'column2'=>'value2',
'column3'=>'value3');
$glue = '';
foreach($given_array as $column_name=>$value){
$where .= " $glue $column_name = $value"; //appending the glue
$glue = 'AND';
}
echo $where;
o/p:
column1 = value1 AND column2 = value2 AND column3 = value3
How about using "end"?
http://php.net/manual/en/function.end.php
It sounds like you want something like this:
$array = array(
'First',
'Second',
'Third',
'Last'
);
foreach($array as $key => $value)
{
if(end($array) === $value)
{
echo "last index!" . $value;
}
}
$array = array("dog", "rabbit", "horse", "rat", "cat");
foreach($array as $index => $animal) {
if ($index === array_key_first($array))
echo $animal; // output: dog
if ($index === array_key_last($array))
echo $animal; // output: cat
}
you can do a count().
for ($i=0;$i<count(arr);$i++){
$i == count(arr)-1 ? true : false;
}
or if you're looking for ONLY the last element, you can use end().
end(arr);
returns only the last element.
and, as it turns out, you CAN index php arrays by integers. It's perfectly happy with
arr[1];
You could also do something like this:
end( $elements );
$endKey = key($elements);
foreach ($elements as $key => $value)
{
if ($key == $endKey) // -- this is the last item
{
// do something
}
// more code
}
I kinda like the following as I feel it is fairly neat. Let's assume we're creating a string with separators between all the elements: e.g. a,b,c
$first = true;
foreach ( $items as $item ) {
$str = ($first)?$first=false:", ".$item;
}
Here's my solution:
Simply get the count of your array, minus 1 (since they start in 0).
$lastkey = count($array) - 1;
foreach($array as $k=>$a){
if($k==$lastkey){
/*do something*/
}
}
foreach ($array as $key => $value) {
$class = ( $key !== count( $array ) -1 ) ? " class='not-last'" : " class='last'";
echo "<div{$class}>";
echo "$value['the_title']";
echo "</div>";
}
Reference
If it is a single dimensional array you can do this to keep it short and sweet:
foreach($items as $idx => $item) {
if (!isset($items[$idx+1])) {
print "I am last";
}
}
Here's another way you could do it:
$arr = range(1, 10);
$end = end($arr);
reset($arr);
while( list($k, $v) = each($arr) )
{
if( $n == $end )
{
echo 'last!';
}
else
{
echo sprintf('%s ', $v);
}
}
If I understand you, then all you need is to reverse the array and get the last element by a pop command:
$rev_array = array_reverse($array);
echo array_pop($rev_array);
You could also try this to make your query... shown here with INSERT
<?php
$week=array('one'=>'monday','two'=>'tuesday','three'=>'wednesday','four'=>'thursday','five'=>'friday','six'=>'saturday','seven'=>'sunday');
$keys = array_keys($week);
$string = "INSERT INTO my_table ('";
$string .= implode("','", $keys);
$string .= "') VALUES ('";
$string .= implode("','", $week);
$string .= "');";
echo $string;
?>
For SQL query generating scripts, or anything that does a different action for the first or last elements, it is much faster (almost twice as fast) to avoid using unneccessary variable checks.
The current accepted solution uses a loop and a check within the loop that will be made every_single_iteration, the correct (fast) way to do this is the following :
$numItems = count($arr);
$i=0;
$firstitem=$arr[0];
$i++;
while($i<$numItems-1){
$some_item=$arr[$i];
$i++;
}
$last_item=$arr[$i];
$i++;
A little homemade benchmark showed the following:
test1: 100000 runs of model morg
time: 1869.3430423737 milliseconds
test2: 100000 runs of model if last
time: 3235.6359958649 milliseconds
Another way to go is to remember the previous loop cycle result and use that as the end result:
$result = $where = "";
foreach ($conditions as $col => $val) {
$result = $where .= $this->getAdapter()->quoteInto($col.' = ?', $val);
$where .= " AND ";
}
return $this->delete($result);

Categories