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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:42:48+00:00 2026-06-03T00:42:48+00:00

I have a problem converting variables into array. I am running foreach loop to

  • 0

I have a problem converting variables into array.
I am running foreach loop to get values from my multidimensional array $images. $images array contains image name eg: “Item Blue.png” or “Item Light Oak.png” and id of each image.

foreach ($images['images'] as $image) {
    $image_name = explode(" ", substr_replace($image->filename ,"",-4));
    if(!empty($image_name[2])) {
        $colour = ucfirst($image_name[1] . " " . $image_name[2]);
    }
    else {
        $colour = ucfirst($image_name[1]);
    }
}

$colour variable is giving me Color name and $image->id can give me image id.

I would like to build $colors array with above variables that it would look like this:

$colors = array(
    'Blue' => 1620,
    'Green' => 1467,
);

Kind of like this:

$colors = array(
    '$colour' => $image->id,
);

I have no idea how to do this and I will appreciate any help to give me at least some directions.

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-03T00:42:50+00:00Added an answer on June 3, 2026 at 12:42 am

    This should be pretty straightforward … Two things to do:

    First initialize the colors array outside of your foreach:

     $colors=array();   //<-- add this
     foreach ($images['images'] as $image) {
         $image_name = explode(" ", substr_replace($image->filename ,"",-4));
         ...
    

    then just add one line after the if/else, still inside your foreach loop that will insert a new item into the $colors array.

        ...
        else {
            $colour = ucfirst($image_name[1]);
        }
        $colors[$colour]=$image->id;  //<-- add this
    }
    

    This will create a colors array with contents like what you’re looking for. I’m assuming that there is an ‘id’ key in the $image iterator. Did you need to create one?

    All that said, you’re not checking for these problems:

    1. color names with spaces, like ‘light oak’
    2. item names with spaces like ‘large item light oak.png’
    3. duplicate colors with different IDs

    Hope that helps

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

Sidebar

Related Questions

I have a problem converting a string binary to a decimal I was using
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a weird problem in converting a string to double in .NET 3.5.
I have a problem when trying to insert a new record into a database.
c++ problem different basics types during converting variables Yes, stupid problem, but I'm newbie
I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem concerning the width of a DialogPreference in Android. According to
i have a problem concerning a select query in MYSQL i have two different
I have a problem conserning uploading multiple files to my ftp server and I
I am using LaTeX and I have a problem concerning string manipulation. I want

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.