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

  • Home
  • SEARCH
  • 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 3318188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:40:34+00:00 2026-05-17T22:40:34+00:00

I have a file with consisted of int;int values in each line. Both columns

  • 0

I have a file with consisted of int;int values in each line. Both columns are ascending, row by row. I plan to load that file into an array with following code:

while( ! feof($f) ) {
    $line = fgets( $f, 32 );
    $tmp = explode( ";", $line );
    $elements[] = array( $tmp[0] => $tmp[1] );
}

I intend to use this array to do a binary search based on the key $tmp[0]. Array will have 1000 elements, but the search will be applied for 10.000 different values. Should I simply define a 2×1000 matrix and load elements into it?

Thx

  • 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-05-17T22:40:34+00:00Added an answer on May 17, 2026 at 10:40 pm

    You can use file to get the entire contents of a file as an array of lines. Assuming that the first int in each pair is unique, you can use it as the key for the array:

    foreach (file('ints.txt') as $line) {
      list($key, $value) = explode(';', $line);
      $elements[$key] = $value;
    }
    

    Looking up values by their keys in $elements will be O(n) but really close to O(1); this might be fast enough for your purposes.

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

Sidebar

Related Questions

I have a c++ file that I want to turn into a Python module
I have a LDIF file that consists of a set of test users and
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts
I have a sqlite .db file which consists of data in string, int and
I have a simple MySQL 5 database that consists of id - Int -
I have a Java program that reads lines from text file. I want to
I am working on an application, that should have the possibility to save/load data.
i have a listview that use a simple adapter. Basically each element consist of
I have a method that converts a file to bytes so that I can
I have this code that reads from marks.txt file. #include <iostream> #include <fstream> #include

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.