How can I implode() only one column from a multidimensional array? [duplicate] - php

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have array in following format:
Array
(
[sales] => Array
(
[0] => Array
(
[0] => 1
[1] => 6
)
[1] => Array
(
[0] => 2
[1] => 8
)
[2] => Array
(
[0] => 3
[1] => 25
)
[3] => Array
(
[0] => 4
[1] => 34
)
)
)
Using:
foreach ($data['sales'] as $k => $row) {
$list = implode(",",$row);
}
I get the following as output:
1,62,83,254,34
But I only need the second values from each subArray. The expected result needs to be:
6,8,25,34
How can I remove the first set of values?

Just grab the first column from your array with array_column(), so that you end up with an array, e.g.
Array (
[0] => 6
[1] => 8
[2] => 25
[3] => 34
)
And implode() it then as you already did, e.g.
echo implode(",", array_column($data["sales"], 1));
output:
6,8,25,34

I like array_column() but if you don't have PHP >= 5.5.0:
$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));
Or with the foreach:
foreach ($data['sales'] as $row) {
$list[] = $row[1];
}
$list = implode(',', $list);

Related

PHP add values from multidimensional array into one array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 months ago.
If I have an array $array like this:
Array (
[0] => Array (
[id] => 11
[name] => scifi
)
[1] => Array (
[id] => 12
[name] => documetary
)
[2] => Array (
[id] => 10
[name] => comedy
)
)
How could I turn it into simply:
Array ( 11, 12, 10 ) with no key value pairs.
I am trying to extract only the id from each array and add them into 1 array. I am trying it with a foreach;
$ids = [];
if ( $array ) {
foreach ( $array as $item ) {
$ids[] = $term->id;
}
}
print_r($ids);
It just returns 3 empty arrays Array ( [0] => [1] => [2] => )
Using a loop, you can do this:
$arr1 = [
['id'=>11,'name'=>'scifi'],
['id'=>12,'name'=>'documentry'],
['id'=>10,'name'=>'comedy'],
];
$arr2 = [];
foreach($arr1 as $internal){
array_push($arr2,$internal['id']);
}
print_r($arr2);
Here we access all internal array's ID's and insert them into a new array.

How to combine associative array by indexes [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have this associative array below:
Array
(
[0] => Array
(
[0] => Category
[1] => fruit
[2] => carbs
)
[1] => Array
(
[0] => Day 1 - Program
[1] => Eat banana
[2] => Eat bread
)
[2] => Array
(
[0] => Day 1 - record
[1] =>
[2] =>
)
)
each array index relates to the same index in the other arrays.
I need to now create 3 arrays by combining the index. The finished array would
look like this:
Array
(
[0] => Array
(
[0] => Category
[1] => Day 1 - Program
[2] => Day 1 - record
)
[1] => Array
(
[0] => fruit
[1] => Eat banana
[2] =>
)
[2] => Array
(
[0] => carbs
[1] => bread
[2] =>
)
)
The empty slots are where I know to put a textbox to record the data.
I've tried nesting for loops and other things but nothing is working.
How to combine array into a multidimensional array based on indexes?
$output = call_user_func_array(
'array_map',
array_merge(
array(NULL),
$input
)
);
Demo
You can achieve this with a nested loop. Loop through the sub-arrays first. On each iteration, loop through the elements in the sub-array, and add them into the result array. The important part here is what we use as the index for the $result array. $index will be the position of the array element in the sub-array. For example, Category would have an index of 0, so it would be pushed to $result[0][].
foreach ($array as $sub) {
foreach ($sub as $index => $val) {
$result[$index][] = $val;
}
}
print_r($result);
Demo
Here's a quick way - it essentially flips the keys. BTW, the first array you have is an indexed array, not an associative array.
$input = array(
array
(
"Category", "fruit", "carbs"
),
array
(
"Day 1 - Program","Eat banana","Eat bread"
),
array
(
"Day 1 - record", "", ""
)
);
foreach ($input as $key => $array){
foreach ($array as $k => $v){
$output[$k][$key] = $input[$key][$k];
}
}
print_r($output);

How to combine two arrays [duplicate]

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 8 years ago.
I have two arrays
Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 [5] => 11 )
Array ( [1] => Zwembad [2] => Airconditioning [3] => Telefoon [4] =>
Internet [5] => Wi-Fi [6] => TV [11] => food )
Is there an array function to combine this array to form a new array.
Result Array should be:
Array ( [0] => Zwembad [1] => Airconditioning [2] => Internet [3] => Wi-Fi [4] => 6 [5] => TV )
That is, the values of First array have been replaced by the values corresponding to the index of second array.
Why not have options?
$new = array();
$i = 0;
$ak = array_values( $array2 );
foreach ( array_keys( $array1 ) as $k )
{
$new[$k] = $ak[$i];
$i++;
}
Try with array_values
$second_array = array_values($second_array);
You can use array_combine.
$combineArray=array_combine($array1, $array2);
<?php
$a1 = array("red","green");
$a2 =array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

Set one array value as parameter to another array [duplicate]

This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 8 years ago.
Hi i have an array like this
Array
(
[0] => Array
(
[employeename] => abc
)
[1] => Array
(
[employeename] => def
)
)
Array
(
[0] => 1
[1] => 3
)
I need the second array value to be set in first array like this
Array
(
[0] => Array
(
[employeename] => abc
[othername] => 1
)
[1] => Array
(
[employeename] => def
[othername] => 3
)
)
Any Help Will be appreciated , Thanks In Advance :)
Try this :
<?php
$array1 = array(array("employeename" => "abc"),
array("employeename" => "def")
);
$array2 = array(1,3);
foreach($array1 as $key=>$val){
$array1[$key]["othername"] = $array2[$key];
}
echo "<pre>";
print_r($array1);
?>

Flatten a multidimensional array and remove duplicate values [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
How to remove duplicate values from an array in PHP
(27 answers)
Closed last year.
all
I want to remove the duplicate value from this Array
Array
(
[0] => Array
(
[0] => Ajay Patel
[1] => Tag 1
)
[1] => Array
(
[0] => Tag 1
[1] => Tag 3
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
I tried this solution from How to remove duplicate values from a multi-dimensional array in PHP
$result2 = array_map("unserialize", array_unique(array_map("serialize", $result2)));
But i think something is wrong here, i am getting this as result.
Array
(
[0] => Array
(
[0] => Ajay Patel
[1] => Tag 1
)
[1] => Array
(
[0] => Tag 1
[1] => Tag 3
)
[2] => Array
(
)
)
What i want is
Array
(
[0] => Ajay Patel
[1] => Tag 1
[2] => Tag 3
)
Tag 1 is removed because its 2 times...
$result2 = array_unique(call_user_func_array('array_merge',$result2));
In modern PHP, the same technique can be written as:
$result2 = array_unique(array_merge(...$result2));
try this
$result = array();
function merge_values(array &$array, $mixed) {
if(is_array($mixed)) {
foreach($mixed as $tags) {
merge_values($array, $tags);
}
}
else {
if(null !== $mixed && strlen($mixed) > 0 && false === array_search($mixed, $array)) {
$array[] = $mixed;
}
}
}
merge_values($result, $array);
print_r($result);
I think you should try this
function uniqueElements($outerArray){
$result=array();
foreach ($outerArray as $innerArray){
$result=array_merge($innerArray);
}
return array_unique($result);
}

Categories