Converting multidimentional array into single array and removing the keys [duplicate] - php

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 months ago.
array (size=1551884)
0 =>
array (size=1)
'entity_id' => string '131813' (length=6)
1 =>
array (size=1)
'entity_id' => string '213808' (length=6)
2 =>
array (size=1)
'entity_id' => string '712885' (length=6)
is it possible to convert it to single array without the key 'entity_id' without a loop?
array
0 =>
131813
1 =>
213808
2 =>
712885
I have tried this one :
call_user_func_array('array_merge', $array)
but somehow is only returning 1 element
UPDATE:
here are the benchmark results from the given answers to this question:
php version > 5.6
array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132
php version > 7
array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068

This is also possible with array_column.
$result = array_column($your_array, 'entity_id');

You can do this very easily with array_map like this:
$result = array_map(function($value) {
return $value['entity_id'];
}, $originalArray);
Working example: https://3v4l.org/JOEMI
Of course you could also do it with a foreach loop:
$result = [];
foreach($originalArray AS $entity) {
$result[] = $entity['entity_id'];
}
Working example: https://3v4l.org/9J5XH
I prefer the first option personally.
Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.

Related

in PHP how to change the key of an array with tke key of a nested array [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 2 years ago.
I have an array with nested associative array for each element, like this:
array (size=47)
0 =>
array (size=1)
'name' => 'Saitama'
1 =>
array (size=1)
'email' => 'Saitama#onepunch.man'
...
I want to build a function that return an associative array like this:
array (size=47)
'name' => 'Saitama',
'email' => 'Saitama#onepunch.man'
...
I tried with array_map() and array_combine() but I cannot manage to do this job.
Thank you very much
Pasquale
It's called flattening and just merge the nested arrays:
$result = call_user_func_array('array_merge', $array);
Obviously this only works with unique keys, as duplicates will be overwritten.

how to convert array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?
Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213
In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);
For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}

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

Array display without key [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I am trying to display an array without key item. Just want to display array values without key.
Here is my sample code I tried
$myList = array(
0 =>array(
"product_id"=> 8085
),
1 =>array(
"product_id"=> 8087
),
2 =>array(
"product_id"=> 8086
),
3 =>array(
"product_id"=> 8042
),
);
$newList = array();
foreach($myList as $listItem) {
$newList[] = $listItem['product_id'];
}
$a=array();
$a= array_values($newList);
print_r($a);
I want my array like this
$productIds = array(8085,8087,8086,8042);
Here is my sample code link
You're looking for array_column (which is available as of PHP 5.5):
$productIds = array_column($myList, 'product_id');
This gives you:
var_export($productIds);
array (
0 => 8085,
1 => 8087,
2 => 8086,
3 => 8042,
)
Which is exactly what you want:
var_dump($productIds === array(8085,8087,8086,8042)); // bool(true)
print_r function will output the keys. even if you use array_values the array still have indexes as keys.
Just output the the array manually using echo and implode (implode will join array values into a single string using the first parameter character):
echo implode(',', $newList);
Arrays will always have keys. If you want an array, you can get all the values, turn them into one comma separated string, and place that into an array:
$productIds = [implode(',', array_column($myList, 'product_id'))];
var_dump($productIds);
// RESULT:
// array (size=1)
// 0 => string '8085,8087,8086,8042' (length=19)

Filter associative array with keys array [duplicate]

This question already has answers here:
How to filter an associative array comparing keys with values in an indexed array?
(12 answers)
Closed 4 months ago.
I have following arrays:
$keys
array (size=2)
0 => string 'foo' (length=3)
1 => string 'buz' (length=3)
$data
array (size=3)
'foo' => int 1
'bar' => int 2
'buz' => int 3
How to get $data array filtered by $keys values ? Desired output:
array (size=3)
'foo' => int 1
'buz' => int 3
array_intersect_key should be able to help you out here
array_intersect_key($data, array_flip($keys));
The array_flip is needed because array_intersect_key operates on keys, so this makes sure both arrays are in the right format.
DEMO: http://codepad.org/AGpDAZtE

Categories