So I have two arrays. My main one is stored in a variable called $topVideos
looks like this:
["rows"] => array(10) {
[0] => array(2) {
[0] => string(11) "GARuSV"
[1] => float(2003)
}
[1] => array(2) {
[0] => string(11) "rDnbeSrk"
[1] => float(1119)
}
[2] => array(2) {
[0] => string(11) "0ykEqaUI"
[1] => float(1015)
}
And the second one is stored in a variable called $titles
and looks like this:
array(10) {
[0] => string(25) "Serve Heroes"
[1] => string(38) "Rover Sailing Adventure!!"
[2] => string(54) "BNI Presentation"
What I have to do is merge the second one's every key into the first one's, so it can look like this:
["rows"] => array(10) {
[0] => array(2) {
[0] => string(11) "GARuSV"
[1] => float(2003)
[3] => string(25) "Serve Heroes"
}
[1] => array(2) {
[0] => string(11) "rDnbeSrk"
[1] => float(1119)
[3] => string(25) "Rover Sailing Adventure!!"
}
[2] => array(2) {
[0] => string(11) "0ykEqaUI"
[1] => float(1015)
[3] => string(25) "BNI Presentation"
}
How can I accomplish this? Any help is welcomed! Thank you for your time
foreach($titles as $key => $title){
$topVideos["rows"][$key][] = $title;
}
Loop the array, check if the value exists in the other array, if so, add it.
<?php
foreach( $topVideos['rows'] as $i => $v ) {
if( isset( $titles[$i] ) ) { $topVideos['rows'][$i][] = $titles[$i]; }
}
?>
You can use array_merge_recursive() to get the desired result
$array = array_merge_recursive($array1, $array2);