Jump to content

Recommended Posts

if i do this:
[code=php:0]$foo = "one=1,two=2,three=3,four=4";
$foo = explode(",", $foo);

echo $foo[0];
echo $foo[1];
echo $foo[2];
echo $foo[3];[/code]

it will output:
[b]one=1two=2three=3four=4[/b]

How can I make it only output the numbers and take away the one=/two=/etc
IF the one=/two=/etc could be dynamic and not always known?
Link to comment
https://forums.phpfreaks.com/topic/33280-solved-explode-help/
Share on other sites

You could explode it a second time, exploding with ='s sign.

Like such.

[code]<?php

$foo = "one=1,two=2,three=3,four=4";
$foo = explode(",", $foo);

$i=0;
while ($foo[$i]) {

$findNumber=explode("=", $foo[$i]);

$number[$i]=$findNumber[1];
$word[$i]=$findNumber[0];

$i++;

}

echo $number[0]."<br />\n";
echo $word[1]."<br />\n";
echo $foo[2]."<br />\n";
echo $number[3]."<br />\n";

?>[/code]

Tested, working, code.
Link to comment
https://forums.phpfreaks.com/topic/33280-solved-explode-help/#findComment-155475
Share on other sites

Try this:

[code]$foo = "one=1,two=2,three=3,four=4";
$myArray = explode(',', $foo);
$newArray = array(count($myArray));
$count = 0;


//loops back through a new array of size $i
//creates new array with pointer and value

while($count < count($myArray)) {


//breaking down urlArray into array pointer and array value
                      //also destroying those evil = signs

foreach($myArray as $row) {

$pointer = substr($url, 0, strpos($row, "="));
$value = substr($row, strpos($row, "=") + 1, strlen($row));
$newArray["$pointer"] = $value;

} //end foreach

$count++;

} //end while


[/code]

yeah, but u exploded the single spot of the array and had to do it for each one...if he tries this code, it gives a new array with the = taken out and his original keys intact
Link to comment
https://forums.phpfreaks.com/topic/33280-solved-explode-help/#findComment-155484
Share on other sites

[quote author=DarkendSoul link=topic=121447.msg499431#msg499431 date=1168223649]
Code posted and tested, it works magic2goodil. ;)
[/quote]

yes, and that is awesome and i thank you.
Doing it the way you posted also means I can retain any value from the original
which is an awesome added bonus!


magic2goodil, thanks for your suggestion.
I think DarkendSoul's solution better suites my needs :)
Link to comment
https://forums.phpfreaks.com/topic/33280-solved-explode-help/#findComment-155488
Share on other sites

[quote author=magic2goodil link=topic=121447.msg499446#msg499446 date=1168224385]
:p

DarkenedSoul 1 Magic  0
You win this round, but next time I wont go so easy on you!

lol
[/quote]

[quote author=DarkendSoul link=topic=121447.msg499449#msg499449 date=1168224467]
Remember to make your post as solved.

&&

A coding war is it? We'll see.
[/quote]

lol!
Thanks for the help all and I almost forgot to 'solve topic' :P
Link to comment
https://forums.phpfreaks.com/topic/33280-solved-explode-help/#findComment-155497
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.