Merge array key values - PHP - php

Hi all i need to merge the same key to convert to single array from multiple array list please any one help me to the problem
for example here the array.
Array
(
[0] => Array
(
[0] => Mr.
[1] => Mrs.
)
[1] => Array
(
[0] => Rob
[1] => Tam
)
[2] => Array
(
[0] => kar
[1] => Man
)
[3] => Array
(
[0] => 55345345345
[1] => 44545345435
)
)
i need the output is
Array
(
[0] => Array
(
[0] => Mr.
[1] => Rob
[2] => kar
[3] => 55345345345
)
[1] => Array
(
[0] => Mrs.
[1] => Tam
[2] => Man
[3] => 44545345435
)
)
Please any one help
Thanks

For PHP version >= 5.5.0 You can use array_column() and array_merge() for this as
$result = array_merge(array_column($records, '0'), array_column($records, '1'));
print_r($result);

$a = array(
0 => array(
0 => 'Mr.',
1 => 'Mrs.'
),
1 => array
(
0 => 'Rob',
1 => 'Tam'
),
2 => array
(
0 => 'kar',
1 => 'Man'
),
3 => array
(
0 => 55345345345,
1 => 44545345435
)
);
$arr1 = array();
foreach($a as $arr)
{
foreach($arr as $key=>$value)
{
$arr1[$key][] = $value;
}
}
echo '<pre>';
print_r($arr1);
Use this one. You can get output same as you want.

try like this
$out = array();
foreach ($arr1 as $key => $value){
$out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)

$title = $array[0];
$firstname = $array[1];
$lastname = $array[2];
$number = $array[3];
$output = array();
for($i=0; $i < count($title); $i++)
{
$output[] = array($title[$i],$firstname[$i],$lastname[$i],$number[$i])
}
var_dump($output);

Related

combine 2 array with same key into 1 array

I have 2 Array:
$arr1 = Array (
[2] => Array ( [0] => 41000 [1] => 31079 )
[3] => Array ( [0] => 42963 [1] => 41189 )
)
$arr2 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 )
[3] => Array ( [0] => 42998 [1] => 34567 )
)
I want to combine these two arrays to this array with same key:
$arr3 = Array (
[2] => Array ( [0] => 40213 [1] => 42054 [2] => 41000 [3] => 31079 )
[3] => Array ( [0] => 42998 [1] => 34567 [2] => 42963 [3] => 41189 )
)
I tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
This is a nice and easy thing to do.
$arr1 = [2 => [0 => 41000, 1 => 31079], 3 => [0 => 42963, 1 => 41189]];
$arr2 = [2 => [0 => 40213, 1 => 42054], 3 => [0 => 42998, 1 => 34567]];
function addArray(&$bucket, $water)
{
foreach ($water as $key => $drop) {
$bucket[$key] = array_merge($bucket[$key] ?? [], $drop);
}
}
$combined = [];
addArray($combined, $arr1);
addArray($combined, $arr2);
var_export($combined);
See a PHP fiddle here.
The output is:
array (
2 =>
array (
0 => 41000,
1 => 31079,
2 => 40213,
3 => 42054,
),
3 =>
array (
0 => 42963,
1 => 41189,
2 => 42998,
3 => 34567,
),
)
How does it work? I wrote a little function that would add the input arrays to an array that combines all arrays.
$arr1 = array(
2 => array(41000, 31079),
3 => array(42963, 41189)
);
$arr2 = array(
2 => array(40213, 42054),
3 => array(42998, 34567)
);
$arr3 = array();
foreach($arr1 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
foreach($arr2 as $key => $values){
if(!isset($arr3[$key])){
$arr3[$key] = array();
}
$arr3[$key] = array_merge($arr3[$key], $values);
}
echo '<pre>';
print_r($arr3);
Prints:
Array
(
[2] => Array
(
[0] => 41000
[1] => 31079
[2] => 40213
[3] => 42054
)
[3] => Array
(
[0] => 42963
[1] => 41189
[2] => 42998
[3] => 34567
)
)

Change value inside an array

I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)

Combine two array and add value of array count

I have two array like this
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
echo "<pre>";
print_r($arr1);
print_r($arr2);
And I want output like this
Array
(
[0] => Array
(
[0] =>Prabhash
[1] =>9
)
[1] => => Array
(
[0] =>Nagda
[1] =>1
)
[2] => => Array
(
[0] =>Sayyed
[1] =>2
)
)
I have tried to combine and merge array but not success, Hope someone will help me for this better.
PHP code demo
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result=array();
foreach($arr1 as $key => $value)
{
if(isset($result[$value]))
{
$result[$value][1]+=$arr2[$key];
}
else
{
$result[$value]=array($value,$arr2[$key]);
}
}
$result= array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Short solution using array_map, array_keys, array_flip, array_unique, array_intersect_key and array_sum functions:
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result = array_map(function($n) use($arr1, $arr2){
$sum = array_sum(array_intersect_key($arr2, array_flip(array_keys($arr1, $n))));
return [$n, $sum];
}, array_unique($arr1));
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Try it.
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$newArray = array();
foreach($arr1 as $key => $value) {
$newArray[$value][0] =$value;
if(!isset($newArray[$value][1]) || $newArray[$value][1] == null)
$newArray[$value][1] = $arr2[$key];
else
$newArray[$value][1] = $newArray[$value][1]+$arr2[$key];
}
$newArray = array_values($newArray);
echo "<pre>";
print_r($newArray);
?>
OUTPUT :
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)

