Merging 2D array by ID - PHP - php

Hello guys I am stuck for this kind of merging of arrays:
Sample Array
Array(
[0] => Array(
'id' => '1',
'task' => 'Task 1.0'
),
[1] => Array(
'id' => '1',
'task' => 'Task 1.1'
),
[2] => Array(
'id' => '2',
'task' => 'Task 2.0'
),
[3] => Array(
'id' => '2',
'task' => 'Task 2.1'
)
)
Expected Result
Array(
[0] => Array(
'id' => '1',
'task' => array(
[0] => 'Task 1.0',
[1] => 'Task 1.1'
)
),
[1] => Array(
'id' => '2',
'task' => array(
[0] => 'Task 2.0',
[1] => 'Task 2.1'
)
)
)
How can I do this kind of merging?
Thanks in advance.

this might not be the best solution, but i would consider it as an aproach:
$oldArray = array (
0 => array(
'id' => '1',
'task' => 'Task 1.0'
),
1 => array(
'id' => '1',
'task' => 'Task 1.1'
),
2 => array(
'id' => '2',
'task' => 'Task 2.0'
),
3 => array(
'id' => '2',
'task' => 'Task 2.1'
)
);
$newArray = array();
foreach( $oldArray as $array ) {
if( !isset( $newArray[$array["id"]] ) ) {
$newArray[$array["id"]] = array( "id" => $array["id"] );
}
$newArray[$array["id"]]["task"][] = $array["task"];
}
// reset the temp keys
$newArray = array_values( $newArray );
Edited: forgot "tasks" in $newArray[$array["id"]]["task"][] = $array["task"];, made an edit again

Related

Extract value from array php [duplicate]

