简体   繁体   中英

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:

$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, ", "); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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