Different Arrays convert into multi-dimension array

I want to combine multiple arrays output in single array. Below is the array I am getting when I do this.
print_r($getData_milestone);
I have arrays as below:
[milestone] => Array
(
[0] => milestone 1
[1] => milestone 2
[2] => milestone 3
)
[date] => Array
(
[0] => 10/25/2015
[1] => 10/30/2015
[2] => 11/25/2015
)
[status] => Array
(
[0] => 1
[1] => 1
[2] => 0
)
And I want to get output such as below:
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
I have tried by this code
foreach($getData_milestone['milestone'] as $miledata)
{
$allDatamile[$i]=$getData_milestone;
$allDatamile[$i]=$getData_milestone['date'];
$allDatamile[$i]=$getData_milestone['status'];
$i++;
}
Try this out, and let me know the result. It should work.
I am considering the given array as an associative array with keys "milestone", "date" and "status" .. Correct me if I'm wrong.
$outputArray = array();
foreach($givenArray['milestone'] as $key=>$val){
$outputArray[$key]['milestone'] = $val;
$outputArray[$key]['date'] = $givenArray['date'][$key];
$outputArray[$key]['status'] = $givenArray['status'][$key];
}
print_r($outputArray)
try this,
$a["milestone"][] = "milestone 1";
$a["milestone"][] = "milestone 2";
$a["milestone"][] = "milestone 3";
$a["date"][] = "10/25/2015";
$a["date"][] = "10/30/2015";
$a["date"][] = "11/25/2015";
$a["status"][] = "1";
$a["status"][] = "1";
$a["status"][] = "0";
foreach ($a['milestone'] as $key => $val) {
$a1[$key]["milestone"] = $val;
$a1[$key]["date"] = $a['date'][$key];
$a1[$key]["status"] = $a['status'][$key];
}
output is
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
array_column (PHP 5 >= 5.5.0) might help -
$keys = array_keys($arr);
// if the number of element increases(to make it more dynamic)
$count = count($arr['milestone']);
$i= 0;
while($i < $count) {
$new[] = array_column($arr, $i);
$i++;
}
foreach($new as $k => $n) {
$new[$k] = array_combine($keys, $n);
}
var_dump($new);
DEMO
Try it out the bellow code
$out= array();
$milestone=array
(
"milestone 1",
"milestone 2",
"milestone 3"
);
$m_date=array
(
"10/25/2015",
"10/25/2015",
"10/25/2015"
);
$status=array
(
0,1,1
);
for($i=0;$i<count($milestone);$i++){
$comArray=array
(
"milestone" => $milestone[$i],
"date" => $m_date[$i],
"status" => $status[$i]
)
$out[]=$comArray;
}
Hope it will solve your problem.

PHP fetch 3d array

I have below array and i want to fetch [2] => Array with foreach but it's showing me an error.
for example array name is $other
Array
(
[0] => Array
(
[0] => aaaaaaaaaaaa
[1] => bbbbbbbbbbbb
[2] => cccccccccccc
)
[1] => Array
(
[0] => dddddddddddd
[1] => eeeeeeeeeeee
[2] => ffffffffffff
)
[2] => Array
(
[0] => gggggggggggg
[1] => hhhhhhhhhhhh
[2] => iiiiiiiiiiii
)
)
fetch array:
foreach ($other[2] as $value) {
echo $value.'<br/>';
}
How do I print all the values of the second array?
You need to nest further more
foreach ($other as $arr)
{
foreach($arr as $k=>$v)
{
if($k==2)
{
echo $v.'<br/>';
}
}
}
OUTPUT :
cccccccccccc
ffffffffffff
iiiiiiiiiiii
Try Something like this,
<?php
$x = array
(
0 => array
(
0 => 'aaaaaaaaaaaa',
1 => 'bbbbbbbbbbbb',
2 => 'cccccccccccc'
),
1 => array
(
0 => 'dddddddddddd',
1 => 'eeeeeeeeeeee',
2 => 'ffffffffffff'
),
2 => array
(
0 => 'gggggggggggg',
1 => 'hhhhhhhhhhhh',
2 => 'iiiiiiiiiiii'
)
);
$count = count($x);
$w = $count - 1;
var_dump($x[$w]);
?>

Categories