Hey All,

Im trying to extract info from an array and format it specific order.

The arrays are being generated from the backend and are dynamic, an example would be as follows:

Array ( [0] => 128 [1] => 265 [2] => 225 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 )

Another example would be

Array ( [0] => 32 [1] => 546 [2] => 183 [3] => 962 [4] => 111 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 )

Basically what i want to do is only extract the ones that arent 0 and then place a comma between them, and before the last one have a &

So for example, if there were only 2 values in the array that didnt equal zero, the output would be

132 & 435

or another example if there were more than 2 values that didnt equal zero

132, 435, 940, 345 & 235

Im really stumped on this, any help would be greatly appreaciated.

Cheers,

    You're probably over thinking it. You just loop through with a "for" and "if" the value is not equal to 0 place it in a new array. Then loop through that array and "if" the index is equal to the length of the new array output the "&".

    $array = array ( 0 => 128, 1 => 265, 2 => 225, 3 => 0, 4 => 0, 5 => 0, 6 => 0. 7 => 0, 8 => 0, 9 => 0 );
    
    $length = count($array);
    
    $newarray = array();
    
    for($i = 0; $i <= $length; $i++){
        if($array[$i] != 0) {
            $newarray[] = $array[$i];
        }
    }
    
    $newlength = count($newarray);
    
    $string ='';
    for($i = 0; $i <= $newlength; $i++){
        if($i == $newlength) {
            $string .= '& '.$newarray[$i];
        }
        else {
            $string .= $newarray[$i].' ';
        }
    }
    echo $string; // outputs 128 265 & 225
    

      Thanks for your response.

      I am getting some errors though.

      This is the complete code im using and the errors produced

      $auth_array = array($result[29],$result[30],$result[31],$result[32],$result[33],$result[34],$result[35],$result[36],$result[37],$result[38]);
      
      $length = count($auth_array);
      
      $newarray = array();
      
      for($i = 0; $i <= $length; $i++){
          if($auth_array[$i] != 0) {
              $newarray[] = $auth_array[$i];
          }
      }
      
      $newlength = count($newarray);
      
      $string ='';
      for($i = 0; $i <= $newlength; $i++){
          if($i == $newlength) {
              $string .= '& '.$newarray[$i];
          }
          else {
              $string .= $newarray[$i].' ';
          }
      }
      echo $string;

      which results in the following errors

      Notice: Undefined offset: 10 in /default.php on line 167

      Notice: Undefined offset: 3 in /default.php on line 177

      Line 167 is

          if($auth_array[$i] != 0) {

      and line 177 is

              $string .= '& '.$newarray[$i];

      It does output something after the error which is the following

      128 265 225 &

      It should display 128, 265 & 225

      Thanks for your help

        Try this. Variable names were "inspired" from Krik's post. 😃
        I'm not fanatic for a small number of code lines, but I find it more elegant.

        $auth_array = array(0, 1, 2, 3, 4, 0, 5, 6, 7, 8, 9, 10, 8, 0, 56);
        
        foreach ($auth_array as $item) {
            if ($item != 0) $new_array[] = $item;
        }
        
        $last = array_pop($new_array);
        
        echo implode(", ", $new_array) . ' & ' . $last;

          I like nevvermind's example, but in this case array_filter can come in handy:

          <?php
          
          $auth_array = array(0, 1, 2, 3, 4, 0, 5, 6, 7, 8, 9, 10, 8, 0, 56);
          
          $new_array = array_filter($auth_array);
          $last = array_pop($new_array);
          echo implode(', ', $new_array) . ' & ' . $last;

          You might want to handle an array with no elements and an array with exactly one element as special cases.

            I'm not fanatic for a small number of code lines, but I find it more elegant.

            I must agree, I definitely like less lines. 🆒 I think I have seldom ever used an example that I didn't find a way to reduce the number lines.

            The example I gave was more to make sure even the least experienced coder could figure it out. I had a couple ideas on how to make it simpler but as I suspected he is getting the data via some other code

            $auth_array = array($result[29],$result[30],$result[31],$result[32],$result[33],$result[34],$result[35],$result[36],$result[37],$result[38]);
            

            It looks like the "$result" array needs to be constructed differently. Makes me wonder if "$result" is data from a database and if a better query wouldn't make this whole thing a lot simpler.

            Or at the very least run the filtering code on the "$result" array instead of making a new array that you then filter through.

              Hey All,

              Thanks for the feedback on this topic ... I have got it working using the code in post number 5 .... but now that if only 1 result gets returned it displays as "& result"

              Is there any way of taking this out if only 1 result is returned?

              Cheers,

                Yes, as I mentioned, you should handle the special cases, e.g.,

                <?php
                
                $auth_array = array(0, 1, 2, 3, 4, 0, 5, 6, 7, 8, 9, 10, 8, 0, 56);
                
                if (empty($auth_array)) {
                    echo 'There is nothing to display.';
                } elseif (count($auth_array) == 1) {
                    echo $auth_array[0];
                } else {
                    $new_array = array_filter($auth_array);
                    $last = array_pop($new_array);
                    echo implode(', ', $new_array) . ' & ' . $last;
                }

                  Hey,

                  Thanks for the reply ... Sorry, i must sound like a dope, but take the following array for example

                  $auth_array = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56);
                  
                  if (empty($auth_array)) {
                      echo 'There is nothing to display.';
                  } elseif (count($auth_array) == 1) {
                      echo $auth_array[0];
                  } else {
                      $new_array = array_filter($auth_array);
                      $last = array_pop($new_array);
                      echo implode(', ', $new_array) . ' & ' . $last;
                  }	

                  It still displays "& 56"

                    Ah, my mistake. You need to tweak it to count based on the filtered array.

                      Write a Reply...