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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:50:26+00:00 2026-05-19T23:50:26+00:00

I have a 2D array which I have exploded from a string. Once it

  • 0

I have a 2D array which I have exploded from a string. Once it has exploded this is what is output:

---> 0 - 16~4~0.0~~~~false~~~~ 
---> 1 - 1000.0~21.75~L~1~2.0~2.0~L~2~ 
---> 2 - 
---> 0 - 2~5~951.3~6.4~~~false~~~~ 
---> 1 - 1000.0~11.77~L~1~ 
---> 2 - 
---> 0 - 3~6~1269.02~5.1~~~false~~~~ 
---> 1 - 5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~L~3~ 
---> 2 - 5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~ 

I want to make it so that for each position in the array it only takes the first value before the ~. I am unsure how to do this. This is the code I have so far:

$test = explode(":", $string);
foreach($test as &$value) $value = explode('|', $value);

Just in case you need it this is the original string input:

1~1~828.32~12.5~~~false~~~~|1000.0~41.73~L~1~2.0~2.0~L~2~|:4~2~4.16~12.5~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:9~3~0.16~24.0~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:16~4~0.0~~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:2~5~951.3~6.4~~~false~~~~|1000.0~11.77~L~1~|:3~6~1269.02~5.1~~~false~~~~|5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~L~3~|5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~:8~7~111.92~7.0~~~false~~~~|6.8~6.78~L~1~6.6~148.39~L~2~6.4~3.7~L~3~|7.6~128.0’…

I would like the output to be:

---> 0 - 16 
---> 1 - 1000.0
---> 2 - 
---> 0 - 2
---> 1 - 1000.0
---> 2 - 
---> 0 - 3
---> 1 - 5.0 
---> 2 - 5.1 
  • 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-19T23:50:26+00:00Added an answer on May 19, 2026 at 11:50 pm

    If I understand correctly, you want to take each element of this array and trim off everything after the first ~ character. Building on your code:

    $test = explode(":", $string);
    foreach($test as &$value)
    {
        $value = explode('|', $value);
        foreach($value as &$inner_value)
        {
            $inner_value = substr($inner_value, 0, strpos($inner_value, '~'));
        }
    }
    

    All I added was an inner foreach loop that inspects each value and removes the rest of the string after the ~ character.

    Best of coding!

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

Sidebar

Related Questions

I have the following string which I am trying to explode and sort from
So I am reading from a text file which has product information I have
I have a large data coming from a csv file which looks something like
I have a loop in my script, which iterates over an array - myArray
In my index file I get data from the database using this function (which
I have tried to explode the returned array and it just doesn't want to
I have three tables and they look like this: users_table categories_table depart_table u_id cid
I have a script which handles the naming of parent/child elements on another page.
i am using implode to store my image paths. with the special symbol '~'
I'm new to working with arrays so I need some help. With getting just

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.