2

I am trying to create 2 new arrays out of one existing array ($array), using the following "foreach" loop. However I am not sure it is correct:

        $emails = array();
        $numbers = array();
        while($array){
            $entry = $array['entry1'];
            $number = number($entry);
            if(isset($number) && (strlen($number) > 9)){
                $numbers[] = array('entry1' => $entry, 'number' => $number);
            }
            else{
                $email = email($entry);
                $emails[] = array('entry1' => $entry, 'email' => $email);
            }
        }

should the internal arrays have []? do I even need to start the arrays outside of the while loop? or skip it? is it better to use a foreach loop?

Update:

Okay, here is the original array: It is extracted from a mysql query, of sets of two numbers:

{('uid1','uid2'),('uid1','uid5'),('uid9','uid93'),....) There might be other data in each row, but these are the only two data points that really matter.

What I am trying to do is for a specific user ($entry), create two separate arrays: of all the users that have numbers (that's a function we have), and all the rest - of their emails. So the outcome will be 2 new arrays which will look like this: for a specific uid79887:

numbers array: {('uid8','xxx-xxxx-xxx'),('uid34','yyy-yyyy-yyy'),('uid654','vvv-vvvv-vvv')}

emails array: {('uid4','[email protected]'),('uid1','[email protected]'),('uid55554','[email protected]')}

4
  • what do you want to happen in making the array? Commented Feb 28, 2012 at 22:55
  • Could you post a var_dump of your original array, and how do you want the new arrays to look like? Commented Feb 28, 2012 at 22:56
  • You definitively shouldn't use an array-structure which requires you to use keys like "entry1", "entry2" etc. An array is either a list (with numbers as keys such as 0, 1, 2, 3...) or associative where the keys are names of elements (for example 'name', 'url', 'email' or whatever). Commented Feb 28, 2012 at 22:57
  • This will be an infinite while loop. Commented Feb 28, 2012 at 22:59

3 Answers 3

3

Few things first:

  • It's good practice to initialize your variables, just do it (it has many positives).
  • What kind of test is while($array)? You should use foreach( $array as $entry) or while( count( $array)) if you're removing items from array.
  • Why are you testing isset( $number) when it's always set? It's initialized variable. You're probably checking null, so use !is_null() or ($number !== null). Even if it works it's misleading.

I guess your code should look like this:

$emails = array();
$numbers = array();
foreach( $array as $entry){
    $entry = isset( $entry['entry1']) ? $entry['entry1'] : null;
    $number = number( $entry);
    if( strlen($number) > 9 ){ // If $number is empty it will have strlen < 1 .)
       $numbers[] = array('entry1' => $entry, 'number' => $number);
    } else {
       $emails[] = array('entry1' => $entry, 'email' => email( $entry));
    }
}
10
  • well, we have a pretty similar answer ^^ Commented Feb 28, 2012 at 23:11
  • good call about the"foreach", that's wht I had there first, donno why I changed it. secondly, 'number' is NOT always set. this is exactly what I am checking - if number is set I want to take this entry and include it in one array. if not - I want to take this entry and include it in the other array. Commented Feb 28, 2012 at 23:19
  • @LucyWeatherford $number = function_call(); even if it doesn't return anything var_dump($number) will show null not unset or something like that. Anyway, what's not working on provided code? Commented Feb 28, 2012 at 23:22
  • how do I know whether $entry is $array['entry']? Commented Feb 28, 2012 at 23:52
  • did you skip that line on purpose? is it not necessary? thanks! Commented Feb 28, 2012 at 23:54
1

I guess this is what you are trying to acheive:

$emails = $numbers = Array();
foreach($array as $item) {
  $e = $item['entry1'];
  $number = number($e);
  if(strlen($number) > 9) {
    $numbers[] = Array('entry1' => $e, 'number' => $number);
  }
  else {
    $email = email($entry);
    $emails[] = Array('entry1' => $e, 'email' => $email);
  }
}

in your code, while($array) do not loop on the array, it loop until $array == false as $array do not change in your loop it will either never enter or the loop, or never exit generally, using a foreach loop produce code easier to understand

2
  • do you mean $item['entry1'];? also, $array is extracted from a mysql query, any ideas what I should add/change to make it from a resource ID into an array here? thanks. Commented Feb 29, 2012 at 19:20
  • what do you mean by "extracted from a mysql query" can you show the line where $array is assigned a value? or the output of var_dump($array); Commented Feb 29, 2012 at 23:17
0

Assuming this isn't some kind of homework assignment, why don't you do it this way:

$emails = array();
$numbers = array();

foreach( $array as $entry )
{
    $number = number($entry);
    if( $number && strlen($number) > 9 )
    {
        array_push($numbers, array('entry1' => $entry, 'number' => $number));
    }
    else
    {
        array_push($emails, array('entry1' => $entry, 'email' => email($entry)));
    }
}

It is better to use built in functions that trying to roll your own. The foreach() function works very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.