I need to remove the outer key from array and reindex the left array. I have array in this format:
Array (
[0] => Array (
[0] => Array(
[id] => 123
),
[1] => Array (
[id] => 144
)
),
[1] => Array (
[0] => Array (
[id] => 354
)
)
)
I want to format this to this one:
Array (
[0] => Array (
[id] => 123
),
[1] => Array (
[id] => 144
),
[2] => Array (
[id] => 354
)
)
You can use a splat operator [PHP 5.6+]
$result = array_merge(...$array);
You can use call_user_func_array() with array_merge:
$array = call_user_func_array('array_merge', $array);
Please try my custom function and get your results.
print_r(array_fun($your_array_variable));
function array_fun($c) {
if (!is_array($c)) {
return FALSE;
}
$result = array();
foreach ($c as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_fun($value));
} else {
$result[][$key] = $value;
}
}
return $result;
}
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I have an array, $result, which outputs:
Array ( [0] => Array ( [username] => boyzerooooo [0] => boyzerooooo ) [1] => Array ( [username] => mynameiszero23 [0] => mynameiszero23 ) [2] => Array ( [username] => yournameiszero [0] => yournameiszero ) [3] => Array ( [username] => zerotolerance [0] => zerotolerance ) )
I use the following function, to turn it into a one dimensional array:
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
Which then outputs to:
Array ( [username] => zerotolerance [0] => boyzerooooo [1] => mynameiszero23 [2] => yournameiszero [3] => zerotolerance )
My problem is that zerotolerance is being outputted twice, and I'm not sure why. When I use foreach to output the array, zerotolerance is outputted twice. How can I fix this?
Use array_unique and array_values..
return array_unique(array_values($result));
The array_values returns all the values from the array and the array_unique returns only the unique values from the array.
i have an array $mainArray of arrays and i would like to remove / undset the arrays that vave keys with no value.
here is my array:
Array
(
[0] => Array
(
[msg_id] => 203
[comment] => Array
(
[0] => Array
(
[com_id] =>
)
)
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
[2] => Array
(
[com_id] =>
)
)
)
[2] => Array
(
[msg_id] => 201
[comment] => Array
(
[0] => Array
(
[com_id] => 198
)
[1] => Array
(
[com_id] =>
)
)
)
)
In this case i would like to look inside comment array arrays and see if there are any of them that have empty values. The best case scenario would be to remove the comment array entirely if all sub arrays are empty.
nut im of with leaving the comment hey there just null
this array should become:
Array
(
[0] => Array
(
[msg_id] => 203
)
[1] => Array
(
[msg_id] => 202
[comment] => Array
(
[0] => Array
(
[com_id] => 196
)
[1] => Array
(
[com_id] => 197
)
)
)
[2] => Array
(
[msg_id] => 201
)
)
any ideas on how to proceed?
thanks.
array_filter() is what you're after. Particularly a recursive version. The following was taken from a comment on the PHP Doc:.
function array_filter_recursive($array, $callback = null) {
foreach ($array as $key => & $value) {
if (is_array($value)) {
$value = array_filter_recursive($value, $callback);
}
else {
if ( ! is_null($callback)) {
if ( ! $callback($value)) {
unset($array[$key]);
}
}
else {
if ( ! (bool) $value) {
unset($array[$key]);
}
}
}
}
unset($value);
return $array;
}
Use php's unset() to unset any array key/value.
More info on this link http://in3.php.net/unset
in your case the code can be, (i have not tested it. but let me know if you have any problem and i can fix it)
function unsetCommentFromArray($mainArray) {
foreach($mainArray as $key => $value) {
foreach($value['comment'] as $k => $v) {
if(empty($v['com_id'])) {
unset($mainArray[$key]['comment'][$k]);
}
}
}
return $mainArray;
}
$array = array_map(function ($i) {
$i['comment'] = array_filter($i['comment'], function ($c) { return $c['com_id']; });
return array_filter($i);
}, $array);
Requires PHP 5.3 or higher.
I have an array like this...
Array
(
[0] => Array
(
[id] => 10651
[userid] => 079eb9f4b9eb573f6aec93ce97ed1e7f
)
[1] => Array
(
[id] => 74315
[userid] => 1283612836
)
[2] => Array
(
[id] => 74315
[userid] => asydk12893489123
)
)
is there a php function that returns me an array of values of the key userid if I make a call like this...
func($arr_name, $key_name);
Regards
No, but you can make one.
function getFromKey($array, $keyName) {
$return = array();
foreach ($array as $value) {
if (isset($value[$keyName]))
$return[] = $value[$keyName];
}
return $return;
}