简体   繁体   English

将关联数组转换为其在 php 中的简单值

[英]Convert an associative array to a simple of its values in php

I would like to convert the array:我想转换数组:

Array
(
    [0] => Array
        (
            [send_to] => 9891616884
        )

    [1] => Array
        (
            [send_to] => 9891616884
        )

)

to

$value = 9891616884, 9891616884

Try this:尝试这个:

//example array
$array = array(
    array('send_to'=>3243423434),
    array('send_to'=>11111111)
);

$value = implode(', ',array_column($array, 'send_to'));

echo $value; //prints "3243423434, 11111111"

You can use array_map:您可以使用 array_map:

$input = array(
  array(
    'send_to' => '9891616884'
  ),
  array(
    'send_to' => '9891616884'
  )
);

echo implode(', ', array_map(function ($entry) {
  return $entry['tag_name'];
}, $input));

Quite simple, try this:很简单,试试这个:

// initialize and empty string    
$str = '';  

// Loop through each array index
foreach ($array as $arr) {
   $str .= $arr["send_to"] . ", ";
}

//removes the final comma and whitespace
$str = trim($str, ", "); 

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

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