This question already has answers here:
PHP - merging arrays
(2 answers)
Closed 7 years ago.
I have these two arrays and I want to combine them into one. Using the duplicate values from 0,1 in the second array. Below is an example of how I would want it to look. I hope someone can help.
Array(
[201500001] => Array
(
[0] => 1003123603
[1] => 3062226597
)
[201500002] => Array
(
[0] => 3067005512
)
)
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
)
)
Expected output:
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
[2] => 201500001
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
[2] => 201500002
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
[2] => 201500001
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
[2] =>
)
)
I rewrote your various arrays as valid arrays. Other than that, I think that what you are looking for is to use array_intersect. This works for me:
$source = array(
201500001 => array(
0 => 1003123603,
1 => 3062226597
),
201500002 => array(
0 => 3067005512
)
);
$target = array(
1127893457 => array(
0 => 1003123603
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$expected = array(
1127893457 => array(
0 => 1003123603,
2 => 201500001
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512,
2 => 201500002
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597,
2 => 201500001
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$newArray = [];
foreach ($target as $targetKey => $targetValue) {
foreach ($source as $sourceKey => $sourceValue) {
if (array_intersect($sourceValue, $targetValue)) {
$targetValue[2] = $sourceKey;
}
$newArray[$targetKey] = $targetValue;
}
}
echo ($newArray === $expected) ? "Match!" : "Miss." . PHP_EOL;
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Related
I have multidimensional array , I just want to push the matched value into another array: Can someone guide me to get the right array as I put into last line.
$getcount= Array
(
0 => Array
(
0 => Array
(
'pickup_trip_location' => 'Bishan',
'trip_route_id' => '1',
'c' => '5'
),
1 => Array
(
'pickup_trip_location' => 'Bukit Merah',
'trip_route_id' => 1,
'c' => 4
),
2 => Array
(
'pickup_trip_location' => 'Geylang',
'trip_route_id' => '1',
'c' => '2'
),
3 => Array
(
'pickup_trip_location' => 'Kallang',
'trip_route_id' => '1',
'c' => '3',
),
) ,
1 => Array
(
0 => Array
(
'pickup_trip_location' => 'Mandai',
'trip_route_id' => '2',
'c' => '2',
),
1 => Array
(
'pickup_trip_location' => 'Queenstown',
'trip_route_id' => '2',
'c' => '3',
),
2 => Array
(
'pickup_trip_location' => 'Toa Payoh',
'trip_route_id' => '2',
'c' => '1'
),
)
);
second array:
$array1_1 = Array
(
0 => array
(
'stoppage_points' => 'Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio',
),
1 => array
(
'stoppage_points' => 'Queenstown,Toa Payoh,Bedok,Paya Lebar,Mandai,Changi,Yishun',
)
);
$array = [];
//$String = "Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio";
$f = 0 ;
foreach($array1_1 as $key_1 => $arr){
$one=explode(",",$arr['stoppage_points']);
$i = 0 ;
foreach ($one as $key2=> $item){
$array[$key_1][$key2] = explode(",",$item);
$i++;
}
pr($array);
//die();
foreach($getcount as $key_1 => $arr){
//echo $key_1;
$p = 0 ;
foreach($arr as $key => $arr_1){
echo $key;
$arrayval = $arr_1['pickup_trip_location'];
$c = $arr_1['c'];
$trip_route_id = $arr_1['trip_route_id'];
//echo $id =array_search($arrayval, $array[$key_1][$key][$p]);
if( array_search( $arrayval ,$array[$key_1][$key]) ) {
$array[$key]['pickup_trip_location'] = $arrayval;
$array[$key]['trip_route_id'] = $trip_route_id;
$array[$key]['count'] = $c;
}
}
$p++;
}
pr($array);
$f++;
}
I want to output like this : Getting output for first array only :
Array
(
[0] => Array
(
[0] => Bishan
[pickup_trip_location] => Bishan
[trip_route_id] => 1
[count] => 5
)
[1] => Array
(
[0] => Bukit Merah
[pickup_trip_location] => Bukit Merah
[trip_route_id] => 1
[count] => 4
)
[2] => Array
(
[0] => Geylang
[pickup_trip_location] => Geylang
[trip_route_id] => 1
[count] => 2
)
[3] => Array
(
[0] => Kallang
[pickup_trip_location] => Kallang
[trip_route_id] => 1
[count] => 3
)
[4] => Array
(
[0] => Toa Payoh
)
[5] => Array
(
[0] => Ang Mo Kio
)
)
I have tried to push value into first array but didn't get any luck
I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);
i have problem to combine values based on id.
I have data like this :
Array(
[0] => Array(
[id] => 1,
[id_name] => a
[id_vales] => 5
)
[1] => Array(
[id] => 1
[id_name] => a
[id_vales] => 4
)
[2] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4
)
[3] => Array(
[id] => 3
[id_name] => b
[id_vales] => 3
)
)
then, i want combine [id_values] based on id, so i can get data like this in php
Array(
[0] => Array(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
You can use the following example to merge your array
<?php
$mainArray = array(array('id' => 1, 'id_name' => 'a', 'id_vales' => 5),
array('id' => 1,'id_name' => 'a','id_vales' => 4),
array('id' => 3, 'id_name' => 'b','id_vales' => 4),
array('id' => 3,'id_name' => 'b','id_vales' => 3)
);
$result = array();
$tempArray = array();
foreach($mainArray as $key => $value)
{
if(isset($tempArray[$value['id']]))
{
$tempArray[$value['id']] .= ", ".$value['id_vales'];
$result[] = array('id' => $value['id'], 'id_name' => $value['id_name'], 'id_vales' => $tempArray[$value['id']]);
}
else
{
$tempArray[$value['id']] = "".$value['id_vales'];
}
}
echo "<pre>";
print_r($result);
?>
You can find running example here https://paiza.io/projects/3sS3GXH7GHqoipH8k-YtBQ
Output:
Array
(
[0] => Array
(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array
(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
I have created an array for you. from this array you can easily create your array and get the result.
$data = array();
foreach($array as $key=>$value){
$data[$value['id']]['id'] = $value['id'];
$data[$value['id']]['id_vales'][] = $value['id_vales'];
$data[$value['id']]['id_name'] = $value['id_name'];
}
I have an array of arrays like this :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
)
),
);
Here is the current working answer by user #Nigel Ren I have to this question here.
$store = [];
$headers = [];
foreach ( $data as $set ) {
$headerRow = array_shift($set);
// Collect all header columns
$headers = array_merge($headers, $headerRow);
foreach ( $set as $index => $list ){
// Create associative list of data so they can be combined (i.e. ID fields)
$list = array_combine($headerRow, $list);
// Use ID value as key and create if needed
if ( !isset($store[$list["ID"]]) ) {
$store[$list["ID"]] = $list;
}
else {
$store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
}
}
}
$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow ) {
// Fill in the fields for this ID and then change to numeric keys
$output['output'][] = array_values(array_merge($blank, $dataRow));
}
print_r($output);
Above code works perfectly for the above given array.
Here are the new cases I need to handle:
CASE 1 :
When the data contains only one array where the ID are same :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '101',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
),
);
In this case Nigel's answer doesn't output both the both records for the same ID. It outputs just a single record.
Expected output when there's a single array with duplicate ID's present :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
)
[2] => Array
(
[0] => 101
[1] => 786075
[2] => 2012-09-05
)
)
)
CASE 2 :
When any of the array's other than the first array contain's entries for multiple same ID.
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Order',
1 => 'ID',
),
1 =>
array (
0 => 'Colgate',
1 => '101',
),
2 =>
array (
0 => 'Waffles',
1 => '101',
)
),
);
Expected output in this case :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
[3] => Order
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Colgate
)
[2] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Waffles
)
[3] => Array
(
[0] => 103
[1] => 786075
[2] => 2012-09-05
[3] =>
)
)
)
How do I do it?
you can get the desired output by using a loop:
$desired_array = array();
$n_data1 = count($data['data1']);
$n_data2 = count($data['data2']);
$n_data3 = count($data['data3']);
for ($i=0; $i < $n_data1; $i++) {
if ($n_data2 > $i) {
foreach ($data['data2'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
for ($i=0; $i < $n_data1; $i++) {
if ($n_data3 > $i) {
foreach ($data['data3'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
$desired_array = $data['data1'];
I try to convert an array to an indexed array but none of the array functions I found can solve my problem
I have this array
Array(
[no_discount] => 0
[manufacturers_id] => 2
[id] => 3
)
Array(
[no_discount] => 1
[manufacturers_id] => 1
[id] => 1
)
and I would like to convert this array to
Array(
[0] => Array(
[no_discount] => 0
[manufacturers_id] => 2
[id] => 3
)
[1] => Array(
[no_discount] => 1
[manufacturers_id] => 1
[id] => 1
)
)
Is there a simple array function or do I have use a loop?
loop - or write you own ..
$tgt = [];
foreach ( $src as $t) { $tgt[] = $t; }
I'm not sure how you are getting your array but will this work for you?
$main_array = array();
$one = Array(
"no_discount" => 0,
"manufacturers_id" => 2,
"id" => 3,
);
$two = Array(
"no_discount" => 1,
"manufacturers_id" => 1,
"id" => 1,
);
array_push($main_array, $one);
array_push($main_array, $two);
print_r($main_array);