Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:50+00:00 2026-06-18T07:39:50+00:00

I have an array that I’m trying alter the output if a particular string

  • 0

I have an array that I’m trying alter the output if a particular string is found.

As you can see below it successfully changes Upper Captiva to Captiva when the condition occurs but my else statement is ignored where it should simply show the $row[‘SUBDIVISION’] value if the condition does not occur.

I’ve tried adding $mycodes and $row[‘SUBDIVISION’] to the else line with no luck.

<?php
    $mycodes = explode(",", $row['SUBDIVISION']);
    $arr = array(   "Upper Captiva" => "Captiva",);
    $printedsomething=0;
    foreach($mycodes as $code) {
        if ($arr[$code] != '' ) {
            if ($printedsomething==1)  echo ", " . $arr[$code];
            else echo $mycodes;
            $printedsomething=1;
        }
    }  //end for loop
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:39:51+00:00Added an answer on June 18, 2026 at 7:39 am

    This is a code example as you asked for in the comments. If this does not work, please let me know so I can remove this answer.

    <?php
      $mycodes = explode(',', $row['SUBDIVISION']);
      $arr = array('Upper Captiva' => 'Captiva');
    
      $printedsomething = false;
    
      foreach($mycodes as $code) {
        if ( !empty($arr[$code]) ) {
          /*******************************
          * This would be your original code refactored.
            $mycodes should probably be $arr[$code]     */
    
          echo $printedsomething ? ', ' . $arr[$code] : $mycodes;
    
          /*******************************/
          $printedsomething = true;
        }
      }  //end for loop
    ?>
    

    But I don’t thik this is what you want. As far as I understand, you want to look if any of your codes ar in $arr, and if so, echo them out, separated by a comma.

    Besides the fact that you can’t echo out an array ($mycodes) (I guess you wanted to output $arr[$code] there), there’s probably a more elegant way, which would be to define an empty results array before the loop:

    $results = array();
    

    Then you could put the relevant values in there, so the code between the stars would go like this:

    $results[] = $arr[$code];
    

    So you could do this after the loop:

    implode(', ', $results);
    

    So the final code would look something like this:

    <?php
      $mycodes = explode(',', $row['SUBDIVISION']);
      $arr = array('Upper Captiva' => 'Captiva');
    
      $results = array();    
    
      foreach($mycodes as $code) {
        if ( !empty($arr[$code]) ) {
           $results[] = $arr[$code];
        }
      }
    
      echo implode(', ', $results);
    ?>
    

    Another, even more elegant approach would be using some of the countless array-functions PHP provides. For instance, if you just want to output only your distinct codes and filter them through another array, you could use array_flip() on $mycodes in order to turn the values into keys. After that you could use array_intersect_key() to filter only the keys which are also present in $arr. That way, you could express the whole loop and its output in one line:

    <?php
      $mycodes = explode(',', $row['SUBDIVISION']);
      $arr = array('Upper Captiva' => 'Captiva');
    
      echo implode(', ', array_intersect_key($arr, array_flip($mycodes)));
    ?>
    

    I can’t tell if this works for you since I don’t know exactly what your arrays look like. Also I’m not sure if this meets your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that contains @sign in the object. I can't access that
I have an array that contains string with the following format: 1-a1,1-a2,1-a3,1-a4,2-b1,2-b2,2-b3,2-b4,3-c1,3-c2,3-c3,4-d1,4-d2 where a
I have array that I can display by code: foreach($form->data as $key => $value){
I have an array that can store numbers in such as 01, 01A, 01B,
I have an array that contains data as follows (string and time data separated
I have an array that is stored in a key, see values format: {id:
I have an array that can vary in its dimension, sometimes can be small,
I have an array that looks just like that: array(3) { [fk_article_id]=> string(1) 4
I have an array that I populate with parents, children, children of children, children
I have an array that looks something like this: Array ( [0] => apple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.