Merging two associative arrays [duplicate] - php

This question already has answers here:
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
I have two arrays.
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
I want to merge into
Array (
[0] => Array (
[caption] => c one
[file] => photo one
)
[1] => Array (
[caption] => c two
[file] => photo two
)
)
or merge into
Array (
[0] => Array (
[0] => c one
[1] => photo one
)
[1] => Array (
[0] => c two
[1] => photo two
)
)
How do you do it?

The last case can be done by array_map function
$res = array_map(null, $a['caption'], $b['photos']);
demo

You can use a nested loop, and just keep track of your keys.
foreach ([$a, $b] as $array) {
foreach ($array as $text_key => $values) {
foreach ($values as $numeric_key => $value) {
$result[$numeric_key][$text_key] = $value;
}
}
}

If the arrays does not have more in common than the key values then this should work.
It loops one array and uses the key to grab the value from the other array.
$a=array('caption'=>array('c one','c two'));
$b=array('photos'=>array('photo one','photo two'));
Foreach($a['caption'] as $key => $capt){
$new[$key]['caption'] = $capt;
$new[$key]['photos'] = $b['photos'][$key];
}
Var_dump($new);
Output:
array(2) {
[0] => array(2) {
["caption"] => "c one"
["photos"] => "photo one"
}
[1] => array(2) {
["caption"] => "c two"
["photos"] => "photo two"
}
}
https://3v4l.org/kfsft

Related

Replace every string in multidimensional array if conditions matched

Ok so I have an array look like this,
Array
(
[0] => Array
(
[0] => order_date.Year
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date.Quarter
[1] => =
[2] => 1
)
)
What I want to do is, in any element of this multidimensional array I want to replace any string that have a . with removing everything after .
So the new array should look like this,
Array
(
[0] => Array
(
[0] => order_date
[1] => =
[2] => 2024
),
[1] => Array
(
[0] => order_date
[1] => =
[2] => 1
)
)
I have tried doing this,
foreach ($filter as $key => $value) {
if(is_array($value)) {
$variable = substr($value[0], 0, strpos($value[0], "."));
$value[0] = $variable;
}
}
print_r($filter);
I'm getting $value[0] as order_date but can't figure out how to assign it to $filter array without affecting other values in array;
The $value variable is not linked with the original array in the foreach loop.
You can make a reference to the original array by using ampersand "&"
foreach ($filter as $key => &$value) { ... }
Or you can use old school key nesting
$filter[$key][0] = $variable;
Please take a look here https://stackoverflow.com/a/10121508/9429832
this will take off values after . in every element of any multidimensional array.
// $in is the source multidimensional array
array_walk_recursive ($in, function(&$item){
if (!is_array($item)) {
$item = preg_replace("/\..+$/", "", $item);
}
});

How to merge two arrays based on their index php?

I need to merge 2 arrays and i have found lot of examples here on stackoverflow, but nothing has worked for me, in my case, so i explain my case:
Arrays (can be one, two, or three, or more...):
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png-
[2] => name-file_icon_003_00.png- )
Array ( [0] => rel
[1] => rel
[2] => rel )
Or can be two:
Array ( [0] => name-file_icon_001_00.png-
[1] => name-file_icon_002_00.png- )
Array ( [0] => rel
[1] => rel )
Or one:
Array ( [0] => name-file_icon_001_00.png- )
Array ( [0] => rel )
Need to insert the relative value "[0] => rel" with "[0] => name-file_icon_001_00.png-"
Expected result (merged):
Array ( [0] => name-file_icon_001_00.png-rel
[1] => name-file_icon_002_00.png-rel
[2] => name-file_icon_003_00.png-rel )
Reading around the web, seems that not exist a native function for make this.
Please, hope in your help :)
A simple foreach loop using the index and value parameters will do it in no time
Example
$a1 = ['name-file_icon_001_00.png-',
'name-file_icon_002_00.png-',
'name-file_icon_003_00.png-'
];
$a2 = ['rel1','rel2','rel3'];
foreach ($a1 as $i => $v){
$new[] = $v . $a2[$i];
}
print_r($new);
RESULT
Array
(
[0] => name-file_icon_001_00.png-rel1
[1] => name-file_icon_002_00.png-rel2
[2] => name-file_icon_003_00.png-rel3
)
You can map each array to a function that concatenates them:
$result = array_map(function($a, $b) { return $a.$b; }, $one, $two);
If you define one array with subarrays then you can unpack that array ...:
$array = [$one, $two];
$result = array_map(function($a, $b) { return $a.$b; }, ...$array);
Or for fun, you can extract each column from the subarrays and implode them:
$array = [$one, $two];
for($i=0; $a=array_column($array, $i); $i++) {
$result[] = implode($a);
}

