I have two arrays both guaranteed to be the same length. The two arrays are of the following structures
array1
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
)
[3] => Array
(
[id] => 1656857
[store] => 11
[position] => 1
)
....
....
)
array2
Array
(
[0] => 5/20/2019
[1] => 7/7/2019
[2] => 12/16/2018
...
...
)
How do I append every value of array2 as a key value pair to array1 to get the following array. The key name can be whatever I just chose date.
Array
(
[0] => Array
(
[id] => 841052
[store] => 11
[position] => 1
[date] => 5/20/2019
)
[1] => Array
(
[id] => 1613197
[store] => 11
[position] => 401
[date] => 7/7/2019
)
[2] => Array
(
[id] => 1648966
[store] => 11
[position] => 1
[date] => 12/16/2018
)
)
...
...
...
I have tried
array_push($array1, $array2);
It just pushed it to the last element of the array. I thought of using two foreach loops but couldn't get ti to work. Is there a built in php function that will do this, or do I have to do it in loops.
Just walk $array1 and modify each sub-array by adding the new key and the value of $array2 with the same key:
array_walk($array1, function(&$v, $k) use($array2) { $v['date'] = $array2[$k]; });
try this:
$array1 = array(array("id" => 841052, "store" => "11", "position" => "1"), array("id" => 1613197, "store" => "11", "position" => "401"),);
$array2 = array("5/20/2019", "7/7/2019");
foreach ($array1 as $index => $valuearray1) {
if (array_key_exists($index, $array2)) {
$array1[$index]["date"] = $array2[$index];
}
}
var_dump($array1);
Related
I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Input array:
Array([0] => Array
(
[id] => 1
[check] => 1
[option] => "mk"
)[1] => Array
(
[id] => 2
[check] => 3
[option] => "zz"
)[2] => Array
(
[id] => 3
[check] => 5
[option] => "mk"
))
Output array:
Array([0] => Array
(
[id] => 1
[check] => 6
[option] => "mk"
)[1] => Array
(
[id] => 2
[check] => 3
[option] => "zz"
))
How to add all values [check] for all same [option] values and simplifiy an output array by removing already added arrays?
You just need to use foreach loop.
Here is the solution.
<?php
$arr = [
["id" => 1, "check" => 1, "option" => "mk"],
["id" => 2, "check" => 3, "option" => "zz"],
["id" => 3, "check" => 5, "option" => "mk"]
];
$newArr = [];
foreach($arr as $a) {
// check if array element is already there based on option key in $newArr. In that case just add check value.
if(isset($newArr[$a['option']])) {
$newArr[$a['option']]['check'] += $a['check'];
continue;
}
$newArr[$a['option']] = $a;
}
$newArr = array_values($newArr);
print_r($newArr);
Here is the output.
Array
(
[0] => Array
(
[id] => 1
[check] => 6
[option] => mk
)
[1] => Array
(
[id] => 2
[check] => 3
[option] => zz
)
)
This question already has answers here:
How to array_merge_recursive() an array?
(2 answers)
Closed 5 months ago.
I need to find a way to combine elements by key in a new array, such that I end up with just one key containing all values for that key (found elsewhere in the parent array). This will make more sense when you see the example below:
Array
(
[0] => Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
)
[1] => Array
(
[AAA] => Array
(
[0] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
)
The original array is quite large with more keys and values. But everything is in the same structure depicted above.
I'd like to read that in, and return
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[4] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
What's the least painful and most efficient way of accomplishing this?
Using array_merge_recursive() and array unpacking to pass the separate parts into it...
print_r(array_merge_recursive(...$array));
gives...
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[3] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
You could use array_map() to traverse the parent array. And use another foreach loop to combine the values. Please note the $output array is passed by reference.
$inputArray = [
0 => [
'AAA' => [
0 => 'F104_f58f85f9dbbe1ce083ca09a6f9ace679',
1 => 'F104_480e9f231b2b5a138f8aa5570ffa8634',
2 => 'F104_d5fde0ad2499052e0eae6dec451f9385',
],
'BBB' => [
0 => 'F44_e2e052e7b78abbae02ffcf7413302a0c',
1 => 'F44_4d4da736509babebba7433b203b16753',
]
],
1 => [
'AAA' => [
0 => 'F104_480e9f231b2b5a138f8aa5570ffa8634'
],
'ZZZ' => [
0 => 'F20_5e742bc21f41a14243c3f72b9fa4815e'
]
]
];
$output = [];
array_map(function($values) use (&$output){
foreach ($values as $key => $value){
if(empty($output[$key])){
$output[$key] = $value;
}else{
$output[$key] = array_merge($output[$key], $value);
}
}
}, $inputArray);
print_r($output);
Here is a working example http://sandbox.onlinephpfunctions.com/code/97909ce93184fad6c33af8eb48d184ae86438774
I have got 2 arrays(One single and one multidimensional).
Single array "A" looks like
[questionid] => Array
(
[0] => 12
[1] => 13
[2] => 55
[3] => 15
[4] => 16
)
Multidimensional array "B" looks like
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 15
[answer] =>
)
[3] => Array
(
[quid] => 16
[answer] =>
)
[4] => Array
(
[quid] => 55
[answer] =>
)
)
Now I want the array B (quid) values to be rearranged depending upon the values from array A. So in array B the value of quid last element(55) is at the very end whereas in array A it is in 3rd position.
I want the array B look like this
Array
(
[0] => Array
(
[quid] => 12
[answer] => AAA
)
[1] => Array
(
[quid] => 13
[answer] => neighbour
)
[2] => Array
(
[quid] => 55
[answer] =>
)
[3] => Array
(
[quid] => 15
[answer] =>
)
[4] => Array
(
[quid] => 16
[answer] =>
)
)
The code for multidimensional array is
$ansid = array
(
array
(
"quid" => 12,
"answer" => "AAA"
),
array
(
"quid" => 13,
"answer" => "neighbour"
),
array
(
"quid" => 15,
"answer" =>""
),
array
(
"quid" => 16,
"answer" =>""
),
array
(
"quid" => 55,
"answer" =>""
)
);
Not using array_walk() as to be mor demonstrative, you could just
$newB=array()
foreach ($arrayB as $b) $newB[$b['quid']]=$b;
$newA=array()
foreach ($arrayA as $k=>$v) $newA[$k]=$newB[$v]
//$newA has the required structure
With the user sort function:
$single_array = ...; // order by the index of this array
$mult_dim_array = ...; // to be ordered by the 'quid' value of the elements
function my_comp($a, $b) {
return array_search($a['quid'], $single_array ) - array_search($b['quid'], $single_array );
}
usort($mult_dim_array, "my_comp");
This will get the index on your first array to determine which element goes first or later. The function reads $single_array as a global variable (defined outside the function).
Documentation at http://php.net/manual/en/function.usort.php
I have my array
Array
(
[0] => Dusche
[1] => Mobliert
)
And I have second array which is composed and looks like this:
[0] => Array
(
[id] => 1002
[attribute_id] => 65
[value_id] => 26815
[name] => Garten/-mitbenutzung
[order] => 0
)
[1] => Array
(
[id] => 1003
[attribute_id] => 65
[value_id] => 26811
[name] => Etagenheizung
[order] => 1
)
[2] => Array
(
[id] => 1004
[attribute_id] => 65
[value_id] => 26829
[name] => Balkon/Terrasse
[order] => 2
How can I search this second array with values from the first array and retrieve attribute_id from elements which have the same names?
PHP way:
filteredArray = array();
foreach ($secondArray as $type) {
if (in_array($type['name'], $firstArray)) {
$filteredArray[] = $type['attribute_id'];
}
}
Cake Set way, something along the lines of:
$filteredArray = array();
foreach ($firstArray as $keyword) {
$filteredArray = array_merge($filteredArray, Set::extract("/.[name=$keyword]/attribute_id", $secondArray));
}