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)
Related
This question already has answers here:
Add a static value to each row in a 2d array
(3 answers)
php - Add value with key to all the elements [duplicate]
(1 answer)
How can I push single element in existing array of JSON? [duplicate]
(5 answers)
Closed 11 days ago.
I have the following multidimenson array
array (
'count' => 386,
'report' =>
array (
'uuid' => '183a3956-9425-43da-845c-2839c30a951b',
'name' => 'OnlyScrumFND',
'Have' =>
array (
0 =>
array (
'uuid' => '00ad6013-4109-4940-a711-4f8fb5389e8c',
),
1 =>
array (
'uuid' => 'd651a86d-beac-498a-85a0-75ce62f28f4e',
),
),
),
)
I would like to add some info to the Array in a sublevel
foreach ($OUTPUT AS &$have['report']['Have']) {
$have['name'] = "something";
}
But it is not woking, Any hint? thanks rob
You are very close. you can try with following solution.
foreach ($OUTPUT['report']['Have'] as &$item) {
$item['name'] = "something";
}
I am using & operator here to modify the original array element in foreach loop instead of copied one.
Find the implementation below .
you can check here
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);
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.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Custom key-sort a flat associative based on another array
(16 answers)
Closed 8 years ago.
is it possible to sort an array by keys using an custom order ?
i have an array with strings that represent the order.
$order = array('ccc','aaa','xxx','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'BBB' => 'value11','ddd' => 'value31')
now i want the array to be sorted with the elemnts with the key 'ccc' at the first position, the nthe elements with the key aaa ... and at the end should be the elements that are not in the sortlist.
is this possible ?
edit: the second 'CCC' was my fault - sorry
See this in action https://eval.in/118734
<?php
$order = array('ccc','xxx','aaa','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'ddd' => 'value31');
$temp = array();
foreach($order as $o) {
if(array_key_exists($o, $myarray)) {
$temp[$o] = $myarray[$o];
}
}
$new = array_merge($temp, $myarray);
print_r($new);
?>
I was just having a think about this as I was having a similar issue with array_multisort() and ksort().
However in your case if the snippet of code is correct, will not be possible as the second 'ccc' key with a value of 'value11' will overwrite the previous one.
php > $myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3','ccc' => 'value11','ddd' => 'value31');
php > print_r($myarray);
Array
(
[ccc] => value11
[aaa] => value2
[xxx] => value3
[ddd] => value31
)
.
This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 4 years ago.
There's an array in php
<?php
$array=array("a"=>"123","b"=>"234","c"=>"345");
array_shift($array);
//array("0"=>"234","1"=>"345");
?>
If I use this function, then key value gets changed. I want my key value to remain the same. How can I remove first element without affecting array key values.
My answer should be like
array("b"=>"234","c"=>"345");
Note:Please do not use foreach(); I want to do this by existing array functions in php
array_splice function is working for above array. But consider the below array
<?php
$array = Array
(
'39' => Array
(
'id' => '39',
'field_id' => '620'
),
'40' => Array
(
'id' => '40',
'field_id' => '620',
'default_value' => 'rrr',
));
array_splice($array, 0, 1);
print_r($array);
?>
It is showing answer as follows:
Array ( [0] => Array ( [id] => 40 [field_id] => 620 [default_value] => rrr ) )
May I know the reason?? Will array_splice() work only for single dimensional array?? Now key value is reset...
In case you do not know what the first item's key is:
// Make sure to reset the array's current index
reset($array);
$key = key($array);
unset($array[$key]);
$array=array("a"=>"123","b"=>"234","c"=>"345");
unset($array["a"]) ;
var_dump($array) ;
Also, what version of PHP do you use?
array_shift works fine for me with string-indexed arrays and I get the expected result.
The solution for this question is as follows:
<?php
unset($array[current(array_keys($array))]);
?>
It removes the first element without affecting the key values..
<?php function array_kshift(&$array)
{
list($k) = array_keys($array);
$r = array($k=>$array[$k]);
unset($array[$k]);
return $r;
}
// test it on a simple associative array
$array=array("a"=>"123","b"=>"234","c"=>"345");
array_kshift($array);
print_r($array);
?>