change inner key in multidimensional array php - php

I want to ask about how to replace inner key in a multidimensional array.
I have an multidimensional array :
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
The second array is
$array2=array(1,2,3,4,5,6);
I expected the new array is
Array(
[0] => Array
(
[1] => 5000
[2] => 6
[3] => 325
[4] => 3
[5] => 3
[6] => 517000000
)
[1] => Array
(
[1] => 20000
[2] => 5
[3] => 217
[4] => 5
[5] => 3
[6] => 1692000000
))
I have tried this code below by another post PHP Replace multidimensional array keys, but I can't assign the value of my array1
foreach($array2 as $array2 ){
for($k=0;$k<sizeof($array2);$k++){
for($l=0;$l<$count;$l++){
$last[$l][$array2] = $array1[$k][$l];
}
$i += $count;
}
}
Thank you

As #Rizier has suggested in his comment you can do this using array_map() and array_combine().
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$array2 = array(1, 2, 3, 4, 5, 6);
foreach($array1 as $arr1){
$array3[] = array_combine($array2, $arr1);
}
var_dump($array3);
output
array (size=2)
0 =>
array (size=6)
1 => int 5000
2 => int 6
3 => int 325
4 => int 3
5 => int 3
6 => int 517000000
1 =>
array (size=6)
1 => int 20000
2 => int 5
3 => int 217
4 => int 5
5 => int 3
6 => int 1692000000

try
<?php
$array1=
array(array(5000, 6, 325, 3, 3, 517000000),
array( 20000, 5, 217, 5, 3, 1692000000)
);
$newArray = array();
foreach($array1 as $arr){
array_unshift($arr,'');
unset($arr[0]);
$newArray[] = $arr;
}
print_r($newArray);
this will have same output you require.
hope it helps :)

Related

Transpose a multidimensional associative array into a multidimensional indexed array sorted against and an external associative key

I need to transpose a multidimensional associative array into a multidimensional indexed array sorted against and an external associative key. In the example below, I need a way to get from the 'input' to the 'expected output'.
I've tried array_match(), array_intersect() but I think I'm missing something. There must be an elegant solution to this but I cannot figure it out.
//Input
$array = array(
array('Volvo' => 22, 'BMW' => 13, 'Saab' => 5, 'Land Rover' => 11),
array('Nissan' => 10, 'Saab' => 4),
array('Land Rover' => 22, 'BMW' => 9, 'Nissan' => 2, 'Ford' => 17)
//...
);
//Expected output
$array_cars = array( // sorted list of unique car names
0 => 'BMW',
1 => 'Ford',
2 => 'Land Rover',
3 => 'Nissan',
4 => 'Saab',
5 => 'Volvo'
//...
);
$compiled_data = array( // 2D matrix, columns: $array, rows: $array_car
array(0 => 13, 2 => 9), // 'BMW'
array(2 => 17), // 'Ford'
array(0 => 11, 2 => 22), // 'Land Rover'
array(1 => 10, 2 => 2), // 'Nissan'
array(0 => 5, 1 => 4), // 'Saab'
array(1 => 22) // 'Volvo'
//...
);
Probably the simplest thing is to just iterate over all the values, sorting them into a car indexed array. You can then use ksort to sort the data:
$output = array();
foreach ($array as $key => $a) {
foreach ($a as $car => $v) {
$output[$car][$key] = $v;
}
}
ksort($output);
$array_cars = array_keys($output);
$compiled_data = array_values($output);
var_export($array_cars);
var_export($compiled_data);
Output:
array (
0 => 'BMW',
1 => 'Ford',
2 => 'Land Rover',
3 => 'Nissan',
4 => 'Saab',
5 => 'Volvo',
)
array (
0 =>
array (
0 => 13,
2 => 9,
),
1 =>
array (
2 => 17,
),
2 =>
array (
0 => 11,
2 => 22,
),
3 =>
array (
1 => 10,
2 => 2,
),
4 =>
array (
0 => 5,
1 => 4,
),
5 =>
array (
0 => 22,
),
)
Demo on 3v4l.org

PHP Merge arrays and add a value

I need help merging two PHP arrays:
Array 1:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 6
)
Array 2:
Array
(
[0] => Array
(
[id_sabor] => 2
[chocolate] => N
)
[1] => Array
(
[id_sabor] => 3
[chocolate] => S
)
[2] => Array
(
[id_sabor] => 4
[chocolate] => N
)
[3] => Array
(
[id_sabor] => 5
[chocolate] => S
)
[4] => Array
(
[id_sabor] => 6
[chocolate] => N
)
)
The values on array 1 are the active objects. I need to keep on Array 2 or on a new array only the ones with an [id_sabor] that matches in the array 1 (in this case: 2, 3, 4 and 6). Also, on those that [chocolate]=S add a new value: [costo_extra]=25.
One way to do that could be to use array_reduce and use in_array to check if the first array contains the value of id_sabor.
$array1 = [2, 3, 4, 6];
$array2 = [
["id_sabor" => 1, "chocolate" => "N"],
["id_sabor" => 2, "chocolate" => "N"],
["id_sabor" => 3, "chocolate" => "S"],
["id_sabor" => 4, "chocolate" => "N"],
["id_sabor" => 5, "chocolate" => "S"],
["id_sabor" => 6, "chocolate" => "N"]
];
$array2 = array_reduce($array2, function($carry, $item) use ($array1){
if (in_array($item["id_sabor"], $array1)) {
if ($item["chocolate"] === "S") {
$item["costo_extra"] = 25;
}
$carry[] = $item;
}
return $carry;
});
Demo

