let's suppos we have the following two arrays
Let's suppose this is called $array1
Array
(
[0] => Array
(
[Name] => Jack
[Height] => 190
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Height] => 160
[Shoe Size] => 52
)
)
Suppose this is called $array2
Array
(
[0] => Name
[1] => Shoe Size
)
Now, what I need to do, is to keep the keys in $array1 which are found in $array2 as values, so the output I'm expecting is something like this
Array
(
[0] => Array
(
[Name] => Jack
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Shoe Size] => 52
)
)
I tried array_intersect and array_intersect_key but they're both failing. does anyone have any idea how to do this?
What you need is array_intersect_key with array_flip
$array3 = array_flip($array2);
foreach($array1 as &$a) {
$a = array_intersect_key($a, $array3);
}
Related
I am trying to remove duplicate and empty values from array with array_unique() function, but getting wrong output.
Data:
Array (
[0] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
[1] => Array (
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
[5] => 101
)
[2] => Array (
[0] =>
[1] =>
[2] => 108
[3] =>
)
)
PHP:
$array = array_filter($userids);
$arrays = array_unique($array, SORT_REGULAR);
print_r($arrays);
nothing happens with SORT_REGULAR - output comes same as raw data, and without SORT_REGULAR this output is coming:
$array = array_filter($userids);
$arrays = array_unique($array);
print_r($arrays);
output:
Array (
[0] => Array
(
[0] =>
[1] => 1
[2] =>
[3] => 108
[4] =>
)
)
output I am looking for:
Array (
[0] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
)
Those array functions only works on a single level. If you flatten the array (adding all elements in a single element array), it should be pretty straight forward.
Flatten the array
$array = array_merge(...$array);
Note: This method works fine for flattening indexed arrays like in your example, but not for associative arrays where any of the sub arrays contains the same keys.
Then filter out all empty
$array = array_filter($array);
and then remove all duplicates
$array = array_unique($array);
Or as a one-liner:
$array = array_unique(array_filter(array_merge(...$array)));
Demo: https://3v4l.org/pEJAJ
I'm trying to rename the parent keys in a Multidimensional Array to the value of a child key.
For example in the code below I would like to change the key [0] to [111] and the key [1] to [222] so it is easy for me to identify keys later for an array merge.
Array (
[0] => Array ( [product_id] => 111 [product_name] => Foo [quantity] => 4 )
[1] => Array ( [product_id] => 222 [product_name] => Bar [quantity] => 2 )
)
I've tried various ways of doing this but after entering a loop, I can't work out how to affect the parent key and assume it's impossible after passing it to a variable. Is there an easy solution to change the key I am missing or is it a case of entering the loop and rebuilding a new array with the desired key?
One-line solution using array_combine and array_column functions:
$result = array_combine(array_column($arr, 'product_id'), $arr);
print_r($result);
The output:
Array
(
[111] => Array
(
[product_id] => 111
[product_name] => Foo
[quantity] => 4
)
[222] => Array
(
[product_id] => 222
[product_name] => Bar
[quantity] => 2
)
)
You need to create a new array, such as:
$original = array(
[0] => Array ( [product_id] => 111 [product_name] => Foo [quantity] => 4 )
[1] => Array ( [product_id] => 222 [product_name] => Bar [quantity] => 2 )
)
$new = array();
foreach ($original as $val) {
$new[$val->product_id] = $val;
}
create new array use foreach to loop through your old array and assign the value of new one
<?php
$oldArray[0] = Array ("product_id"=> 111 , "product_name" => "Foo" , "quantity" => 4 );
$oldArray[1] = Array ("product_id"=> 222 , "product_name" => "Bar" , "quantity" => 2 );
$newArray=array();
foreach($oldArray as $childarray){
$newArray[$childarray['product_id']]=array('product_id'=>$childarray["product_id"],'product_name'=>$childarray["product_name"],'quantity'=>$childarray["quantity"]);
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
?>
Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);
How do I merge these associative arrays so that the indices ([0],[1]) are preserved and var_id, name and id are merged? I've tried array_combine and array_merge_recursive without succes.
Input
Array (
[0] => Array (
[var_id] => 43
)
[1] => Array (
[var_id] => 25
)
)
Array (
[0] => Array (
[name] => Tortoise
)
[1] => Array (
[name] => Black
)
)
Array (
[0] => Array (
[id] => 1907
)
[1] => Array (
[id] => 1908
)
)
Desired output
Array (
[0] => Array (
[var_id] => 43
[name] => Tortoise
[id] => 1907
)
[1] => Array (
[var_id] => 25
[name] => Black
[id] => 1908
)
)
Cheers,
Adnan
Assuming your three arrays are called $array1, $array2, and $array3 here's a loop that will do what you want:
foreach(array($array1, $array2, $array3) AS $array) {
foreach($array AS $key => $value) {
foreach($value AS $subkey => $subvalue) {
$final[$key][$subkey] = $subvalue;
}
}
}
Working example: http://3v4l.org/GY9oa
If you have an unknown number of input arrays to merge, it would be trivial to turn this into a function to handle that.
I two arrays set out like this.
First array
Array
(
[0] => Array
(
[manufacture] => Moto
[name] => ZSX 125
[code] => ZS125-48A
[cc] => 125
[bike_type] => 3 Motorcycle
[title] => Mto ZSX 125
[about] => The ZSX
)
[1] => Array
(
[manufacture] => Moto
[name] => LSM 125
[code] => STR125YB
[cc] => 125
[bike_type] => 6 Endurancemotor
[title] => Moto
[about] => Moto
)
)
Second array
Array
(
[0] => Array
(
[id] => 183
[model] => ZS125-48A
[engine_brand] => 158FMI-B
[engine_type] => Single Cylinder
)
[1] => Array
(
[id] => 172
[model] => STR125YB
[engine_brand] => K154FMI
[engine_type] => Single Cylinder
)
)
As you can see 'code' from the first array is the same as 'model' from the second and there will always be a match.
This is the array i would like to have.
Array
(
[0] => Array
(
[id] => 183
[model] => ZS125-48A
[engine_brand] => 158FMI-B
[engine_type] => Single Cylinder
[manufacture] => Moto
[name] => ZSX 125
[code] => ZS125-48A
[cc] => 125
[bike_type] => 3 Motorcycle
[title] => Moto ZSX 125
[about] => The ZSX
)
[1] => Array
(
[id] => 172
[model] => STR125YB
[engine_brand] => K154FMI
[engine_type] => Single Cylinder
[manufacture] => Moto
[name] => LSM 125
[code] => STR125YB
[cc] => 125
[bike_type] => 6 Endurancemotor
[title] => Moto
[about] => Moto
)
)
Is this possible? i have tried array_merge and array_merge_recursive, but it only appends the second array onto the end of the first, it doesnt merge it on the keys.
Deceze's solution is perfect if the two arrays are synchronized (i.e. items with matching code and model are at the same index in both arrays). If that is not the case you will need more than one line of code.
The arrays need to have string keys in order to be merged with array_merge. In this case you want to rekey the first one based on code and the second based on model. After rekeying you can merge them with array_merge_recursive.
Rekeying is easy in PHP 5.5+:
// array_column will conveniently rekey when the second argument is null
$array1 = array_column($array1, null, 'code');
$array2 = array_column($array2, null, 'model');
A bit more complicated in earlier versions:
// array_map is a more complicated way to extract a column from an array
// and array_combine will do the rekeying
$array1 = array_combine(
array_map(function($i) { return $i['code']; }, $array1),
$array1);
$array2 = array_combine(
array_map(function($i) { return $i['model']; }, $array2),
$array2);
In both cases the final step would be the same:
$result = array_merge_recursive($array1, $array2);
Looks to me like this should do it:
$array3 = array_map('array_merge', $array1, $array2);
This feeds $array1[0] and $array2[0] to array_merge, then $array1[1] and $array2[1] etc.
And, if you prefer "simpler" code... :-)
$result = array();
foreach ($one as $a) {
foreach ($two as $b) {
if ($a->code == $b->model) {
array_push($result, array_merge($a, $b));
break;
}
}
}
print_r($result);