Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My code is...

$string ="foo:bar,bar,bar|foo2:bar2,bar2,bar2";

$first_array = explode("|", $string);

function split(&$block) {

$block = explode(":", $block);

}

array_walk($first_array, "split");

echo $block["0"]["0"];
echo "<br />";
echo $block["0"]["1"];

?>

Thank you for the help thus far. From what I've gathered this should be the clean version of the supplied code. This does not echo anything, and neither does the code supplied.

share|improve this question
    
$final_array is going to be an array of arrays. echo $result will just spit out the word Array, not the array's contents. –  Marc B yesterday
    
Maybe try using more sensible delimiters like <delim1> and <delim2> rather than ?!#@^&$%@# and &*%&@#*&^% which you probably typed in manually and mistyped somewhere throwing it all off. –  developerwjk yesterday
    
Also get your variable names consistent. Is it $city_array or $cityarray... –  developerwjk yesterday
    
"I don't want to use a million columns and tables, so I would like to split my result" Wait, you want to store relational data in a relational database, but you don't want to organize the data in a relational way? Am I misunderstanding something? –  robbmj yesterday

2 Answers 2

This will do the trick:

$string ="foo:bar:bar:bar|foo2:bar2:bar2:bar2";

// first explode on |
$first_array = explode("|", $string);
// this function go on each lines of an array and transform each by a function
array_walk($first_array, function(&$item) {
         // so for each line explode with ':' as delimiter
         $item = explode(':', $item);
});

// To check all lines of the array
foreach($first_array as $line_array) {
     //my code for each sub-array
}

// To get only the last (second)
$second_sub = array_pop($first_array);

// if you want one dimension with all exploded you can use
// this function split with a regexp pattern
// "/[\|:,]+/" foreach "|" or ":" or ","  split the string
preg_split("/[\|:,]+/", $string);
share|improve this answer
    
Can you explain the syntax for me? Thank you! –  Allenph yesterday
    
just edited with few explainations –  Charkan yesterday
    
did it works?.. –  Charkan yesterday
    
I figured out the syntax. However, this is only half the battle. How do I pull the information from the second dimension of the array? Thanks. –  Allenph yesterday
    
just edited my code, with method to access each lines and method to get only the last –  Charkan yesterday

Don't use $block since it's defined in another scope, moreover you cannot give "split" as a name for your function since it's an existing php function

$string ="foo:bar,bar,bar|foo2:bar2,bar2,bar2";

$first_array = explode("|", $string);

function mysplit(&$block) {

$block = explode(":", $block);

}

array_walk($first_array, "mysplit");

echo $first_array[0][0];
echo "<br />";
echo $first_array[0][1];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.