sorting an array with unsorted index

i got an array like this with unsorted outer index.
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
foreach($a as $b)
array_multisort($b, SORT_ASC, SORT_NUMERIC);
echo "<br/>After Sorting: ";
print_r($a);
which gives me output as below
Before Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
After Sorting:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
AND WHAT I WANT IS
$a = array(
(0) => array(2, 1, 5, 0, 3, 4),
(1) => array(1, 2, 3, 0, 5, 4)
);
please tell me how to deal with.........
How about just using ksort (as you need to reorder your array by key)?
$a = array(
1 => array(1, 2, 3, 0, 5, 4),
0 => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
ksort($a);
echo "<br/>After Sorting: ";
print_r($a);
Since you're only wanting to sort by the top level indices, you don't need to use multisort. You don't even need a loop.
Try this:
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
ksort($a);
print_r($a);
Should give you what you want.
See http://www.php.net/manual/en/function.ksort.php for more info.

Array matches with values on keys

It feels I am going way over my head while discovering the ultimate usage of arrays.
I have two arrays, where the first has main keys, and the value is a count of files attached to that key.
The goal is to match the keys of this first array to the values in a second array, but still mainting (and show) the (value)count of Array-1 -- but for only the values in the second array.
Seems somewhat hazy perhaps, but here are the arrays. The second one has the values that should match the keys in the first.
(My problem is that I keep losing the values of array 1 with every attempt I make.)
Hope you can help me out with this one.
(working matches are keys like: 125, 2051 & 2214)
Array 1:
Array (
[6960] => 3
[2214] => 4
[2051] => 4
[6944] => 2
[6938] => 4
[1823] => 1
[766] => 6
[3993] => 4
[5896] => 6
[6927] => 2
[4220] => 3
[77] => 3
[83] => 1
[125] => 2
[6618] => 2
[196] => 1
[4072] => 12
[3718] => 1
[5918] => 1
[3388] => 10
[4500] => 13
[5968] => 2
[3000] => 2
[942] => 1
[4246] => 8
[5868] => 2
[6394] => 3
[1168] => 1
[2163] => 1
[1827] => 2
[2071] => 8
[4597] => 1
[1702] => 7
)
Array 2:
Array (
[0] => 1024
[1] => 1076
[2] => 111
[3] => 124
[4] => 125
[5] => 1301
[6] => 1409
[7] => 2051
[8] => 2214
[9] => 2636
[10] => 3246
[11] => 4838
[12] => 6946
[13] => 6955
[14] => 6961
[15] => 73
[16] => 74
[17] => 8
)
What about doing this:
<?php
$arr1 = array(1 => 1000, 500 => 1111, 1000 => 5000, 5000 => 5555);
$arr2 = array(1, 5000);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
(
[1] => 1000
[5000] => 5555
)
Or, using your data:
<?php
$arr1 = array(6960 => 3, 2214 => 4, 2051 => 4, 6944 => 2, 6938 => 4, 1823 => 1, 766 => 6, 3993 => 4, 5896 => 6, 6927 => 2, 4220 => 3, 77 => 3, 83 => 1, 125 => 2, 6618 => 2, 196 => 1, 4072 => 12, 3718 => 1, 5918 => 1, 3388 => 10, 4500 => 13, 5968 => 2, 3000 => 2, 942 => 1, 4246 => 8, 5868 => 2, 6394 => 3, 1168 => 1, 2163 => 1, 1827 => 2, 2071 => 8, 4597 => 1, 1702 => 7);
$arr2 = array(1024, 1076, 111, 124, 125, 1301, 1409, 2051, 2214, 2636, 3246, 4838, 6946, 6955, 6961, 73, 74, 8);
print_r(array_intersect_key($arr1, array_flip($arr2)));
OUTPUT:
Array
(
[2214] => 4
[2051] => 4
[125] => 2
)
array_interset_keys will find the intersection of arrays by keys, not values. Since your second array is an index based array (not an associative array) we need to first flip the keys and values using array_flip. Then the keys can be intersected.
Your question is somewhat unclear, but I think this is what you're looking for:
foreach( $array2 as $key)
{
$count = ( isset( $array1[ $key ]) ? $array1[ $key ] : 0);
echo $key . ' has ' . $count . ' files.';
}
Uhhmm.. i cant seem to understand what you want to imply.. but from the way i see it.. if you want to have the keys of array 1 as value to array 2.. just do this code..
foreach($array1 as $key=>$val) {
$array2[] = $key;
}
This should grab the KEYS of array1 and insert it to your array2[].
Hope this helps you.. Cheers :)
This should print out what you need:
foreach($array2 as $key=>$val) {
echo $val;
foreach($array1 as $key2 => $val2){
if($key == $val2){
echo $val2;
}
}
echo '\n'; // new line
}

Merge two multidimensional arrays and reindex all subarrays

I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)

Categories