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, ", ");
Related
PHP seperate number using based on 2 delimiter
I have this variable $sid
$sid = isset($_POST['sid']) ? $_POST['sid'] : '';
and it's output is:
Array
(
[0] => 4|1
[1] => 5|2,3
)
Now I want to get the 1, 2, 3 as value so that I can run a query based on this number as ID.
How can I do this?
Use explode()
$arr = array(
0 => "4|1",
1=> "5|2,3"
);
$output = array();
foreach($arr as $a){
$right = explode("|",$a);
$rightPart = explode(",",$right[1]);
foreach($rightPart as $rp){
array_push($output,$rp);
}
}
var_dump($output);
Use foreach to loop thru your array. You can explode each string to convert it to an array. Use only the 2nd element of the array to merge
$sid = array('4|1','5|2,3');
$result = array();
foreach( $sid as $val ) {
$val = explode('|', $val, 2);
$result = array_merge( $result, explode(',', $val[1]) );
}
echo "<pre>";
print_r( $result );
echo "</pre>";
You can also use array_reduce
$sid = array('4|1','5|2,3');
$result = array_reduce($sid, function($c, $v){
$v = explode('|', $v, 2);
return array_merge($c, explode(',', $v[1]));
}, array());
These will result to:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
You can do 2 explode with loop to accomplish it.
foreach($sid as $a){
$data = explode("|", $a)[1];
foreach(explode(",", $data) as $your_id){
//do you stuff with your id, for example
echo $your_id . '<br/>';
}
}
I have an array that looks like this:
$array = array (
[level_1] => array (
[level_2] => array (
[level_3] => something
)
),
[level_12] => array (
[level_2] => somethingelse
),
[level_13] => array (
[level_22] => array (
[level_3] => something
)
),
);
The keys or values aren't always unique but the branches are.
And I have a string that looks like this:
$string = 'level_1-level_2-level_3';
Those are the keys for a branch.
And I need to somehow get the value from the array based on that string?
Like this:
$string_array = explode('-', $string);
$array[$string_array[0]][$string_array[1]][$string_array[2]] // something
But since the depth can be different this is not a viable solution...
Try this simple example, no need for a recursive function:
function get_item( $path, $array )
{
$paths = explode( '-', $path );
$result = $array;
foreach ( $paths as $path) {
isset( $result[$path] ) ? $result = $result[$path] : $result = false;
}
return $result;
}
$path = 'level_1-level_2-level_3';
echo get_item( $path, $array );
Try this:
$array = array (
'level_1' => array (
'level_2' => array (
'level_3' => 'something'
)
),
'level_12' => array (
'level_2' => 'somethingelse'
),
'level_13' => array (
'level_22' => array (
'level_3' => 'something'
)
),
);
$string = 'level_1-level_2-level_3';
$keys = explode('-', $string);
echo getItemIterative($keys, $array);
echo "\n";
echo getItemRecursive($keys, $array);
function getItemIterative($keys, $array)
{
$value = null;
foreach ($keys as $key) {
if ($value == null) {
$value = $array[$key];
}
if (is_array($value) && array_key_exists($key, $value)) {
$value = $value[$key];
}
}
return $value;
}
function getItemRecursive($keys, $array)
{
$key = array_shift($keys);
$value = $array[$key];
if (empty($keys)) {
return $value;
} else {
return getItemRecursive($keys, $value);
}
}
Make a $result variable which initially points to the root of the array, and loop through the levels of your $string_array 'til $result points at the leaf you were looking for.
// stuff you already have:
$array = array(...); // your big array
$string = 'level_1-level_2-level_3';
$string_array = explode('-', $string);
// new stuff:
$result = $array;
foreach ($string_array as $level) {
$result = $result[$level];
}
echo $result; // 'something'
Working example: Ideone
I have an array like:
Array
(
[0] => Array
(
[kanal] => TV3+
[image] => 3Plus-Logo-v2.png
)
[1] => Array
(
[kanal] => 6\'eren
[image] => 6-eren.png
)
[2] => Array
(
[kanal] => 5\'eren
[image] => 5-eren.png
)
)
It may expand to several more subarrays.
How can I make a list like: TV3+, 6'eren and 5'eren?
As array could potentially be to further depths, you would be best off using a recursive function such as array_walk_recursive().
$result = array();
array_walk_recursive($inputArray, function($item, $key) use (&$result) {
array_push($result, $item['kanal']);
}
To then convert to a comma separated string with 'and' separating the last two items
$lastItem = array_pop($result);
$string = implode(',', $result);
$string .= ' and ' . $lastItem;
Took some time but here we go,
$arr = array(array("kanal" => "TV3+"),array("kanal" => "5\'eren"),array("kanal" => "6\'eren"));
$arr = array_map(function($el){ return $el['kanal']; }, $arr);
$last = array_pop($arr);
echo $str = implode(', ',$arr) . " and ".$last;
DEMO.
Here you go ,
$myarray = array(
array(
'kanal' => 'TV3+',
'image' => '3Plus-Logo-v2.png'
),
array(
'kanal' => '6\'eren',
'image' => '6-eren.png'
),
array(
'kanal' => '5\'eren',
'image' => '5-eren.png'
),
);
foreach($myarray as $array){
$result_array[] = $array['kanal'];
}
$implode = implode(',',$result_array);
$keyword = preg_replace('/,([^,]*)$/', ' & \1', $implode);
echo $keyword;
if you simply pass in the given array to implode() function,you can't get even the value of the subarray.
see this example
assuming your array name $arr,codes are below
$length = sizeof ( $arr );
$out = '';
for($i = 0; $i < $length - 1; $i ++) {
$out .= $arr [$i] ['kanal'] . ', ';
}
$out .= ' and ' . $arr [$length - 1] ['kanal'];
I think it would work to you:
$data = array(
0 =>['kanal' => 'TV1+'],
1 =>['kanal' => 'TV2+'],
2 =>['kanal' => 'TV3+'],
);
$output = '';
$size = sizeof($data)-1;
for($i=0; $i<=$size; $i++) {
$output .= ($size == $i && $i>=2) ? ' and ' : '';
$output .= $data[$i]['kanal'];
$output .= ($i<$size-1) ? ', ' : '';
}
echo $output;
//if one chanel:
// TV1
//if two chanel:
// TV1 and TV2
//if three chanel:
// TV1, TV2 and TV3
//if mote than three chanel:
// TV1, TV2, TV3, ... TV(N-1) and TV(N)
$last = array_slice($array, -1);
$first = join(', ', array_slice($array, 0, -1));
$both = array_filter(array_merge(array($first), $last));
echo join(' and ', $both);
Code is "stolen" from here: Implode array with ", " and add "and " before last item
<?php
foreach($array as $arr)
{
echo ", ".$arr['kanal'];
}
?>
I have a string ($c) that contains a comma-separated list of settings. Some settings are stand-alone, while others require a sub-setting that is separated by an equal sign.
Example:
$c = "title,author,tax=taxonomy_A,tax=taxonomy_B,date";
I need to parse this string into a multidimensional array ($columns) that outputs like this:
$columns = array(
[0] => array( 'title', '' ),
[1] => array( 'author', '' ),
[2] => array( 'tax', 'taxonomy_A' ),
[3] => array( 'tax', 'taxonomy_B' ),
[4] => array( 'date', '' )
)
I have tried this:
$c = "title,author,tax=taxonomy_A,tax=taxonomy_B,meta=custom_field_key";
$c = explode( ',', $c );
$columns = array( explode( '=', $c ) );
But that just returns this:
$columns = array(
[0] => array( 'Array' )
)
What am I missing here? Should the second explode be replaced with a different function?
Thanks in advance!
foreach(explode(',', "title,author,tax=taxonomy_A,tax=taxonomy_B,date") as $item)
$items[] = array_pad(explode('=', $item), 2, '');
You'll need to loop after the first explode to do the second one:
$c = "title,author,tax=taxonomy_A,tax=taxonomy_B,meta=custom_field_key";
$arr = explode( ',', $c );
$result = array();
foreach($arr as $a) {
array_push($result, explode('=', $a));
}
print_r($result);
In your code, you don't apply the second explode to each element of the array, but just once to the whole array object. This causes the array to be transformed into the string "Array", which is then run through explode() and results in your output.
Instead you could use array_map() (PHP docu):
function filterArr( $value ) {
return explode( '=', $value )
}
$c = "title,author,tax=taxonomy_A,tax=taxonomy_B,meta=custom_field_key";
$c = explode( ',', $c );
$c = array_map( "filterArr", $c );
I have multiple strings similar to:
$str = "/One/Two";
$str2 = "/One/Two/Flowers";
$str3 = "/One/Two/Grass";
$str4 = "/One/Another/Deeper";
$str5 = "/Uno/Dos/Cow";
I want to split it into a deep nested array that looks similar to the following:
Array
(
[One] => Array
(
[Two] => Array
(
[Flowers] =>
[Grass] =>
)
[Another] => Array
(
[Deeper] =>
)
)
[Uno] => Array
(
[Dos] => Array
(
[Cow] =>
)
)
)
This should do it:
$strings = array(
"/One/Two",
"/One/Two/Flowers",
"/One/Two/Grass",
"/One/Another/Deeper",
"/Uno/Dos/Cow"
);
$result = array();
foreach($strings as $string) {
$parts = array_filter(explode('/', $string));
$ref = &$result;
foreach($parts as $p) {
if(!isset($ref[$p])) {
$ref[$p] = array();
}
$ref = &$ref[$p];
}
$ref = null;
}
print_r($result);
Working example:
http://codepad.org/GmAoXLXp
Something like this should work. I couldn't think of any nice functional way to build the structure, so I fell back to a couple foreach loops.
<?php
$strings = array(
'/One/Two',
'/One/Two/Flowers',
'/One/Two/Grass',
'/One/Another/Deeper',
'/Uno/Dos/Cow'
);
$paths = array_map(
function ($e) {
return explode('/', trim($e, '/'));
},
$strings
);
$pathStructure = array();
foreach ($paths as $path) {
$ref =& $pathStructure;
foreach ($path as $dir) {
$ref =& $ref[$dir];
}
}
unset($ref);
print_r($pathStructure);