Two dimentional array into single dimentional array without foreach PHP [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 4 years ago.
Is there way to convert two dimentional array into single dimentional array without using foreach loop in php.
Below is the actual array
Array
(
[0] => Array
(
[male] => male
[female] => female
)
[1] => Array
(
[male] => male1
[female] => female1
)
)
And Output will be like
Array
(
[0] = > male
[1] = > female
[2] = > male1
[3] = > female1
)
You can use reduce and use array_merge
$array = array( ... ); //Your array here
$result = array_reduce($array, function($c,$v){
return array_merge(array_values($c),array_values($v));
}, array());
This will result to:
Array
(
[0] => male
[1] => female
[2] => male1
[3] => female1
)
This loops through your multidimensional array and stores the results in the new array variable $newArray.
$newArray = array();
foreach($multi as $array) {
foreach($array as $k=>$v) {
$newArray[$k] = $v;
}
}

How to add comma between elements of array [duplicate]

This question already has answers here:
How to implode subarrays in a 2-dimensional array?
(4 answers)
Closed 5 years ago.
everyone.
I have an output of multidimensional array(WRONG):
[ [23], [145], [16], [2], [2], [3], [], ]
I need to have an output like this(GOOD):
[ [2,3], [1,4,5], [1,6], [2], [2], [3], [], ]
Here's array of my data: Array ( [0] => Array ( [0] => 2 [1] => 3 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [2] => Array ( [0] => 1 [1] => 6 ) [3] => Array ( [0] => 2 ) [4] => Array ( [0] => 2 ) [5] => Array ( [0] => 3 ) [6] => Array ( ) )
The way I'm trying to do that:
print_r($gretimumosarasas);
echo "[ ";
for ($row = 0; $row < count($gretimumosarasas); $row++) {
echo "[";
for ($col = 0; $col < count($gretimumosarasas[$row]); $col++) {
echo $gretimumosarasas[$row][$col];
}
echo "], ";
}
echo " ]";
Can someone please explain, how to add comma like I wrote an output example below?
If there only way to add coma in second for loop and then search for last symbol index and delete it?
THANK YOU.
While I am extremely nervous of the fact you appear to generating code from your code (here be dragons!) its simple enough.
function array_formatter($arr)
{
$temp=array();
foreach ($arr as $v) {
$temp[] = is_array($v) ? array_formatter($v) : $v;
}
return '[' . implode(',', $temp) . ']';
}
BTW PHP arrays are not multi-dimensional, they are nested. Nested arrays can emulate multi-dimensional arrays, but your example data is not multi-dimensional itself.

how to search in array to find all sub arrays that contain a certain value

i have a array placed in the variable $class that is constituted of sub arrays containing 2 student id's each,
i am trying to find all the sub arrays that contain a certain id for example 11
i want to keep all arrays containing this id in a variable.
array example
Array
(
[0] => Array
(
[s1] => 6
[s2] => 37
)
[1] => Array
(
[s1] => 8
[s2] => 11
)
[2] => Array
(
[s1] => 11
[s2] => 48
)
)
code
foreach ($class as $key => $value) {
if(!in_array($id, $class)){
unset($class[$key]);
}
}
You are close. If you loop and reference the correct variable with the in_array(), it would work as you hopefully need. Then assign the matching array into a new array var to use later (so you are not altering your original array!):
$id = 11;
$matched = array();
foreach ($class as $i => $students) {
if ( in_array($id, $students) ) {
$matched[] = $class[$i];
}
}
print_r($matched);
Would result in:
Array (
[0] => Array
(
[s1] => 8
[s2] => 11
)
[1] => Array
(
[s1] => 11
[s2] => 48
)
)
Associative array
http://php.net/manual/es/function.array-search.php
$class = array(array("s1"=>6, "s2"=>37),array("s1"=>8,"s2"=>11),array("s1"=>11, "s2"=>48));
$id = 11;
foreach ($class as $key => $value) {
if(!array_search($id, $value)){
unset($class[$key]);
}
}
Sorry, you can follow this answers
using array_search for multi dimensional array

Categories