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 9049637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:26:57+00:00 2026-06-16T12:26:57+00:00

I am attempting to convert the MySQL table into an array using PHP. Each

  • 0

I am attempting to convert the MySQL table into an array using PHP. Each Listing ID in the Image Table has at least one Image ID, however the total number of Image IDs may vary.

Image Table:

|Listing ID | Image ID |
|  1234     |     1    |
|  1234     |     2    |
|  1234     |     3    |
|  1235     |     1    |
|  1235     |     2    |

Essentially, I would like to transform the Image Table into an array with a key value pair like follows:

array([0] 1234 => /FilePath/1234-1::/FilePath/1234-2::/FilePath/1234-3, [1] 1235 => /FilePath/1235-1::/FilePath/1235-2, ...)

I’ve been able to write some code that, for the most part, accomplishes this task. However, there are some issues.

The main issue with my code is that the $lid array and $prod array do not contain the same amount of elements ($prod contains one more element than $lid). This is baffling to me since they are pulling data from the same Image Table and therefore should have the same number of elements.

My code:

//Set document path
$target_path = realpath($_SERVER['DOCUMENT_ROOT']). "\mypath\image\uploads\\";
//Query to download all listing IDs in Markers table 
$query = $db->query("SELECT * FROM image");
while ($row = $query->fetch(PDO::FETCH_BOTH)) {
   $temp[] = $row;
}

foreach($temp as $i=>$v){
    $line = $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
    if($temp[$i]['L_ListingID']==$temp[$i+1]['L_ListingID']){
    //appending '::' to each string in the $prod array   
        $prod[] = $line."::";
    }else{
    // * will serve as the delimiter in the $prod array
        $prod[] = $line."*";
    //Add each listing ID into Listing Array 
        $lid[] = $v['L_ListingID'];
    } 
}

//Convert the array into a big string
$bigString = implode("", $prod);

//Chop up the big string into sections delimited by '*' and insert into 'prod' array
$prod = explode("*",$bigString);

//Combine $lid array with $prod array     
$combo = array_combine($lid, $prod);

A secondary issue is that PHP returns the following message whenever the foreach loop is run:

Notice: Undefined offset: 2789 in C:\mypath\getimage.php on line 78

Row 2789 is the last row in the image table, so I think this error notice may be related to the fact that $lid and $prod have difference of the number of elements by 1.

Any suggestions are much appreciated. Also, let me know if you can think of a more efficient way to accomplish this task.

Thanks,

  • 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-16T12:26:58+00:00Added an answer on June 16, 2026 at 12:26 pm

    With this algorithm, you’ll have to make a special case for the last line (this causes the notice “Undefined offset” as Ninsuo indicated).

    The following algorithm should build the array you expect:

    $combo = array();
    foreach($temp as $v){
        $line = $target_path.$v['L_ListingID']."-".$v['L_ImageID'].".jpg";
        $key  = $v['L_ListingID'];
        if (array_key_exists($key, $combo)) {
          // Append
          $combo[$key] .= '::' . $line;
        } else {
          // Create key
          $combo[$key] = $line;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to convert a PDF to SVG. However, the one I am using
I'm attempting to convert a PDF into a single image using GhostScript. Only the
I am attempting to convert the following dict into JSON using json.dumps : {
We are attempting to convert a flash frame into a JPEG without using the
I'm attempting to convert a Python application I coded using the cmd module into
I'm attempting to convert a C header into a Python library using ctypes and
I am attempting to convert some text files into a pdf. Now the easiest
I'm attempting to convert dates from one format to another: From e.g. October 29,
I seem to be running into great difficulty in attempting to convert a Google
I'm attempting to convert MySQL timestamps in an ActiveRecord object to another timestamp format.

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.