This question already has answers here:
Find value of sibling key in php array
(4 answers)
Closed 3 years ago.
I have the following array of data and I am trying to access the tracking number field.
Someting like:
$array->meta_data->TrackingNumber
After 3 hours I cannot find a way to access it.
Any help much appreciated.
array (
'id' => 448,
'parent_id' => 0,
'status' => 'completed',
'currency' => 'GBP',
'version' => '3.7.1',
'prices_include_tax' => true,
'meta_data' =>
array (
0 =>
array (
'id' => 8041,
'key' => 'is_vat_exempt',
'value' => 'no',
),
1 =>
array (
'id' => 8043,
'key' => '_wpam_id',
'value' => '4',
),
2 =>
array (
'id' => 8046,
'key' => '_woo_pp_txnData',
'value' =>
array (
'refundable_txns' =>
array (
0 =>
array (
'txnID' => '1U998392V9620752U',
'amount' => '5.95',
'refunded_amount' => 0,
'status' => 'Completed',
),
),
'txn_type' => 'sale',
),
),
3 =>
array (
'id' => 8056,
'key' => 'Payer PayPal address',
'value' => 'anthony#befive.co.uk',
),
4 =>
array (
'id' => 8057,
'key' => 'Payer first name',
'value' => 'Anthony',
),
5 =>
array (
'id' => 8058,
'key' => 'Payer last name',
'value' => 'Evans',
),
6 =>
array (
'id' => 8059,
'key' => 'Payment type',
'value' => 'instant',
),
7 =>
array (
'id' => 8060,
'key' => '_paypal_status',
'value' => 'completed',
),
8 =>
array (
'id' => 8061,
'key' => '_paypal_transaction_fee',
'value' => '0.47',
),
9 =>
array (
'id' => 8290,
'key' => 'TrackingNumber',
'value' => '10046182',
),
10 =>
array (
'id' => 8291,
'key' => 'CarrierName',
'value' => 'SEABOURNE',
),
11 =>
array (
'id' => 10349,
'key' => '_alg_wc_custom_order_number',
'value' => '6',
),
),
'line_items' =>
array (
48 =>
array (
),
),
'tax_lines' =>
array (
50 =>
array (
),
),
'shipping_lines' =>
array (
49 =>
array (
),
),
'fee_lines' =>
array (
),
'coupon_lines' =>
array (
51 =>
array (
),
),
)
You're trying to access your array with $array->value, but the correct syntax would be $array['value']
I would loop through the meta data and check each key to see if is equals "tracking number" then echo out or do whatever you want with the result!
<?php
$test =array (
'id' => 448,
'parent_id' => 0,
'status' => 'completed',
'currency' => 'GBP',
'version' => '3.7.1',
'prices_include_tax' => true,
'meta_data' =>
array (
0 =>
array (
'id' => 8041,
'key' => 'is_vat_exempt',
'value' => 'no',
),
1 =>
array (
'id' => 8043,
'key' => '_wpam_id',
'value' => '4',
),
2 =>
array (
'id' => 8046,
'key' => '_woo_pp_txnData',
'value' =>
array (
'refundable_txns' =>
array (
0 =>
array (
'txnID' => '1U998392V9620752U',
'amount' => '5.95',
'refunded_amount' => 0,
'status' => 'Completed',
),
),
'txn_type' => 'sale',
),
),
3 =>
array (
'id' => 8056,
'key' => 'Payer PayPal address',
'value' => 'anthony#befive.co.uk',
),
4 =>
array (
'id' => 8057,
'key' => 'Payer first name',
'value' => 'Anthony',
),
5 =>
array (
'id' => 8058,
'key' => 'Payer last name',
'value' => 'Evans',
),
6 =>
array (
'id' => 8059,
'key' => 'Payment type',
'value' => 'instant',
),
7 =>
array (
'id' => 8060,
'key' => '_paypal_status',
'value' => 'completed',
),
8 =>
array (
'id' => 8061,
'key' => '_paypal_transaction_fee',
'value' => '0.47',
),
9 =>
array (
'id' => 8290,
'key' => 'TrackingNumber',
'value' => '10046182',
),
10 =>
array (
'id' => 8291,
'key' => 'CarrierName',
'value' => 'SEABOURNE',
),
11 =>
array (
'id' => 10349,
'key' => '_alg_wc_custom_order_number',
'value' => '6',
),
),
'line_items' =>
array (
48 =>
array (
),
),
'tax_lines' =>
array (
50 =>
array (
),
),
'shipping_lines' =>
array (
49 =>
array (
),
),
'fee_lines' =>
array (
),
'coupon_lines' =>
array (
51 =>
array (
),
),
);
foreach($test['meta_data'] as $data){
if($data['key'] === "TrackingNumber"){
echo "The tracking number is: " . $data['value'];
}
}

Finding duplicate values in a multidimensional array for Search Method

My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);

php, 3 array to one array merge alternative

