Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating Array Values [closed]

Tags:

php

What would be the easiest way to take some $_POST textinput data that looks like:

Array
(
    [0] => string() "The Black Keys - Gold On the Ceiling
    Angeline - Blackout
    Allele - Closure
    etc"
)

And turn it into this:

Array
(
    [0] => string() 'artist:"The Black Keys" track:"Gold On the Ceiling"'
    [1] => string() 'artist:"Angeline"  track"Blackout"'
    [2] => string() 'artist:"Allele"  track"Closure"'
    etc
)
like image 940
Elle Stewards Avatar asked Sep 11 '26 10:09

Elle Stewards


1 Answers

You can simply achieve this by using explode.

Code in action: eval.in

$string = "The Black Keys - Gold On the Ceiling
    Angeline - Blackout
    Allele - Closure
    etc";
        print $string;

    $exploded_string = explode("\n",$string);

    foreach($exploded_string as $child_string){
    $array = explode(" - ",$child_string);
    $output[]= "artist:\"".trim($array[0])."\" track:\"".trim($array[1])."\"";
    }
        print_r($output);

Hope this will help.

like image 123
Sumoanand Avatar answered Sep 14 '26 01:09

Sumoanand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!