繁体   English   中英

在数组的第一个$ key上插入特定值

[英]Insert specific value on first $key in array

这确实使我精疲力尽:

if(isset($_POST['does_what'])){
    $strings = array();
    foreach($_POST['does_what'] as $key => $value){
        if($value[$key] == 0){
            $strings[0] = "This is $value";
        }
        $strings[] = $value;
    }
}

这给了我错误: PHP Notice: Uninitialized string offset: 0

我试图在数组的第一个键上插入“一些额外的”文本。 并且其他键应该插入。

使用array_unshift

if(!empty($_POST['does_what']) && is_array($_POST['does_what'])){
    $strings = array();
    foreach($_POST['does_what'] as $key => $value){
        if($value[$key] == 0){
            $text = "This is ".$value
            $value = array_unshift($strings[$key], $text );
        }
        else{
            echo "Value is not a Zero";
        }
        $strings[] = $value;
    }
}
else{
    echo "Post is empty Or its not an array";
}

array_unshift示例

我想您想要更多类似这样的东西:

foreach($_POST as $key => $value){
    if (count( $strings) == 0)
        $strings[] = "This is $value";
    else
        $strings[] = $value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM