使用PHP数组和子数组生成字符串

时间:2015-11-24 15:45:46

标签: php arrays string

修改

我退出野生动物园并找到了这个网页:http://www.homeandlearn.co.uk/php/php6p5.html

他们提出了不同风格的数组(下图),他们的输出看起来与我想要的类似。我发现我 能够在foreach中获得名字和姓氏作为单个变量!

$full_name = array( );

$full_name["David"] = "Gilmour";
$full_name["Nick"] = "Mason";
$full_name["Roger"] = "Waters";
$full_name[""] = "";
$full_name["Richard"] = "Wright";

foreach ($full_name as $firstName => $lastName) {
    if ($firstName != '') {

        $fn = $firstName;
        $ln = $lastName;

        $sn_display = $firstName . ' ' . $lastName;
        $sn_slug = $firstName . $lastName;

    echo '| '.$sn_display.' ';
    }
}
echo ' |';

然后我意识到如何使用类似于我之前提交的数组语法:

$submissions = array (  
    array(
        'Jim' => 'Jones' , 
    ),
    array(
        'Tina' => 'Tim' , 
    ),
    array(
        '' => '' , 
    ),
    array(
        'Lazer' => 'Lisa' , 
    )
);

foreach ($submissions as $full_name) {
    foreach ($full_name as $firstName => $lastName) {
        if ($firstName != '') {

            $fn = $firstName;
            $ln = $lastName;

            $sn_display = $firstName . ' ' . $lastName;
            $sn_slug = $firstName . $lastName;

            echo '| '.$sn_display.' ';
        }
    }
}
echo ' |';

两者都给我有用的输出:

| Jim Jones | Tina Tim | Lazer Lisa  |

我仍然很好奇 - 有更优雅的方式吗?

ORIGINAL

我正在尝试使用这样的数组,这将通过WordPress Gravity Forms提交填充:

$submissions = array (  
    array( 'fn' => 'Jim' , 'ln' => 'Jones' ),
    array( 'fn' => 'Tim' , 'ln' => 'Tomb' ),
    array( 'fn' => '' ,  'ln' => '' ),
    array( 'fn' => 'Lisa' , 'ln' => 'Lazer' )
);

实现了几个目标

  1. 表单可能有空提交 - 必须将其丢弃
  2. 输出必须作为字符串使用 - 我需要构建用户ID /用字符串填写用户元数据
  3. 使用以下内容,我可以输出数据:

    foreach($submissions as $submit) {   //break down the array
        if ($submit != '') {                 //check for empties
            foreach($submit as $key => $value){  //cycle through each subarray
    
                echo $value. ' ';                    //get the info - the problem: I cant isolate the values as strings
    
            }
        }
    }
    

    我的输出没用: | Jim | Jones | Tim | Tomb | Lisa | Lazer |

    特别是因为我想为每个用户激活一个使用$value的函数 - 而不是每个用户的姓名!

    如何在这个循环中将名字和姓氏作为字符串?如果我可以将第一个和最后一个作为字符串,我可以在需要时将它们连接起来,并在我想要的时候在元数据中使用它们。

    foreach($submissions as $submit) {   //break down the array
        if (implode('', $submit) != '') {    //check for empties
            foreach($submit as $key => $value){  //cycle through each subarray
    
                $fn = $value?Looking for this code?; //I want to make strings!
                $ln = $value?Looking for this code?; //One for each value.
    
                $display_name = $fn . ' ' . $ln;     // With strings I can make stuff
                $name_slug = $fn . $ln;
    
    /* More functions will happen as we cycle through each 
    submission that has user data until all the user data submissions
    have been handled */
    
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您正在尝试使用$ value [0]获取$ value字符串的第一部分。 试试吧:

$output .= ' | ' . $key . ' - ' . $value;

而不是

回声'| ”。$值[0]。 '';

并在最后回显$ output。