How to merge arrays in one variable in PHP [duplicate] - php

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

Related

Php reverse array is not working [duplicate]

This question already has answers here:
Reverse an associative array with preserving keys in PHP
(4 answers)
Closed 5 years ago.
I am developing a new website, and I have a quetion.
Input array:
Array ( [1319] => ####,[1316] => ###)
I have an array and I want to revese him, after the reverse the array would be like this:
Expected output:
Array ( [1316] => ###,[1319] => ####)
but when i'm using array_reverse function, it doesnt work for me, I got this array:
Array ( [0] => ###,[1] => ####)
Why it is happen?
For preserving keys you just pass second parameter to true in array_reverse.
Try this code snippet here
$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
you can try this:
$a = []; //your array
$keys = array_keys($arr);
$values = array_values($arr);
$rv = array_reverse($values);
$newArray = array_combine($keys, $rv);

Combine php arrays into one [duplicate]

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.

Combine two array values to make them key=>value format [duplicate]

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);

Remove duplicate values from a multidimensional array in PHP [duplicate]

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

How to get the value of an array that's inside another array [duplicate]

This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.

Categories