0

I have array with alphanumeric key values..While filling array on particular condition I have filled array key with empty word. Now I want if on key empty is arises array should be increment with one position, so that that value is not stored in db. Simply want to skip that key and move to next.

foreach( $inputs as $key => $value) {

if(key($inputs)=="empty"){ $inputs[]++; }
else{   echo "<strong>$key</strong> Singer <strong>$value</strong></br>";
//INSERT Query;    }
}

5 Answers 5

1

For deleting elements for an array use:

unset($inputs[$key]);

For skipping, perhaps the most obvious is:

if(key($inputs) !== "empty") {
   //..
}

Note: you can insert multiple rows with one query, it might be better to do that.. build an array containing the rows "(value1,value2,...)" and use implode

Sign up to request clarification or add additional context in comments.

1 Comment

Note exact match, but this should help understand the concept: stackoverflow.com/questions/4300552/php-array-to-sql
1

you perhaps mean to use something like a conditional continue inside your foreach loop:

foreach( $inputs as $key => $value)
{
  if ($key === "empty")
  {
    continue; # at next element in $inputs
  }
  echo "<strong>$key</strong> Singer <strong>$value</strong></br>";
  //INSERT Query;
}

Comments

1

Not sure I understand what you want, but:

foreach( $inputs as $key => $value) {
    if ($key == 'empty') continue;

    // insert query
}

Comments

1

Use continue;

Change this:

if(key($inputs)=="empty"){ $inputs[]++; }

To:

if(key($inputs)=="empty"){ continue; }

Comments

0

I think you want this:

foreach( $inputs as $key => $value) {
if($key == "empty"){ 
    $inputs[$key]++; 
}
else{   
    echo "<strong>$key</strong> Singer <strong>$value</strong></br>";
    //INSERT Query;    
}

3 Comments

is this skip particular key and jump to next?
that will increment the value for that key
That Wat I had confusion. as it will increase value of key by one. N I have alphabetical key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.