Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php arrays

Options
  • 16-06-2007 7:31pm
    #1
    Registered Users Posts: 4,475 ✭✭✭


    I'm writing a function and I want to make it as generic as possible. The function accepts an array as one of its parameters, but I want to do different things based on whether the array has implicit keys or not.
    array1 = array ("adams", "flynn", "smith");
    array2 = array (99 => "adams", 103 => "flynn", 441 => "smith");
    
    should react differently inside the function.

    My problem is that array_keys() is always going to be fine, as php appears to think that the first array is actually more like:
    array1 = array (0 => "adams", 1 => "flynn", 2 => "smith");
    

    Is there any way of determining that an array is using this form of key generation (ie generated by php rather than my code). Note that it could happen that array 2 would be:
    array2 = array (0 => "adams", 1 => "flynn", 2 => "smith");
    
    also so I can't check for the key() value.


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Use a composite key. Something like:
    [PHP]$array2 = array ("id99" => "adams", "id103" => "flynn", "id441" => "smith");
    foreach ($array2 as $k => $v) {
    if (is_numeric($k)) {
    echo "$k => $v (assigned by PHP)<br>";
    } else {
    echo ((int) substr($k, 2))." => $v (assigned by your application)<br>";
    }
    }[/PHP]
    This would differentiate between arrays generated automatically by PHP and those assigned specifically by you.

    Another, probably easier, option is store your custom keys as negative values:
    [PHP]$array2 = array (-99 => "adams", -103 => "flynn", -441 => "smith");
    foreach ($array2 as $k => $v) {
    if ($k > 0) {
    echo "$k => $v (assigned by PHP)<br>";
    } else {
    echo abs($k)." => $v (assigned by your application)<br>";
    }
    }[/PHP]
    Only problem with this option is where you have a zero key value, so you may be better off using the first option.


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Nice ideas, Corinthian. Since the keys are going to be database driven, the negative value would be the most feasible, but I don't understand your issue about 0 values


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    corblimey wrote:
    I don't understand your issue about 0 values
    Zero may denote either a key assigned by your application or by PHP - so, given that -0 is the same as 0, the confusion would remain in that case if using the negatives solution.

    Of course, if memory serves me correctly, you can specify the key range or, at least, whether PHP starts at 0 or 1 when assigning keys which could solve this issue, but you'll have to look that one up in the manual yourself ;)


Advertisement