I have the following array defined.
$a = Array
(
[0] => 30:27
[1] => 29:28
[2] => 30:27
)
$b = Array
(
[0] => 102186
[3] => 102991
[4] => 102241
)
I have used array_map($a,$b); But not what i want the result comes.
Always first to first key, second to second key, third to third key, I expect the following result...
$ab = $b = Array
(
[0] => 102186 [30:27]
[1] => 102991 [29:28]
[2] => 102241 [30:27]
)
Edit:
If the array keys doesn't match (thought it was a typo), then just reset the arrays by using $a = array_values($a) and $b = array_values($b) like this:
$a = array(
0 => "30:27",
1 => "29:28",
2 => "30:27"
);
$b = array(
0 => "102186",
3 => "102991",
4 => "102241"
);
// Reset keys
$a = array_values($a);
$b = array_values($b);
$ab = array();
for ($i=0; $i < count($a); $i++) {
$ab[] = "{$b[$i]} [{$a[$i]}]";
}
echo "<pre>";
print_r($ab);
echo "</pre>";
Outputs:
Array
(
[0] => 102186 [30:27]
[1] => 102991 [29:28]
[2] => 102241 [30:27]
)
Just loop over the 1st array and add the corresponding value from the 2nd one. You can actually use array_map for this:
$ab = array_map(function($aVal, $bVal){
return "$bVal [$aVal]";
}, $a, $b);
DEMO: https://eval.in/78684
USE:
$arrayFirst and $arraySecond - your input arrays;
$result = array();
for ($i=0; $i < count($arrayFirst); $i++) {
$result[] = "{$arraySecond[$i]} [{$arrayFirst[$i]}]";
}
var_dump ($result);
Or array_merge_recursive()
$array = array_merge_recursive($array1, $array2);
Related
I have list of unique elements and want to change it to list of associative arrays. What is the most elegant way to do this? I tried foreach but it looks bogus.
Expected Input:
array('2019-10-01', '2019-10-02', '2019-10-03')
Expected Output:
array(array('day' => '2019-10-01'), array('day' => '2019-10-02'), array('day' => '2019-10-03'))
You can use array_map:
$array = array('2019-10-01', '2019-10-02', '2019-10-03');
$output = array_map(function ($v) { return array('day' => $v); }, $array);
or a simple foreach:
$output = array();
foreach ($array as $v) {
$output[] = array('day' => $v);
}
In both cases the output is the same:
Array
(
[0] => Array
(
[day] => 2019-10-01
)
[1] => Array
(
[day] => 2019-10-02
)
[2] => Array
(
[day] => 2019-10-03
)
)
Demo on 3v4l.org
See this short code example. it iterates over the given array and associates a key with an increment:
$a = Array('2019-10-01', '2019-10-02', '2019-10-03');
$b = [];
for($x = 0; $x < count($a); $x++) {
$b['day' . $x] = $a[$x];
}
print_r($b);
// output: Array ( [day0] => 2019-10-01 [day1] => 2019-10-02 [day2] => 2019-10-03 )
For example I have like more than 3 different arrays, with element like below:
1st array
hello-1
hi-1
2nd array
ok-two
hi-2
22-two
hello
3rd array
hi-3rd
hello3
And so on...
I want to combine this array in the order one by one. For example the expected output for the 3 arrays above would be:
hello-1
ok-two
hi-3rd
hi-1
hi-2
hello3
22-two
hello
I tried array_merge(). But it appends the 2nd array after the complete 1st array, which is not what I'm looking for, so here I'm kinda stuck and don't know which functions I can use here. Any hints or ideas?
This should work for you:
First I get the first element of each array into a sub array, then the second value into the next sub array and so on, that you get this structure of array:
Array
(
[0] => Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
)
//...
)
After this you can just loop through each array value with array_walk_recursive() and get every value into your array.
<?php
$arr1 = [
"hello-1",
"hi-1",
];
$arr2 = [
"ok-two",
"hi-2",
"22-two",
"hello",
];
$arr3 = [
"hi-3rd",
"hello3",
];
$arr = call_user_func_array("array_map", [NULL, $arr1, $arr2, $arr3]);
$result = [];
array_walk_recursive($arr, function($v)use(&$result){
if(!is_null($v))
$result[] = $v;
});
print_r($result);
?>
output:
Array
(
[0] => hello-1
[1] => ok-two
[2] => hi-3rd
[3] => hi-1
[4] => hi-2
[5] => hello3
[6] => 22-two
[7] => hello
)
I have another way to solve this issue
<?php
$arr1 = array(
"hello-1",
"hi-1");
$arr2 = array("ok-two",
"hi-2",
"22-two",
"hello");
$arr3 = array(
"hi-3rd",
"hello3");
$max = count($arr1);
$max = count($arr2) > $max ? count($arr2) : $max;
$max = count($arr3) > $max ? count($arr3) : $max;
$result = array();
for ($i = 0; $i < $max; $i++) {
if (isset($arr1[$i])) {
$result[] = $arr1[$i];
}
if (isset($arr2[$i])) {
$result[] = $arr2[$i];
}
if (isset($arr3[$i])) {
$result[] = $arr3[$i];
}
}
print_r($result);
I have been trying to code this for a while but I am so confused with multidimensional arrays.
I have simplified it to this:
Consider I already have an array:
$myarray = ("1233","4345","3452");
I would like to push an array, onto this array (to create multidimensional array) at a certain value.
For example,
With these two variables:
$eventID = 4345
$photoID = 12
I would want to end up with:
("1233", "4345 => 12", "3452")
First, your array needs to be an "associative array". You can create the array in one statement like this:
$myarray = array(
1233 => "",
4345 => 12,
3452 => ""
);
Or one item at a time like this:
$myarray = array();
$myarray[1233] = "";
$myarray[4345] = 12;
$myarray[3452] = "";
This is still one dimensional array. You can go one step further and create a multi-dimensional array like this:
$myarray = array();
$myarray[1233] = "";
$myarray[4345] = array("PhotoID" => 12, "Location" => "Somewhere");
$myarray[3452] = array();
$myarray[3452]["PhotoID"] = 13;
$myarray[3452]["Location"] = "Somewhere else";
See the section on Arrays in PHP manual. It is a very frequently used data structure in PHP and I encourage you to read the section thoroughly.
In PHP arrays works like that:
$a = array("1233", "4345", "3452");
In the above example, the values, "1233", "4345" and "3452" they have each own an index number. So if you run that code:
$a = array("1233", "4345", "3452");
echo "<pre>";
print_r($a);
echo "</pre>";
you will get that result:
Array
(
[0] => 1233
[1] => 4345
[2] => 3452
)
In that case you can't assign an array on "4345" but on "[1]". So, with that in mind if you have another one array like that :
$b = array("321", "654", "987");
and you like to assign it into position "[1]" then you have to do something like that:
$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");
$a[1] = $b;
TAKE CARE
The above code will replace your value "4345" with the content of the array $b. Now let's try to print out your array:
$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");
$a[1] = $b;
echo "<pre>";
print_r($a);
echo "</pre>";
The result will be that now:
Array
(
[0] => 1233
[1] => Array
(
[0] => 321
[1] => 654
[2] => 987
)
[2] => 3452
)
Finaly, if you like to keep both the values "4345" from the array $a and assign an array to that same position into the array $a you have to consider what you like to do with the value "4345"
Some ideas are here:
$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");
$b[] = $a[1];
$a[1] = $b;
echo "<pre>";
print_r($a);
echo "</pre>";
The above code has that result:
Array
(
[0] => 1233
[1] => Array
(
[0] => 321
[1] => 654
[2] => 987
[3] => 4345
)
[2] => 3452
)
Or you can try that:
$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");
$c = array();
$c[] = $a[1];
$c[] = $b;
$a[1] = $c;
echo "<pre>";
print_r($a);
echo "</pre>";
The above code will have the following result
Array
(
[0] => 1233
[1] => Array
(
[0] => 4345
[1] => Array
(
[0] => 321
[1] => 654
[2] => 987
)
)
[2] => 3452
)
It looks like you want to create an associative array from an existing array. You can try this:
$assoc_array = array_combine($myarray, array_fill(0, count($myarray), null));
$assoc_array['4345'] = 12;
$assoc_array will be filled with null values for all other eventIDs.
This code will produce what you want:
$myarray = array("1233","4345","3452");
print_r($myarray);
$eventID = 4345;
$photoID = 12;
if( in_array( $eventID, $myarray) )
{
array_diff( $myarray, array($eventID) );
$myarray[ $eventID] = $photoID;
}
print_r($myarray);
Do you want to create a key value pair? In that case why don't u try
$myarray["4345"]=12;
which will end up and array like
Array ( [0] => 1233 [1] => 4345 [2] => 3452 [4345] => 12 )
I have an array of array .
Example like
a[0]={1,2,3};
a[1]={2,3,4};
**Edit** in a[2] from a[2]={4,5};
a[2]={2,4,5};
and more
How can I find common element which exist in all array ?
This is a function I have made. It's just a reference for a multidimensional array.
<?php
$array1 = array('angka'=>12,'satu','2'=>'dua','tiga'=>array('dua','enam','empat'=>array('satu','lima',12)));//object
$array2 = array('dua','tiga','empat',12);//object as intersect refference
function intersect($data=NULL)
{
if(!empty($data))
{
$crashed = array();
$crashed2 = array();
foreach($data[0] as $key=>$val)
{
if(!is_array($val))
{
$crashed[$key] = in_array($val,$data[1]);//return true if crashed (intersect)
}
else
{
$crashed2[$key] = intersect(array($val,$data[1]));
}
$crashed = array_merge($crashed,$crashed2);
}
}
return $crashed;
}
$intersect = intersect(array($array1,$array2));
print_r($intersect);
?>
It returns a result like this:
Array ( [angka] => 1 [0] => [1] => 1 [tiga] => Array ( [0] => 1 [1] => [empat] => Array ( [0] => [1] => [2] => 1 ) ) )
It returns true if the value of an array matches with a reference array.
Hope the code can help you.
Have a look here array-intersect.
You could use it like this:
$intersect = $a[0];
for ($i = 1; $i < count($a); $i++)
{
$intersect = array_intersect($intersect, $a[$i]);
}
You can avoid foreach loop by
call_user_func_array('array_intersect',$a);
As the name suggest, I think you can just use array-intersect
From that page:
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
gives
Array
(
[a] => green
[0] => red
)
I know there's a ton of answers but I can't seem to get it right. I have the following arrays and what I've tried:
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array($b,$c,$d) );
But with this I get the error array_combine() [function.array-combine]: Both parameters should have an equal number of elements.
I know it's because the $a's array values aren't keys. That's why I'm coming here to see if I could get some help with an answer that can help me make it look something like this:
array(2) {
[1421]=>array( [0] => teststring1
[1] => teststring3
[2] => teststring5
)
[2241]=>array( [0] => teststring2
[1] => teststring4
[2] => teststring6
)
}
If you have control over creating the arrays, you should create them like:
$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');
$e = array_combine($a, array($b,$c) );
If not, you have to loop over them:
$result = array();
$values = array($b, $c, $d);
foreach($a as $index => $key) {
$t = array();
foreach($values as $value) {
$t[] = $value[$index];
}
$result[$key] = $t;
}
DEMO
Here is a one-liner in a functional coding style. Calling array_map() with a null function parameter followed by the "values" arrays will generate the desired subarray structures. array_combine() does the key=>value associations.
Code (Demo)
var_export(array_combine($a, array_map(null, $b, $c, $d)));
Output:
array (
1421 =>
array (
0 => 'teststring1',
1 => 'teststring3',
2 => 'teststring5',
),
2241 =>
array (
0 => 'teststring2',
1 => 'teststring4',
2 => 'teststring6',
),
)
Super clean, right? I know. It's a useful little trick when you don't have control of the initial array generation step.
Here's a new version of array_merge_recursive which will handle integer keys. Let know how it goes.
$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );
$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);
function array_merge_recursive2() {
$args = func_get_args();
$ret = array();
foreach ($args as $arr) {
if(is_array($arr)) {
foreach ($arr as $key => $val) {
$ret[$key][] = $val;
}
}
}
return $ret;
}