AikenDrum Posted November 5, 2009 Share Posted November 5, 2009 Hi There, relatively new to php so please bear with me - When I post some data to the second page - I get the following output form $_POST: Array ( [market] => 44 [question568] => 2 [question572] => 1 [question569] => 5 ) I need to be able to parse only the questions (no idea how questions many in advance) and create a string like this : 568=2&572=1&569=5 Amy help would be greatly appreciated Many kind thanks in advance AikenD Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/ Share on other sites More sharing options...
trq Posted November 5, 2009 Share Posted November 5, 2009 $out = ''; foreach ($array as $k => $v) { if (substr($k,7) == 'question') { $out .= "$k=$v"; } } echo $out; Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951686 Share on other sites More sharing options...
Mchl Posted November 5, 2009 Share Posted November 5, 2009 Adding & in the mix: $out = ''; foreach ($array as $k => $v) { if (substr($k,7) == 'question') { $out .= "$k=$v&"; } } $out = substr(0,-1); echo $out; Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951687 Share on other sites More sharing options...
AikenDrum Posted November 5, 2009 Author Share Posted November 5, 2009 Hi there - many thanks for the very fast response - this code returns a 0 $out = ''; foreach ($_POST as $k => $v) { if (substr($k,7) == 'question') { $out .= "$k=$v&"; } } $out = substr(0,-1); echo $out; I changed the 7 to 8 but that did not make a difference either ? Kindest Aiken Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951693 Share on other sites More sharing options...
Mchl Posted November 5, 2009 Share Posted November 5, 2009 My fault $out = ''; foreach ($_POST as $k => $v) { if (substr($k,7) == 'question') { $out .= "$k=$v&"; } } $out = substr($out, 0,-1); echo $out; Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951694 Share on other sites More sharing options...
AikenDrum Posted November 5, 2009 Author Share Posted November 5, 2009 Hi there, I am still getting no response back - not even a zero now :-( I again tried replacing 7 with 8 (I think that's correct) $out just comes back empty Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951696 Share on other sites More sharing options...
Mchl Posted November 5, 2009 Share Posted November 5, 2009 Try this. Should work now. $out = ''; foreach ($_POST as $k => $v) { if (substr($k,0, == 'question') { $out .= "$k=$v&"; } } $out = substr($out, 0,-1); echo $out; Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951701 Share on other sites More sharing options...
AikenDrum Posted November 5, 2009 Author Share Posted November 5, 2009 Bingo - many thanks indeed - I had my own version worked out - but this is better - AikenD Link to comment https://forums.phpfreaks.com/topic/180397-need-to-extract-certain-values-from-array/#findComment-951708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.