Transform keys and values in an associative array - php

I have 2 associative Arrays but they are in the wrong form. The first
Array(
'name' => 'adam',
'age' => '13'
)
and the second one shoulb be combined (merged?)
Array(
'key' => 'pet',
'value' => 'dog'
)
that the result would be like
Array(
'name' => 'adam',
'age' => '13',
'pet' => 'dog'
)
Can anyone give me a hint/solution?
EDIT: I did it that way:
$result = array_merge($item, array_column($metas, 'v', 'k'));
Thanks

$data1 = Array(
'name' => 'adam',
'age' => '13'
);
$data2 = Array(
'key' => 'pet',
'value' => 'dog'
);
let $result=[];
foreach ($data1 as $key => $value) {
$result[$data2['key']]=$data2['value'];
$result=$data1+$result;
}
print_r($result);

Related

PHP multidimensional array: group repeated values and create "sub array"

I have a array with repeated values like this:
[
[
'id' => 112,
'name' => 'John',
'country' => 'Spain',
'age' => 24,
'company' => 'One',
'price' => 10
],
[
'id' => 112,
'name' => 'John',
'country' => 'Spain',
'age' => 24,
'company' => 'Two',
'price' => 15
],
[
'id' => 112,
'name' => 'John',
'country' => 'Spain',
'age' => 24,
'company' => 'Three',
'price' => 20
],
[
'id' => 224,
'name' => 'Paul',
'country' => 'France',
'age' => 25,
'company' => 'One',
'price' => 25
],
[
'id' => 224,
'name' => 'Paul',
'country' => 'France',
'age' => 25,
'company' => 'Two',
'price' => 40
]
]
I need to group same id with name, country and age, and make a "sub array" companies with company and price
array (
112 =>
array (
'id' => 112,
'name' => 'John',
'country' => 'Spain',
'age' => 24,
'companies' =>
array (
0 =>
array (
'company' => 'One',
'price' => 10,
),
1 =>
array (
'company' => 'Two',
'price' => 15,
),
2 =>
array (
'company' => 'Three',
'price' => 20,
),
),
),
224 =>
array (
'id' => 224,
'name' => 'Paul',
'country' => 'France',
'age' => 25,
'companies' =>
array (
0 =>
array (
'company' => 'One',
'price' => 25,
),
1 =>
array (
'company' => 'Two',
'price' => 40,
),
),
),
)
I have tried this code without good results:
$new_array = array();
foreach ($my_array as $item) {
$new_array[$item['id']][] = $item;
$new_array[$item['id']]['companies'][] = $item;
}
Then I tried this:
$new_array = array();
foreach ($my_array as $item) {
$new_array[$item['id']]['temp'] = $item;
$new_array[$item['id']]['companies'][] = $item;
}
Works fine, but I get temp key that I don't want it. For example, with this code I need to access to id items with $new_array [$key]['temp']['id']
I can remove the temp key with another loop:
$final_array = array();
foreach ($new_array $key=>$item) {
$final_array [$key] = $item['temp'];
$final_array [$key]['temp'] = $item['companies'];
}
With this code I can access correctly to id with: $final_array[$key]['id']
Another option is this code:
foreach($array as $v) {
$result[$v['id']]['id'] = $v['id'];
$result[$v['id']]['name'] = $v['name'];
$result[$v['id']]['country'] = $v['country'];
$result[$v['id']]['age'] = $v['age'];
$result[$v['id']]['companies'][] = array('company' => $v['company'],
'price' => $v['price']);
}
But it is not very elegant if we had more keys (phone, email...)
Any ideas?
Nothing wrong with the code you've provided at the end, but here's some ideas for how to make it more tolerant to more array values (as you mentioned, phone, email, etc).
This uses the handy PHP array_diff_key function to remove the array elements you don't want from the "core" records. Then, applying array_diff_key to get those same array elements INTO the "company" records.
// set up the array of keys you don't want in the "original" record
$exclude = ['company' => FALSE, 'price' => FALSE];
// Your original loop
foreach($array as $v) {
// not strictly necessary, but helps readability
$id = $v['id'];
// Assign the "core" record all the values you want, excluding those defined above
// in this case, will remove "company" and "price"
$record = array_diff_key( $v, $exclude );
// only set this if not already set, otherwise wipes out previous companies
if ( ! isset($result[$id] ) ) {
$result[$id] = $record;
$result[$id]['companies'] = [];
}
// strip off the OTHER values from the array you don't want stored with the company
// this will automatically pick up the field NOT excluded above
// in this case, will remove all BUT "company" and "price"
$company = array_diff_key( $v, $record );
$result[$id]['companies'][] = $company;
}
One way to rome...
$collector=array();
foreach($array as $set){
//get a copy
$ref = $set;
//unset company data
unset($ref['company'],$ref['price']);
//make a refer
$r=$ref['id'];
//setup main data if not set before
if(!isset($collector[$r])){
$collector[$r]=$ref;
}
//collect company data
$collector[$r]['companies'][] = array('company'=>$set['company'],'price'=>$set['price']);
}
print_r($collector);
Similar to other answers:
$info = array (
array('id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'One', 'price' => 10),
array('id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Two', 'price' => 15),
array('id' => 112, 'name' => 'John', 'country' => 'Spain', 'age' => 24, 'company' => 'Three', 'price' => 20),
array('id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'One', 'price' => 25),
array('id' => 224, 'name' => 'Paul', 'country' => 'France', 'age' => 25, 'company' => 'Two', 'price' => 40)
);
$grouped = array();
foreach ($info as $item) {
if (!isset($grouped[$item['id']])) {
$grouped[$item['id']] = $item;
}
$grouped[$item['id']]['companies'][] = array($item['company'],$item['price']);
unset($grouped[$item['id']]['company']);
unset($grouped[$item['id']]['price']);
}
print_r($grouped);
The sample data does not seem to require grouping by more than the id column.
The snippet below will extract/consume the last two elements in each row before pushing the first encountered row with a unique id, then it unconditionally pushes those two extracted rows into the group's companies subset.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$companies = array_splice($row, -2);
$result[$row['id']] ??= $row;
$result[$row['id']]['companies'][] = $companies;
}
var_export($result);

Sort array's duplicate value and make it the key

I'm having trouble getting to re-arrange an array by the key's duplicate values of the Key 'Company' and making it the value so that it is re-arranged in the code below this one:
Original array:
$array = array(
'number' => 2,
'items' =>
array(
'Company' => 'ABC',
'First' => 'John',
'Last' => 'Doe',
),
array(
'Company' => 'ABC',
'First' => 'Jane',
'Last' => 'Doe',
),
array(
'Company' => 'XYZ',
'First' => 'Peter',
'Last' => 'Pumpkin',
),
);
I want to re-sort the array on the 'Company' value and make it the key, like the example below:
$array = array(
'number' => 2,
'items' =>
array(
'ABC' =>
array(
'First' => 'John',
'Last' => 'Doe'),
array(
'First' => 'Jane',
'Last' => 'Doe'),
'XYZ' =>
array(
'First' => 'Peter',
'Last' => 'Pumpkin'),
)
);
I looked hard in the PHP Docs, so this seems to be a unique type of sort.
$array = array(
'number' => 2,
'items' => array(
array(
'Company' => 'ABC',
'First' => 'John',
'Last' => 'Doe',
),
array(
'Company' => 'ABC',
'First' => 'Jane',
'Last' => 'Doe',
),
array(
'Company' => 'XYZ',
'First' => 'Peter',
'Last' => 'Pumpkin',
)
)
);
$final = $array;
$final['items'] = array();
foreach ($array['items'] as $item) {
$final['items'][$item['Company']] = $item;
unset($final['items'][$item['Company']]['Company']);
}
var_dump($final);
Result:
array (size=2)
'number' => int 2
'items' =>
array (size=2)
'ABC' =>
array (size=2)
'First' => string 'Jane' (length=4)
'Last' => string 'Doe' (length=3)
'XYZ' =>
array (size=2)
'First' => string 'Peter' (length=5)
'Last' => string 'Pumpkin' (length=7)
You can construct the array you want by looping over your original array.
BTW, I think you meant to put another array() to hold all the items in.
$array = array(
'number' => 2,
'items' =>
array(//I assume this was missing in your original Question
array(
'Company' => 'ABC',
'First' => 'John',
'Last' => 'Doe',
),
array(
'Company' => 'ABC',
'First' => 'Jane',
'Last' => 'Doe',
),
array(
'Company' => 'XYZ',
'First' => 'Peter',
'Last' => 'Pumpkin',
),
)
);
$newarray = array();
foreach ($array['items'] as $key=>$val){
$company = $val['Company'];
if (empty($newarray[$company])){
//set a new key to hold our data
$newarray[$company] = array();
}
// because $val contains 'Company', we want to get rid of it
unset($val['Company']);
// add the current item under the company key
$newarray[$company][] = $val;
}
$array['items'] = $newarray;
print_r($array);
I see CodeGodie just answered with a much shorter version of this. :)

