top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use foreach loop for filtering on the API to show the match value from database?

0 votes
436 views

I am working on an API integration. Now I have a problem. First I am trying to show all specific city hotels, which city is having in my database. First FOREACH worked as well its search hotel by my database city, but on Second FOREACH I am trying to show First FOREACH city hotels show only match hotel which hotels matches from database and its not worked.

My Task is: 1st search by city which city have on my database and 2nd time search in 1st time hotel and showed hotel only match my database hotel.

1st foreach:

if($RowCount>0){
            foreach($Results as $Result){
                foreach($api_array as &$value){
                    if($Result['county'] == $value['address']['city']){ 
                        $final_array[] = $value; 
                    } 
                } 
            }
        }

2nd foreach:

foreach($final_array as &$display){
            var_dump($display);
            if($Result['hotel'] == $display['property_name']){ 
                $final_array2[] = $display; 
            }  
        }

full code:

foreach($array as $api_array){
        $final_array = array(); 
        $Results = $wpdb->get_results( "select * FROM hotels where county = '$countyname'",ARRAY_A );
        $RowCount  =  $wpdb->num_rows;

        if($RowCount>0){
            foreach($Results as $Result){
                foreach($api_array as &$value){
                    if($Result['county'] == $value['address']['city']){ 
                        $final_array[] = $value; 
                    } 
                } 
            }
        }
        foreach($final_array as &$display){
            if($Result['hotel'] == $display['property_name']){ 
                $final_array2[] = $display; 
            }  
        }
    }
posted May 29, 2017 by Anand Huded

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Replace this on your 2nd foreach loop.

       foreach($Results as $Result){
            foreach($final_array as &$display){
                if($Result['hotel'] == $display['property_name']){ 
                    $final_array2[] = $display;
                }  
            }
        }
answer Jun 3, 2017 by Ananna Dey
Similar Questions
0 votes

I am trying to export certain data from my PHP form to CSV. I can echo out to screen during testing and I can also export to CSV the static test data (stored in the $contents array) you see below. But I am stuck trying to export the certain fields that I only need to export.
This is my code

// How do I get this info into the CSV?
/*foreach ( $entries as $entry ) :  
    echo $entry['2'];
    echo $entry['3'];
    echo $entry['6'];
endforeach;*/

$csv_headers = [
    'Organisation Name',
    'Registered Charity Number',
    'Address',
    'Phone',
];

$contents = [
  [2014, 6, '1st half', 'roland@fsjinvestor.com', 0, 0],
  [2014, 6, '1st half', 'steve@neocodesoftware.com', 0, 0],
  [2014, 6, '1st half', 'susanne@casamanager.com', 0, 0],
  [2014, 6, '1st half', 'tim', 0, 0]
];

fputcsv($output_handle, $csv_headers);

foreach ( $contents as $content) :
    fputcsv($output_handle, $content);
endforeach;
+2 votes

How to use forEach() method as loop in Java 8, please explain with example?

–1 vote

I want to update my sql database record and want to populate the value of previous record. So anybody can help me to get the previous value of the record to show when the update require.

...