This question already has answers here:
PHP - Merge two arrays (same-length) into one associative?
(4 answers)
Closed 8 years ago.
I have two array like these.
array("a","b","c");
array("1","2","3");
I have to add those items like that way.
$c=array("a"=>"1","b"=>"2","c"=>"3");
how can i retrieve each inner array by this way.
echo $c[0];
Use array_combine()
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
<?php
$a = array("a","b","c");
$b = array("1","2","3");
$c = array_combine($a, $b);
print_r($c);
?>
The above example will output:
Array
(
[a] => 1
[b] => 2
[c] => 3
)
$new_array = $array;
array_unshift($new_array, $value1);
array_push($new_array, $value2);
Use array_merge method for combining two arrays . Array_merge($arr1'$arr2);
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 8 months ago.
I have an array as shown bottom
array (
[det1] => 1,2,3,4
[det2] => 5,6
);
So i want to join items of this array and convert array to an string like bottom
$uru = 1,2,3,4,5,6
How can i do this work?
Did you try like this with implode()
<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>
DEMO: https://3v4l.org/UBQrv
You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.
In that case loop the array and merge the new array with the subarray.
$arr = array (
"det1" => [1,2,3,4],
"det2" => [5,6]
);
$new= [];
foreach($arr as $sub){
$new = array_merge($new, $sub);
}
echo implode(",",$new); // 1,2,3,4,5,6
https://3v4l.org/NaDXN
This can be easily done by extracting into array_merge and then joining the resulting array.
$data = [
[1,2,3,4,5],
[6,7,8],
];
echo join(',', array_merge(... $data));
Output:
1,2,3,4,5,6,7,8
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have two types of arrays:
1:
$array1["a"][] = "value1";
$array1["a"][] = "value2";
$array1["b"][] = "value3";
2:
$array2["0"] = "a";
What I need now is to somehow find difference between these two arrays. I need to filter out array1 by key, which is located in array2 value. I have tried doing the following:
array_diff(array_keys($array1), array_values($array2));
But I get the following error on that line:
ErrorException Array to string conversion
Any ideas?
Something like this?
foreach ($array1 as $key => $value)
if( array_search ($key , $array2 ))
unset($array1[$key]);
If $array1 needs to have the values, you just need to put the diff in $array1 :
$array1 = array_diff(array_keys($array1), array_values($array2));
Depending on how you constructed your arrays, it should work. The following code (based on your question) worked:
<?php
$array1=array("a" => array(),"a" => array(),"b" => array());
$array2=array("0"=>"a");
print_r(array_keys($array1));
echo("<br/>");
print_r(array_values($array2));
echo("<br/>");
print_r(array_diff(array_keys($array1), array_values($array2)));
>
This results in:
Array ( [0] => a [1] => b )
Array ( [0] => a )
Array ( [1] => b )
This question already has answers here:
Flatten a multidimensional array and remove duplicate values [duplicate]
(3 answers)
Closed 8 years ago.
How can I remove duplicate values from a multidimensional array in PHP like below.
I tried Remove duplicate value in multidimensional array. But did not solve my problem.
Array(
[0] => outdoor
[1] => indoor
)
Array(
[0] => indoor
)
Result should be a single array like below :
array(outdoor,indoor);
finally i found the result from Remove duplicate value in multidimensional array. I'll share them for other users.
$result = array_unique(call_user_func_array('array_merge',$result2));
Use array_unique to remove duplicates from a single array.
Use array_merge to combine arrays.
Try:
array_unique(array_merge($array1,$array2), SORT_REGULAR);
$array = array(array("outdoor","indoor"),array("indoor"));
$result = array_unique($array);
print_r($result[0]);
Demo
Try this:
<?php
$Arr = array(array('outdoor','indoor'),array('indoor'));
$result = array_unique($Arr);
$newArr = $result[0];
echo '<pre>';
print_r($newArr);
?>
The result will be
Array
(
[0] => outdoor
[1] => indoor
)
--
Thanks
This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
Closed 9 years ago.
I have got an array
$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);
Now, i am going to unset the 1 and 2 keys.
unset($array[1]); unset($array[2]);
How to do if i want an array
array(0=>'c', 1=>'d');
Try This:
$array = array_values($array);
Try
unset($array[1]);
unset($array[2]);
$array = array_values($array);
This works because array_values returns the array you gave, but with increasing keys starting at 0.
Have you tired to use something like that ?
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
array_values
$myarray = array_values($myarray);
btw here is a good answer posted
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Merge 2 Arrays and Sum the Values (Numeric Keys)
I have two arrays both have same keys, i want to add key values,
Example
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
want to create new array which add these two array values
like
$arr3 = array("first"=>30,"second"=>50);
How can i do that in one line, i can do it using forach or other technique, but wondering if some one know a good way of doing?
Example for string keys (note that no error checking is done!):
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
$arr3 = $arr1;
array_walk($arr3, function (&$val, $key, $foo) { $val += $foo[$key]; }, $arr2);
print_r($arr3);
/*Array
(
[first] => 30
[second] => 50
)*/