Extracting data from complicated associative array in php and put into new array

I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

Removing selected elements from array of associative arrays

I have the following array of associative arrays.
$result = array(
(int) 0 => array(
'name' => 'Luke',
'id_number' => '1111',
'address' => '1544addr',
'time_here' => '2014-04-12 13:07:08'
),
(int) 1 => array(
'name' => 'Sam',
'id_number' => '2222',
'address' => '1584addr',
'time_here' => '2014-04-12 14:15:26'
I want to remove selected elements from this array such that it will look like this;
array(
(int) 0 => array(
'name' => 'Luke',
'id_number' => '1111'
),
(int) 1 => array(
'name' => 'Sam',
'id_number' => '2222',
This is the code I wrote;
foreach($result as $value)
{
unset($value('address') );
unset($value('time_here') );
}
When I run the code, Apache web server crashed.
Can the smarter members point out what did I do wrong? Thank you very much.
Array notation is wrong, use this;
$finalResult = array();
foreach($result as $value)
{
unset($value['address'] );
unset($value['time_here'] );
$finalResult[] = $value;
}
Here is a working demo: Demo
That is because you are not accessing array correctly. Use square bracket insted of round brackets :
foreach($result as $value)
{
unset($value['address'] );
unset($value['time_here'] );
}

multidimensional array merge operation inside loop

let's say I have two arrays like so:
$array1 = array('A' => array(
'B' => array(
'C' => array(
'D' => array(
'data' => array(
0 => array(
'id' => 1,
'name' => 'name 1'),
1 => array(
'id' => 2,
'name' => 'name 2')))))));
$array2 = array('A' => array(
'B' => array(
'C' => array(
'E' => array(
'data' => array(
0 => array(
'id' => 3,
'name' => 'name 3'),
1 => array(
'id' => 4,
'name' => 'name 4')))))));
As you can see, the two arrays have the same key A, B, and C but the keys are different afterwards. How do I merge these two arrays into something like this:
$final_array = array('A' => array(
'B' => array(
'C' => array(
'D' => array(
'data' => array(
0 => array(
'id' => 1,
'name' => 'name 1'),
1 => array(
'id' => 2,
'name' => 'name 2'))),
'E' => array(
'data' => array(
0 => array(
'id' => 3,
'name' => 'name 3'),
1 => array(
'id' => 4,
'name' => 'name 4')))))));
As you can see, in this case I merge the arrays together into the same array that contains different keys for both. In order words, here I'm putting the array starting from key E from the second array into the array with index C.
Any help will be appreciated, thanks
EDIT: Now, how about if my arrays ($array1, $array2, $array3, $array4, etc...) are generated inside a foreach loop, how do I merge all of those arrays together (Notice that I do not know the number of arrays beforehand)
http://php.net/manual/en/function.array-merge-recursive.php
print_r(array_merge_recursive($array1, $array2));
This should do the trick.
Added:
$collection=array();
foreach() {
$collection[]=$myArray; //here you add your array to collection
}
print_r(call_user_func_array('array_merge_recursive', $collection));
i have not tested this but try this code:
foreach( $array1 as $key => $val )
{
if( !in_array( $key, $array2 ) )
{
$array2[$key] = $val;
}
}
EDIT
use Rok Kralj's answer, using native functions are probably the best way to do this as they are much faster.

Categories