PHP的 - 创建assciative阵列while循环 [英] php - create assciative array while loop

查看:124
本文介绍了PHP的 - 创建assciative阵列while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个现有的数组($数组)创建2个新的数组,使用下面的foreach循环。但是我不确定是否正确:

$ $ $ $ $ $ c $ $ 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);






$ b

如果内部数组有 []
我甚至需要在while循环之外启动数组吗?或跳过?
最好使用foreach循环吗?

更新:

好的,这里是原始数组:它是从一个mysql查询中提取的两组数字:
$ b $ p $ {$ uid1','uid2'),( 'uid1','uid5'),('uid9','uid93'),....)
每行中可能还有其他数据,但这些是唯一的两个数据点真的很重要。

我想要做的是针对特定用户($ entry),创建两个独立的数组:所有具有数字的用户这是我们有的功能),以及其他所有的电子邮件。
所以结果将是2个新的数组,如下所示:
为特定的 uid79887


$ b $('uid8','xxx-xxxx-xxx'),('uid34','yyy-yyyy-yyy'),('uid654','vvv -vvvv-vvv')}



emails array:{('uid4','mmm@mmm.com '),('uid1','llll@lll.com'),('uid55554','ppp@ppp.com')}




  • 初始化变量是一个很好的做法,只要做到这一点(它有很多好处)。
  • 什么样的测试是 while($ array)?你应该使用 foreach($ array as $ entry) while(count($ array))从数组中删除项目。
  • 为什么在总是设置时测试 isset($ number)?它是初始化的变量。您可能正在检查 null ,因此请使用!is_null()($ number! == null)。即使它起作用,也是误导性的。


  •   $ emails = array(); 
    $ numbers = array();
    foreach($ array为$ entry){
    $ entry = isset($ entry ['entry1'])? $ entry ['entry1']:null;
    $ number = number($ entry);
    if(strlen($ number)> 9){//如果$ number是空的,它将有strlen< 1)
    $ numbers [] = array('entry1'=> $ entry,'number'=> $ number);
    } else {
    $ emails [] = array('entry1'=> $ entry,'email'=> email($ entry));
    }
    }


    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','mmm@mmm.com'),('uid1','lll@lll.com'),('uid55554','ppp@ppp.com')}

    解决方案

    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));
        }
    }
    

    这篇关于PHP的 - 创建assciative阵列while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