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);
}
Related
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);
}
});
I asked a question called 'How to count items in an array that's in an array?' and now I need help on expanding from that question.
How do you count items in two arrays?
My array looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
And I use:
<?php $arrays = get_field('irl_today_entry');
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, 'irl_today_website'));?>
to count items in [irl_today_social]. How do I count items in [irl_today_social] and [irl_today_website]?
I tried array_column($arrays, "irl_today_social", "irl_today_website") and it only counted items in [irl_today_social]
array_map() can be fed multiple arrays to work with. The first array "irl_today_social" elements are referenced by $x, the second "irl_today_website" by $y in this case.
Use following:
$res = array_map(function($x, $y) {
$soc = count($x);
$web = count($y);
return ['soc' => $soc, 'web' => $web];
}, array_column($arrays, "irl_today_social"), array_column($arrays, "irl_today_website"));
array_map() will return an array with the count for each - the result sample output:
Array
(
[0] => Array
(
[soc] => 2
[web] => 3
)
)
demo
By definition, PHP's array_shift will return the first elem from the array and the array will downsize one.
I want to slice an array alternatingly into two sub-arrays, i.e index of 0,2,4,6,... and index of 1,3,5,7,...
I first break it into chunks of 2, then I use array_map to call array_shift. However, it is half correct (output below):
array1: Array ( [0] => greeting [1] => question [2] => response )
array2: Array ( [0] => Array ( [0] => greeting [1] => hello ) [1] => Array ( [0] => question [1] => how-are-you ) [2] => Array ( [0] => response [1] => im-fine ) )
Here's the code:
$a=array("greeting", "hello", "question", "how-are-you", "response", "im-fine");
$b=array_chunk($a, 2);
$c=array_map("array_shift", $b);
echo "array1: ";
print_r($c);
echo "array2: ";
print_r($b);
array2 does not seem to downsize after array_shift. Is this the correct behavior? How can I do what I intend to do?
Thanks
While this is already solved using array functions here is a low tech solution using a simple toggle to separate the odd and even elements...
$in = array("greeting", "hello", "question", "how-are-you", "response", "im-fine");
$i = 0;
$list = array();
foreach ($in as $text)
{
$list[$i][] = $text;
$i = 1 - $i;
}
print_r($list[0]);
print_r($list[1]);
Result...
Array ( [0] => greeting [1] => question [2] => response )
Array ( [0] => hello [1] => how-are-you [2] => im-fine )
Try this way to split your array alternatively EVEN and ODD keys, there may be many ways to do this, but this is my simple way using array_walk
$odd = [];
$even = [];
$both = [&$even, &$odd];
$array=["greeting", "hello", "question", "how-are-you", "response", "im-fine"];
array_walk($array, function($value, $key) use ($both) { $both[$key % 2][] = $value; });
print '<pre>';
print_r($even);
print_r($odd);
print '</pre>';
RESULT:
Success time: 0.02 memory: 24448 signal:0
<pre>Array
(
[0] => greeting
[1] => question
[2] => response
)
Array
(
[0] => hello
[1] => how-are-you
[2] => im-fine
)
I'm trying to experiment with array_splice and I get an output like this (from $match)
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
)
[0] => valuel.jpg //this should really be inside [Leep me Updated] array
[1] => value2.jpg //this should really be inside [Leep me Updated] array
[2] => value3.jpg //this should really be inside [Leep me Updated] array
}
from (this foreach creates puts in the values into $match)
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match,1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}
What I'm trying to get is bring [0],[1],[2] into the [Keep me Updated] $match array:
Array
(
[Keep me Updated] => Array
(
[winner] => winnerl.jpg
[0] => value0.jpg
[1] => value1.jpg // old one: [0] => valuel.jpg
[2] => value2.jpg // old one: [1] => value2.jpg
[3] => value3.jpg // old one: [2] => value3.jpg
)
}
This is what $data looks like
$data[] = array(
"data"=>array
(
"name"=>$name,
),
"winner"=>array
(
"lrg_img"=>$img_url_winner
),
"loser"=>array
(
"lrg_img"=>$img_url_loser
)
$data has array values, and $match is where I'm trying to sort the data. So if my values match, it'll consolidate.
Thanks!
Use the inner array as the argument to array_splice
foreach($data as $d)
{
if (isset($match[$d['data']['name']])) {
$match_loser = array($d['loser']['lrg_img']);
array_splice($match[$d['data']['name']],1,0,$match_loser);
}else{
$match[$d['data']['name']] = array("winner"=>$d['winner']['lrg_img'],
$d['loser']['lrg_img']);
}
}
Actual array as below is basically the array of $_POST.
One can understand what is coming from the form. three controls with same name different value. What i need is below this array.
Array
(
[mytext] => Array
(
[0] => aaaa
[1] => bbbb
[2] => ddd
)
[mysel] => Array
(
[0] => one
[1] => two
[2] => two
)
[submit] => Submit
)
I need the array in row format below but be dynamic based of $_POST data.
Array
(
[0] => Array
(
[0] => aaaa
[1] => one
)
[1] => Array
(
[0] => bbbb
[1] => two
)
[2] => Array
(
[0] => dddd
[1] => two
)
)
Try this:
$out = Array();
foreach($_POST['mytext'] as $k=>$v) {
$out[$k] = Array($v,$_POST['mysel'][$k]);
}
var_dump($out);
// Code To Get controls value in row wise
$count=0;
foreach($_POST as $key=>$val){
foreach($_POST[$key] as $val2){
$row[$count][]=$val2;
$count++;
}
$count=0;
}
print_r($row);
Loop through one of the fields, and then pull the corresponding value from the other field.
$result = array();
foreach($_POST['mytext'] as $k=>$v){
$result[] = array($v, $_POST['mysel'][$k]);
}
You can also use array_map to do this:
// PHP 5.3+
$result = array_map(function($a, $b){
return array($a, $b);
}, $_POST['mytext'], $_POST['mysel']);
// PHP <= 5.2
$result = array_map(create_function('$a,$b', 'return array($a,$b);'), $_POST['mytext'], $_POST['mysel']);