0

I have a multiple arrays with strings that comes out from a wordpress database. Its a table from a plugin that stores the data in a very strange way, like this:

    print_r($results);
    
Array 
(
    [form] => text^name14^Antony~text^secondname14^White~email^email14^[email protected]
)
Array  
(
    [form] => ......
)

I need to get the clean data, so:

foreach ($results as $result) {

    $formdata_array = explode('~',$result);
    $formdata_array_count = count($formdata_array);
    for ( $i=0 ; $i < $formdata_array_count ; $i++) {
       if ( empty( $formdata_array[$i] ) ) {
          continue;
       }
       $elemnts = explode('^',$formdata_array[$i]);

       $type = $elemnts[0];
       $element_name = $elemnts[1];
       $value = $elemnts[2];
       $value = nl2br($value);
}

And at this point I get:

print_r($value)

    Antony 
    White
    [email protected]

But I need to have an array to work with

Array
    (
        [0] => Antony
        [1] => White
        [2] => [email protected]
    )

I triede differents methods like array_merge, array_column, array_combine but I can't get the final result

4
  • PLease show us your code, add a minimal reproducible example. Commented Dec 30, 2022 at 15:05
  • 1
    My guess is that you call print_r(array($output)); three times, for the three items. We need to see the code that does that to help you. Commented Dec 30, 2022 at 15:05
  • 1
    Are you explode()ing your input into $output properly to make the array? Commented Dec 30, 2022 at 15:07
  • Updated mi question Commented Dec 30, 2022 at 15:34

1 Answer 1

1

This probably is what you are looking for:

<?php

$results = [
   ['form' => "text^name14^Antony~text^secondname14^White~email^email14^[email protected]"],
   ['form' => "text^name14^Georgy~text^secondname14^Black~email^email14^[email protected]"],
];

foreach ($results as $result) {
    $output = [];
    $formdata_array = explode('~',$result['form']);
    $formdata_array_count = count($formdata_array);
    for ( $i=0 ; $i < $formdata_array_count ; $i++) {
       if ( empty( $formdata_array[$i] ) ) {
          continue;
       }
       $elements = explode('^',$formdata_array[$i]);

       $output[] = [
        'type' => $elements[0],
        'name' => $elements[1],
        'value' => $elements[2],
       ];
    }
   print_r(array_column($output, 'value'));
}

The output is:

Array
(
    [0] => Antony
    [1] => White
    [2] => [email protected]
)
Array
(
    [0] => Georgy
    [1] => Black
    [2] => [email protected]
)
Sign up to request clarification or add additional context in comments.

6 Comments

It works but with a strange behavior: the 1st array it's ok, the 2nd is 1st + 2nd, the 3rd is 1st + 2nd +3rd and so on. You can try using multiple lines
That is not strange at all, that is because the $output has not been reset between loop runs. I changed the code accordingly.
My output from database is the one you see on top, multiple arrays, not an array vith multiple values
Please note that you really should be able to spot such things yourself. We are not here to implement a fully working solution, so to do you work for you. We are here to help you find your solution.
Yes, sorry. It works. I got it. Thank you for your time
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.