Having trouble with strpos(), how to return true?
Posted: Thu Oct 14, 2010 5:49 pm
I don't know if I'm going about this the right way. If there is a better way to deal with this, I'd appreciate that information.
I'm retrieving the value of $_POST, which includes a series of elements whose names start with "promo_id_" and are followed by an integer. The integers will always vary. There are also several other elements in $_POST, but right now they're irrelevant to my problem... except that I can't process the whole $_POST array since they're in there and I don't need them for this particular task.
I'd like to place all the elements whose keys start with promo_id_ into a separate array to use later. I noted that php.net indicates that strpos() can return a deceptive value that evaluates to false and that === should be used... but I think I'm doing it wrong.
So I thought I could do it like this (the print statements are part of my attempts to make it work):
So if $_POST originally included:
What I wanted to get out of it in $new_data was:
The number attached to the promo_id is pulled from the primary key in a MySQL table and will always be unique, so that isn't a concern.
But instead I just get nothing--the if statement never returns true because $in_key is always NULL (not zero like I thought it would be), so the commands in it never happen.
I'm sure I'm just missing something really obvious, but I'm at a loss. Any suggestions for how to make my code work or recommendations for a different method that would accomplish what I am trying to do would be greatly appreciated.
I'm retrieving the value of $_POST, which includes a series of elements whose names start with "promo_id_" and are followed by an integer. The integers will always vary. There are also several other elements in $_POST, but right now they're irrelevant to my problem... except that I can't process the whole $_POST array since they're in there and I don't need them for this particular task.
I'd like to place all the elements whose keys start with promo_id_ into a separate array to use later. I noted that php.net indicates that strpos() can return a deceptive value that evaluates to false and that === should be used... but I think I'm doing it wrong.
So I thought I could do it like this (the print statements are part of my attempts to make it work):
Code: Select all
$new_data = Array();
foreach ($_POST as $key => $sort_order) {
// What is the value in key?
$key_value = $key;
print "Key value is: " . $key_value . "<br />";
// Does it include the text "promo_id_"?
$in_key = strpos($searching_for,$key_value);
print "in_key value is: " . $in_key . "<br />";
// Check if the value is zero
if ($in_key === "0") {
// If we find it, strip it out and save the
// remaining number to $promo_id
$promo_id = str_replace('promo_id_','',$key_value);
}
// Add the data to the new array
$new_data[$promo_id] = $sort_order
}
Code: Select all
[promo_id_26] => 1
[promo_id_16] => 5
[promo_id_17] => 8
[promo_id_20] => 1
Code: Select all
[26] => 1
[16] => 5
[17] => 8
[20] => 1
But instead I just get nothing--the if statement never returns true because $in_key is always NULL (not zero like I thought it would be), so the commands in it never happen.
I'm sure I'm just missing something really obvious, but I'm at a loss. Any suggestions for how to make my code work or recommendations for a different method that would accomplish what I am trying to do would be greatly appreciated.