Is there a way, with the two followings arrays to get another array which is the combination of those two arrays?
Array 1:
Array
(
[0] => Array
(
[a_id] => 9
[name] => Mario Lopez
)
[1] => Array
(
[a_id] => 8
[name] => Lisa Turtle
)
)
Array 2:
Array
(
[0] => Array
(
[b_id] => 1
[name] => AC Slater
)
[1] => Array
(
[b_id] => 2
[name] => Lisa Turtle
)
[2] => Array
(
[b_id] => 3
[name] => Kelly Kapowski
)
)
What I would like to get :
Array
(
[0] => Array
(
[b_id] => 1
[name] => AC Slater
)
[1] => Array
(
[a_id] => 8
[b_id] => 2
[name] => Lisa Turtle
)
[2] => Array
(
[b_id] => 3
[name] => Kelly Kapowski
)
[3] => Array
(
[a_id] => 9
[name] => Mario Lopez
)
)
The third array merges the two first arrays where the key name matches
I have not found a builtin function and tried this solution without success: combine 2 associative arrays where values match.
Thank you,
Edit: sorry, I forgot to add Mario Lopez.
My attempt was:
function array_extend($a, $b) {
foreach($b as $k=>$v) {
if( is_array($v) ) {
if( !isset($a[$k]) OR isset($v[0])) {
$a[$k] = $v;
} else {
$a[$k] = array_extend($a[$k], $v);
}
} else {
$a[$k] = $v;
}
}
return $a;
}
This probably is what you are looking for, although as #OldPadawan already pointed out in the comments to the question the actual result differs from the proposed one...
<?php
$a = [
[
'a_id' => 9,
'name' => 'Mario Lopez'
],
[
'a_id' => 8,
'name' => 'Lisa Turtle'
]
];
$b = [
[
'b_id' => 1,
'name' => 'AC Slater'
],
[
'b_id' => 2,
'name' => 'Lisa Turtle'
],
[
'b_id' => 3,
'name' => 'Kelly Kapowski'
]
];
$c = $a;
array_walk($b, function($be) use (&$c) {
$done = false;
array_walk($c, function(&$ce) use($be, &$done) {
if ($ce['name'] == $be['name']) {
$ce['b_id'] = $be['b_id'];
$done = true;
}
});
if ( ! $done) {
array_push($c, $be);
}
});
print_r($c);
The output of above code is:
Array
(
[0] => Array
(
[a_id] => 9
[name] => Mario Lopez
)
[1] => Array
(
[a_id] => 8
[name] => Lisa Turtle
[b_id] => 2
)
[2] => Array
(
[b_id] => 1
[name] => AC Slater
)
[3] => Array
(
[b_id] => 3
[name] => Kelly Kapowski
)
)
Related
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
)
)
I have a multi dimension array like below:
Array
(
[1200] => Array
(
[B] => Array
(
[4] => Array
(
[Name] => 'Joe']
)
)
[A] => Array
(
[3] => Array
(
[Name] => 'Paul']
)
)
)
[1100] => Array
(
[F] => Array
(
[2] => Array
(
[Name] => 'Sam']
)
)
[D] => Array
(
[1] => Array
(
[Name] => 'Jane']
)
)
)
What I wish to achieve is having the 4 digit number 1100 and 1200 in order ascending, then I need the letters (B A) and (F D) also in order, and then the single digit number under them in order ascending too. I believe I'm looking at a multi dimension array but any help would be appreciated.
The below function might be what you're looking for. It recursively orders arrays by their key.
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
Example usage
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
$data = [
1200 => [
'B' => [
4 => [
'Name' => 'Joe'
]
],
'A' => [
3 => [
'Name' => 'Paul'
]
]
],
1100 => [
'F' => [
2 => [
'Name' => 'Sam'
]
],
'D' => [
1 => [
'Name' => 'Jane'
]
]
]
];
ksort_r($data);
print_r($data);
The above will output...
Array
(
[1100] => Array
(
[D] => Array
(
[1] => Array
(
[Name] => Jane
)
)
[F] => Array
(
[2] => Array
(
[Name] => Sam
)
)
)
[1200] => Array
(
[A] => Array
(
[3] => Array
(
[Name] => Paul
)
)
[B] => Array
(
[4] => Array
(
[Name] => Joe
)
)
)
)
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
)
)
I want to compare the value of key[0] of each array.
If they are the same then I would like to add a new key[3] to each array with an id.
This is an array of variable products if the product has the same key[0] then its the same product with different variations.
If the key[0] is different from the previous then add id+1, in this example, I would like to end up with:
Array
(
[681074CRPAK4] => Array
(
[0] => 681074
[1] => 681074CRPAK4
[2] => 5602385431605
[3] => 1
)
[681520XXXP6L] => Array
(
[0] => 681520
[1] => 681520XXXP6L
[2] => 5602385667394
[3] => 2
)
[681530XXXP6V] => Array
(
[0] => 681530
[1] => 681530XXXP6V
[2] => 5602385667417
[3] => 3
)
[681530XXXP6W] => Array
(
[0] => 681530
[1] => 681530XXXP6W
[2] => 5602385667424
[3] => 3
)
[681530XXXP6X] => Array
(
[0] => 681530
[1] => 681530XXXP6X
[2] => 5602385667400
[3] => 3
)
)
can you guys help me with this?
I tried this:
but does not work
foreach ($new as $current_key => $current_array) {
foreach ($new as $search_key => $search_array) {
$ref1 = $current_array[0];
$ref2 = $search_array[0];
if (($search_key != $current_key) and ($ref1 == $ref2)) {
$current_array[3] = $p_id_product;
}
else{
$current_array[3] = $p_id_product++;
}
}
}
Assuming you have already sorted the array by the initial index, so at least they are grouped:
<?php
$data =
[
[
'foo',
'spam',
'bar',
],
[
'foo',
'eggs',
],
[
'bar',
'ham'
],
];
$output = [];
$counter = 0;
$last = null;
foreach($data as $k => $v) {
if($last !== $v[0])
$counter++;
$v[3] = $counter;
$output[$k] = $v;
$last = $v[0];
}
var_export($output);
Output:
array (
0 =>
array (
0 => 'foo',
1 => 'spam',
2 => 'bar',
3 => 1,
),
1 =>
array (
0 => 'foo',
1 => 'eggs',
3 => 1,
),
2 =>
array (
0 => 'bar',
1 => 'ham',
3 => 2,
),
)
I have an array which holds some articles of a website. The problem is: when the same article is in 2 different categories it only shows 1. In this example the categories are 'Service' and 'Sales'.
If the article is duplicate and the category is 'Sales' I want 1 of them to change to 'Service' and vice versa.
The array I got now(3 & 4 as duplicates and 7 & 8):
Array
(
[0] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/38BbjLmVJXk
[3] => Park assist
)
[1] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/3lGfTZdVK1s
[3] => Multi Collision braking system
)
[2] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/6mgDraWpGvE
[3] => Area view
)
[3] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[4] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[5] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/N0fa4dUBkvE
[3] => Trailer assist
)
[6] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/NCNDyW6Yr1g
[3] => Ruitenwissers
)
[7] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
[8] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
)
The array I want to accomplish (No duplicates anymore and the values changed):
Array
(
[0] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/38BbjLmVJXk
[3] => Park assist
)
[1] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/3lGfTZdVK1s
[3] => Multi Collision braking system
)
[2] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/6mgDraWpGvE
[3] => Area view
)
[3] => Array
(
[0] => Service
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[4] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/II68oVm4zro
[3] => Lane Assist
)
[5] => Array
(
[0] => Sales
[1] => assistentiesystemen
[2] => www.youtube.com/video/N0fa4dUBkvE
[3] => Trailer assist
)
[6] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/NCNDyW6Yr1g
[3] => Ruitenwissers
)
[7] => Array
(
[0] => Sales
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
[8] => Array
(
[0] => Service
[1] => veilig-op-de-weg
[2] => www.youtube.com/video/PJEC-yqUwzE
[3] => Bandenafdichtset
)
)
What I tried:
$length = count ($sorted);
for ($c = 0; $c < $length; $c++) {
$check_array = $sorted[$c];
for ($x = 0; $x < $length; $x++) {
$compare_array = $sorted[$x];
if ($x != $i){
if($check_array[2] == $compare_array[2] && $check_array[0] == $compare_array[0]){
//print_r ($check_array);
//print_r ($compare_array);
if($check_array[0] == 'Sales'){
$compare_array[0] = 'Service';
}
if($check_array[0] == 'Service'){
$compare_array[0] = 'Sales';
}
}
}
}
}
Any help will be much appreciated.
Put your data in $array variable and try it:
$hash = array_map( function ( $value ) { return md5( implode( $value ) ); } , $array);
$keys = array_keys( array_diff_assoc( $hash, array_unique( $hash ) ) );
foreach( $keys as $key )
$array[ $key ][0] = $array[ $key ][0] == 'Service' ? 'Sales' : 'Service';
If the order is not important for you, you can use array_multisort, array_column and array_walk:
$replacements = [
'Sales' => 'Service',
'Service' => 'Sales'
];
// Sort array by youtube url.
array_multisort($array, array_column($array, 2));
array_walk($array, function (&$curr, $_, &$prev) use ($replacements) {
if (
$prev !== null
&& $prev[2] === $curr[2]
) {
$curr[0] = $replacements[$curr[0]];
}
$prev = $curr;
}, null);
Here is working demo.
Here I have a written some code, you can try it.
<?php
$array = [
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/38BbjLmVJXk',
3 => 'Park assist',
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/3lGfTZdVK1s',
3 => 'Multi Collision braking system'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/6mgDraWpGvE',
3 => 'Area view'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/II68oVm4zro',
3 => 'Lane Assist'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/II68oVm4zro',
3 => 'Lane Assist'
],
[
0 => 'Sales',
1 => 'assistentiesystemen',
2 => 'www.youtube.com/video/N0fa4dUBkvE',
3 => 'Trailer assist'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/NCNDyW6Yr1g',
3 => 'Ruitenwissers'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/PJEC-yqUwzE',
3 => 'Bandenafdichtset'
],
[
0 => 'Service',
1 => 'veilig-op-de-weg',
2 => 'www.youtube.com/video/PJEC-yqUwzE',
3 => 'Bandenafdichtset'
]
];
/**
* #param $firstIndexValue
* #param $thirdIndexValue
* #param $searchArray
* #return bool
*/
function isExistValue($firstIndexValue, $thirdIndexValue, $searchArray)
{
foreach ($searchArray as $element) {
if ($firstIndexValue == $element[0] && $thirdIndexValue == $element[3]) {
return true;
}
}
return false;
}
$newArray = [];
foreach ($array as $arr) {
$isExist = isExistValue($arr[0], $arr[3], $newArray);
if ($isExist) {
if ($arr[0] == 'Sales') {
$arr[0] = 'Service';
} elseif ($arr[0] == 'Service') {
$arr[0] = 'Sales';
}
}
$newArray[] = $arr;
}
var_dump($newArray);
I have an array that looks like this:
$cars = array (
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'peugeot',
'b' => '306'),
array(
'a' => 'audi',
'b' => 'a4'),
array(
'a' => 'audi',
'b' => 'a5'),
array(
'a' => 'peugeot',
'b' => '106'),
array(
'a' => 'peugeot',
'b' => '106'),
);
I need to order arrays like this to (id is the same as name):
name => audi
id=> audi
data => a4 => 2
a5 => 1
name => peugeot
id=> peugeot
data => 306 => 1
106 => 2
So the car brands need to be grouped an the car types counted.
I already have this code; but that is only for the group part and the count part is missing.
function mergeAndOrder($data){
// set group arrays
$i = 0; $group1 = array();
// loop trough array
$array = array(); $array2 = array();
if($data != null){
foreach($data AS $row){
// search and order level1
$search = array_search($row->a,$group1);
// this object is not found
if(is_int($search) == false){
$group1[$i] = $row->a;
$array[$i]['id'] = $row->a;
$array[$i]['name'] = $row->a;
$array[$i]['data'] = array();
$i++;
}
}
}
return $array;
}
Does somebody know an solution for this case? Thanks!
--- INPUT (part of) ---
a = lease company in this case
Array
(
[0] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[1] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[2] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
[3] => stdClass Object
(
[b] => AUDI
[a] => LPN
)
--- OUTPUT (part of) ---
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
[3] => Array
(
[id] => LPD
[name] => LPD
[data] => Array
(
)
)
)
Array
(
[0] => Array
(
[id] => LPN
[name] => LPN
[data] => Array
(
)
)
[1] => Array
(
[id] => ARV
[name] => ARV
[data] => Array
(
)
)
[2] => Array
(
[id] => ARVB
[name] => ARVB
[data] => Array
(
)
)
If I understand your question correctly, this should do what you want.
function mergeAndOrder ($data) {
$output = array();
foreach ($data as $item) {
$id = $item->a;
$value = $item->b;
if (!array_key_exists($id, $output)) {
$output[$id] = array('id' => $id, 'name' => $id, 'data' => array());
}
if (!array_key_exists($value, $output[$id]['data'])) {
$output[$id]['data'][$value] = 0;
}
$output[$id]['data'][$value]++;
}
// Order by name element
uasort($output, function ($a, $b) {
return strcasecmp($a['name'], $b['name']);
});
return $output;
}
Output:
Array
(
[audi] => Array
(
[id] => audi
[name] => audi
[data] => Array
(
[a4] => 2
[a5] => 1
)
)
[peugeot] => Array
(
[id] => peugeot
[name] => peugeot
[data] => Array
(
[306] => 1
[106] => 2
)
)
)