I have 3 arrays. These inter-related. I want to combine these statements.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$merge = array();
foreach($posts_user as $data){
foreach ($posts as $post){
if($data['post_id'] == $post['id'] ){
foreach ($users as $user){
if($data['user_id'] == $user['id']){
$post['user'] = $user;
$merge['post'][] = $post;
}
}
}
}
}
print_r($merge);
I want to be as follows. How can I do it?
I want results.
$merge = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3',
'user' => array (
'id' => '2',
'name' => 'Selena'
),
),
);
Is there another alternative? For example; How do I do with them?
array_walk_recursive(), ArrayIterator(), RecursiveArrayIterator()
I tried to do.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$newuser = array();
foreach($post_user as $data){
foreach ($user as $users){
if($data['user_id'] == $users['id']){
$newuser[] = $users;
}
}
}
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($post),1);
$mi->attachIterator(new ArrayIterator($newuser),'user');
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
print_r($newArray);
Results
Array
(
[0] => Array
(
[1] => Array
(
[id] => 1
[title] => Title 1
[content] => Content 1
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[1] => Array
(
[1] => Array
(
[id] => 2
[title] => Title 2
[content] => Content 2
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[2] => Array
(
[1] => Array
(
[id] => 3
[title] => Title 3
[content] => Content 3
)
[user] => Array
(
[id] => 2
[name] => Selena
)
)
)

PHP merge associative arrays by replacing same keys and adding new

I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)

Offline and online server processes code differently

I have a piece of code that get's processed differently on my localhost and live server.
I have no idea why or what to change.
Here is the piece of code:
for($k = 0; $k < count($data['SurveyAnswer']); $k++) {
if(isset($data['SurveyAnswer'][$k]['answer']['number'])) {
if($data['SurveyAnswer'][$k]['answer']['number'] != '')
$data['SurveyAnswer'][$k]['answer'] = $data['SurveyAnswer'][$k]['answer']['number'].','.$data['SurveyAnswer'][$k]['answer']['text'];
else
$data['SurveyAnswer'][$k]['answer'] = '';
} else if(isset($data['SurveyAnswer'][$k]['answer']['yn'])) {
if($data['SurveyAnswer'][$k]['answer']['yn'] == 'No')
$data['SurveyAnswer'][$k]['answer'] = 'No,' . $data['SurveyAnswer'][$k]['answer']['text'];
else
$data['SurveyAnswer'][$k]['answer'] = 'Yes';
} else if(isset($data['SurveyAnswer'][$k]['answer']['scale'])) {
$data['SurveyAnswer'][$k]['answer'] = $data['SurveyAnswer'][$k]['answer']['scale'] . ',' . $data['SurveyAnswer'][$k]['answer']['text'];
}
}
For arguments sake, this is the data:
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => array(
'number' => '4',
'text' => 'Test text'
),
1 => array (
'answer' => array(
'number' => '',
'text' => ''
),
2 => array (
'answer' => array(
'yn' => 'No',
'text' => 'Test text'
),
3 => array (
'answer' => array(
'yn' => 'Yes',
'text' => ''
),
4 => array (
'answer' => array(
'scale' => 'Good',
'text' => 'Testing text'
)
),
5 => array (
'answer' => '3'
)
)
);
This is how my localhost changes the data (PHP version 5.4.7 on Windows):
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => '4,Test text'
),
1 => array (
'answer' => ''
),
2 => array (
'answer' => 'No,Test text'
),
3 => array (
'answer' => 'Yes'
),
4 => array (
'answer' => 'Good,Testing text'
)
),
5 => array (
'answer' => '3'
)
)
);
And this is how my live server changes the data (PHP version 5.3.23 on CentOS):
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => '4,4'
),
1 => array (
'answer' => ''
),
2 => array (
'answer' => 'T,T'
),
3 => array (
'answer' => 'Yes'
),
4 => array (
'answer' => 'T,T'
)
),
5 => array (
'answer' => '3'
)
)
);
Can it be the version difference causing the issue or is there something else.
UPDATE
Real world data, got this with a CakePHP debug before and CakePHP debug after the code:
localhost
\app\Controller\SurveyAnswersController.php (line 51) (BEFORE)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => array(
'number' => '3',
'text' => 'asdfasdfasdf'
)
)
)
)
\app\Controller\SurveyAnswersController.php (line 67) (AFTER)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => '3,asdfasdfasdf'
)
)
)
webserver
/app/Controller/SurveyAnswersController.php (line 51) (BEFORE)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => array(
'number' => '3',
'text' => 'asdfasdfasdf'
)
)
)
)
app/Controller/SurveyAnswersController.php (line 67) (AFTER)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1,1'
),
(int) 1 => array(
'answer' => '2,2'
),
(int) 2 => array(
'answer' => '3,3'
),
(int) 3 => array(
'answer' => '3,asdfasdfasdf'
),
)
)
I shortened the data as it's around 100 questions.
Believe it or not, but it was a version issue. Updated my PHP to 5.4.x and it worked.

Categories