替换foreach所需的数组函数

时间:2014-04-18 09:03:56

标签: php foreach

我希望通过使用任何php函数来获取验证消息,而不是使用foreach。 这是我的演示代码

Array
(
    [SeriesConcateCharacter] => Array
        (
            [0] => Please enter only spaecial characters.
        )

    [NumberPaddingCharacter] => Array
        (
            [0] => You cannot enter more than 1 character.
        )

)

我想直接

  

[0] =>请只输入spaecial字符。和
  [0] =>您不能输入超过1个字符。

使用php函数。所以请建议我适当的解决方案。

2 个答案:

答案 0 :(得分:0)

正如Mark提到的那样,不能具有相同的数组索引。使用array_walk()获取输出,如图所示。

$new_arr = array();
array_walk($arr,function ($v,$k) use (&$new_arr) { $new_arr[]=$v[0];} );
print_r($new_arr);

<强> OUTPUT :

Array
(
    [0] => Please enter only spaecial characters.
    [1] => You cannot enter more than 1 character.
)

Working Demo

答案 1 :(得分:0)

我认为这会对你有帮助............

 //declare your array
    $array=array('SeriesConcateCharacter'=>array('Please enter only spaecial characters.'),
                'NumberPaddingCharacter'=>array('You cannot enter more than 1 character.'));

    //this function will remove all your unwanted keys
    $array1=array_values($array);
    foreach($array1 as $val){
        echo "<pre>"; print_r($val);
        //Array
        //(
        //    [0] => Please enter only spaecial characters.
        //)

        //Array
        //(
        //    [0] => You cannot enter more than 1 character.
        //)


    }