This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
A variable called $result
When print_r ($result);
It goes:
Array
(
[0] => iPhone
)
Array
(
[0] => iOS
)
Array
(
[0] => Safari
)
Seems like there are 3 Arrays in this $result, but I want to marge it to one and give theme order, I've tried array_merge(), and array_combine(), but none of theme get the result I want, what I really want is:
Array( 'iPhone', 'iOS', 'Safari' )
So I could output one of them using $results[0] or $results[1]...
How can I achieve that? Thanks alot.
Use array_merge with ... splat operator
$result = array_merge(...$array);
OR
$result = call_user_func_array('array_merge', array($a));
For example :- https://3v4l.org/38Q5h
This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.
This question already has answers here:
difference between two arrays
(7 answers)
Closed 6 years ago.
array('5','6','3')
array('3','2','1','5','9','0','6')
I need the elements of 2nd array removing the elements matching from the first array. ie.
array('2','1','9','0')
please help.
you can use array_diff
<?php
$array1=array('5','6','3');
$array2=array('3','2','1','5','9','0','6');
$diff = array_diff($array2, $array1);
echo "<pre>";
print_r($diff);
output:
Array
(
[1] => 2
[2] => 1
[4] => 9
[5] => 0
)
You can use the function array_diff in PHP that will compare arrays and returns the difference
$diffarray= array_diff($array2, $array1);
This question already has answers here:
Combine two arrays
(11 answers)
Closed 6 years ago.
I'm new at PHP so forgive me if this question has been asked. How do you combine two PHP arrays into one? This is the code I'm using that needs to be combined:
<?php $duplicates[] = get_the_ID(); ?>
<?php $images = get_attached_media( 'image', $post->ID );?>
Thanks for the help.
If all you need to do is merge two arrays into one, this should do the trick:
$new_array = array_merge($duplicates, $images);
Array Combine Procedure
Code:
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
Output:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
PHP function 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.
This question already has answers here:
How do I use array_unique on an array of arrays?
(7 answers)
Closed 7 years ago.
I am storing some value in multidimensional array, while storing that iam trying to neglate duplicate array in my values for that i used array_unique function in php. Its working in local, but not working in live can any one help me. I have attached my code below
$ex = $_POST;
$_SESSION["cartdetail"][] = $ex;
$_SESSION["cartdetail"] = array_unique($_SESSION["cartdetail"]);
echo "<pre>";
print_r($_SESSION["cartdetail"]);
echo "</pre>";
outPut
Array
(
[0] => Array
(
[cid] => 7
[cname] => studies
[ctype] => Class Room
[cdate] => 12-1
[ctime] => 09:30 am-06:30 pm
[cdays] => 5
[cprice] => 1
[viewDetails] => start
)
)
When i am trying to store different value in array, its not storing. Its storing only first value in array.Thank for your help
You need to serialize the internal arrays and then unserialize back:
$_SESSION["cartdetail"] = array_map("unserialize", array_unique(array_map("serialize", $_SESSION["cartdetail"])));