如何将多维数组转换为单个变量?

时间:2016-09-12 12:37:33

标签: php

问题我云没有得到数组值并分配给某个变量。我想向一个以上的数字发送一个msg,来自DB的数字。这是我的数组来保存所有的暴徒号码。现在问题是我想从这个数组发送msg所有数字。

Array
(
    [0] => Array
        (
            [mobile] => 9944176261
        )

    [1] => Array
        (
            [mobile] => 9994444783
        )

    [2] => Array
        (
            [mobile] => 9944176261
        )

)
,Array
(
    [0] => Array
        (
            [mob_no] => 9944176262
        )

    [1] => Array
        (
            [mob_no] => 9944176263
        )

    [2] => Array
        (
            [mob_no] => 9944176265
        )

)

这是我的msg使用SMS网关发送代码。我想将所有数字分配给receipientno变量。基于此receipientno值发送消息。

//msg code for customers
        $ch = curl_init();
        $user="username:pwd";
        $receipientno=$_POST['check_list'];
        $receipientno = implode(',', $receipientno);        
        $senderID="TEST SMS"; 
        $msgtxt=$_POST['message'];
        curl_setopt($ch,CURLOPT_URL,  "http://api.mVaayoo.com/mvaayooapi/MessageCompose");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&senderID=$senderID&receipientno=$receipientno&msgtxt=$msgtxt");
        $buffer = curl_exec($ch);
        if(empty ($buffer))
        { echo " buffer is empty "; }
        else
        { echo $buffer; } 
        curl_close($ch);

2 个答案:

答案 0 :(得分:0)

假设您的POST阵列以这种方式构建:

$arr = array(
array(
    0 => array
        (
            'mobile' => 9944176261
        ),
    1 => array
        (
            'mobile' => 9994444783
        ),
    2 => array
        (
            'mobile' => 9944176261
        )

),
array
(
0 => array
    (
        'mob_no' => 9944176262
    ),
1 => array
    (
        'mob_no' => 9944176263
    ),

2 => array
    (
        'mob_no' => 9944176265
    )
)
)
;

您可以使用此代码段获取所有mob_nomobile索引的逗号分隔列表。

$receipientsNo = "";
foreach($arr as $row){
$recipientsNo .= ",". implode(',', array_map(function ($input) {
  if(isset($input['mobile']))
    return $input['mobile'];
  elseif (isset($input['mob_no']))
     return $input['mob_no'];
}, $row));
}

$recipientsNo = ltrim($recipientsNo,",");

<强>更新 您需要更新//msg code for customers代码,如下所示:

$ch = curl_init();
$user="username:pwd";

$receipientsNo = "";
foreach($_POST['check_list'] as $row){
    $receipientsNo .= ",". implode(',', array_map(function ($input) {
      if(isset($input['mobile']))
        return $input['mobile'];
      elseif (isset($input['mob_no']))
         return $input['mob_no'];
    }, $row));
}

$receipientsNo = ltrim($receipientsNo,",");

$senderID="TEST SMS"; 
$msgtxt=$_POST['message'];
curl_setopt($ch,CURLOPT_URL,  "http://api.mVaayoo.com/mvaayooapi/MessageCompose");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$user&senderID=$senderID&receipientno=$receipientsNo&msgtxt=$msgtxt");
$buffer = curl_exec($ch);
if(empty ($buffer))
{ echo " buffer is empty "; }
else
{ echo $buffer; } 
curl_close($ch);

答案 1 :(得分:0)

试试这个

        <?php

    $receipientno='';

    $ss = array(
    array(
        0 => array
            (
                'mobile' => 9944176261
            ),
        1 => array
            (
                'mobile' => 9994444783
            ),
        2 => array
            (
                'mobile' => 9944176261
            )

    ),
    array
    (
    0 => array
        (
            'mob_no' => 9944176262
        ),
    1 => array
        (
            'mob_no' => 9944176263
        ),

    2 => array
        (
            'mob_no' => 9944176265
        )
    )
    )
    ;


    foreach($ss as $row ) {

        if(isset($row[0]['mobile']))
        {
             $receipientno.= implode(',',array_column($row,'mobile')); 


        }
        else if(isset($row[0]['mob_no']))
        {
             $receipientno.= implode(',',array_column($row,'mob_no')); 
        }

    }

    echo $receipientno;

更新1:

        foreach($ss as $row ) {


        foreach($row as $new ){


        if(isset($new['mobile']))
        $receipientno.=$new['mobile'].",";

        else if(isset($new['mob_no']))
            $receipientno.=$new['mob_no'].",";


        }

    }

    echo rtrim($receipientno,",");

输出:

9944176261,9994444783,99441762619944176262,9944176263,9944176265