0

Im trying to return first key & value from an external array, This array currently outputs like so Company name - €10.00 Company Name2 - €20.00 until the array is finished.

    protected function _results_text($results)
{
  $output = '';

  if( ! empty( $results ) ) : foreach ( $results as $result ) :
    $output .= '';
      if ( count( $result['quotes']) > 0 ) :
        foreach ( $result['quotes'] as $quote ) :

          if ( $quote->is_available() ) :
            $output .= $quote->provider['name'] .' - '. $quote->final_price;
          else:
            $output .= $quote->provider['name'] . 'Not quoting';
          endif;
        endforeach;
      else:
        $output .= 'Please call us for a quote on 1800-828-800';
      endif;

  endforeach; endif;

  return $output;
}

I have tried adding a reset in the form of adding it to the line:

 foreach ( $result['quotes'] as $quote ) :

But this didnt work (Sorry i dont have the code anymore)

This is a custom plugin for Wordpress Ninja Forms that pulls data from an external API and emails / displays the quote results on page.

I just need the first $quote->provider['name'] and $quote->final_price to be returned

Edit - here is the results function:

    public function results()

{

    if( $results = $this->get_cache('results') ) {

        return $results;

    }



    $results = [];



    if ( isset( $this->response->Quotes->Type ) ) {

        foreach($this->response->Quotes->Type as $quoteTypeResults) {



            $type = $quoteTypeResults->Desc;

            $warnings = array();



            foreach($quoteTypeResults->CriticalWarning as $warning) {

                $warnings[] = (string) $warning;

            }



            $quotes = $this->process_quotes($quoteTypeResults->Company, $this->modifiers);



            $results[] = array(

                'type' => $quoteTypeResults->Desc,

                'warnings' => $warnings,

                'quotes' => $quotes

            );

        }

    }



    return $this->set_cache('results', $results);

}
  • No, that case has only one key & value. I want to return key/value pair out of up to 6 responses. Thanks though – Adrian Morrin Nov 20 '20 at 11:16
  • You said _first_ right at the start of your question, and it didn’t mention _six_ anywhere at all. So is that the actual question now, how to break out of such a loop after a maximum of six items have been processed? https://stackoverflow.com/questions/2865373/how-would-i-stop-this-foreach-loop-after-3-iterations – CBroe Nov 20 '20 at 11:20
  • Apologies, I'm not very good at coding. Currently trying to learn using some Apps/Sites. That piece of code above is a merge tag that currently returns Up to 6 keys & values (Sorting by price lowest). These are in the form of "Company name - €10.00" for example. I just need it to return the first "Company name - €10.00" from the array. Ive read lots of methods how to do this like array_shift and reset but Im not sure how/where to implement it. Apologies again – Adrian Morrin Nov 20 '20 at 11:25
  • There is little difference between getting the first item out of an array that contains only one item to begin with, and one that might contain six. One is one, and first stays first. – CBroe Nov 20 '20 at 11:28
  • I'm still very confused at what to do here sorry, I've been at this single thing for hours and every time it just returns ALL "$quote->provider['name'] and $quote->final_price .... Instead of the first instances of each. Or it will just cause the form to stick on "processing" and never submit – Adrian Morrin Nov 20 '20 at 11:47
  • Show us a proper example of what your actual data structure is here. The code you have shown appears to loop over two different levels, over $results first, and then `$result['quotes']` second. So if you want the first element across those two levels, you of course need to apply the solution mentioned in the first link I posted _twice_. – CBroe Nov 20 '20 at 11:49
  • I voted to close as Unclear. It shouldn't be this hard for us to understand your [mcve]. – mickmackusa Nov 20 '20 at 12:02

0 Answers0