how can i break a multidimensional array into keys and values - php

I really have no clue to achieve the outpu i want. I tried adding these brackets [] next to my key but it made the array more complex
this is how my array look like
Array
(
[0] => Array
(
[0] => Array
(
[white] => 1
[black] => 1
)
)
)
this is what i would like
Array
(
[0] => Array
(
[0] => Array
(
[0] => white
[1] => 1
)
[1] => Array
(
[0] => black
[1] => 1
)
)
)

It's pretty simple:
$arr = [[['white' => 1, 'black' => 1]]];
$items = $arr[0][0];
$arr[0] = [];
foreach ($items as $k => $v) {
$arr[0][] = [$k, $v];
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[0] => white
[1] => 1
)
[1] => Array
(
[0] => black
[1] => 1
)
)
)

Related

Push in array if key exists

I have the following multidimensional array:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1560000096000,47886
)
)
[5] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[6] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
I'm currently using a foreach loop to extract the values from the array:
$result = [];
$nameData = [];
foreach($data as $key => $itemData)
{
}
I want array look like this:
Array
(
[0] => Array
(
[name] => Viettel
[data] => Array
(
[0] => 1559881644000,500
[1] => 1560000096000,47886
)
)
[1] => Array
(
[name] => FPT
[data] => Array
(
[0] => 1559994465000,172
)
)
[2] => Array
(
[name] => MobiFone
[data] => Array
(
[0] => 1559997314000,11164
)
)
[3] => Array
(
[name] => Vietnamobile
[data] => Array
(
[0] => 1559993699000,1246
)
)
[4] => Array
(
[name] => VinaPhone
[data] => Array
(
[0] => 1559997553000,11132
)
)
[5] => Array
(
[name] => VNPT
[data] => Array
(
[0] => 1559993066000,84
)
)
)
It seems you are looking for groupby the array by name.
You can use foreach with array_key_exists and array_push
$groupBy = [];
foreach($a as $v){
array_key_exists($v['name'], $groupBy) ?
array_push($groupBy[$v['name']]['data'], $v['data'][0])
:
($groupBy[$v['name']] = $v)
;
}
If you want to rearrange the keys of array, you can use array_values
print_r(array_values($groupBy));
Working DEMO :- https://3v4l.org/ASWDV
You can just loop over the input array, adding values to the output if they don't exist and merging that data values if they do:
$result = array();
foreach ($data as $itemData) {
if (($k = array_search($itemData['name'], array_column($result, 'name'))) !== false) {
$result[$k]['data'] = array_merge($result[$k]['data'], $itemData['data']);
}
else $result[] = $itemData;
}
print_r($result);
Output is as you desire (too long to reproduce here)
Demo on 3v4l.org
If you are adding a single value to the array you can just access to it with something like this:
$result[0]['data'][1] = 1560000096000,47886

Remove array with single element from assosiative array

I have an array
Array
(
[0] => Array
(
[0] => 96
[1] => ML based
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
I want to remove
Array
(
[0] => Array
(
[0] => 98
)
)
Array
(
[0] => Array
(
[0] => 99
)
)
from this array
I tried:
$data = array_map('array_filter', $rowData);
unset($data[0][0]);
Expected output:
Array
(
[0] => Array
(
[0] => 96
[1] => ML based )
[7] => Open
)
)
Array
(
[0] => Array
(
[0] => 97
[1] => Application
[7] => Open
)
)
any help would be Appreciated .
array_filter() will work. Try -
array_filter($array, function ($a) {
return count($a[0]) == 3; // return array with 3 elements only
});
Working code

getting column data from array

I have an array as follows
Array
(
[0] => Array
(
[route_id] => 2/2A
[direction] => right
[bus_stop_count] => 1
[bus_id] => Array
(
[0] => 1000
[1] => 1002
)
)
[1] => Array
(
[route_id] => 1
[direction] => right
[bus_stop_count] => 1
[bus_id] => Array
(
[0] => 1004
)
)
)
I want to get an array for bus_id like the following
Array
(
[0] => 1000
[1] => 1002
[2] => 1004
)
Here is what i tried so far
$bus_ids = array_column($array, 'bus_id');
Array
(
[0] => Array
(
[0] => 1000
[1] => 1002
)
[1] => Array
(
[0] => 1004
)
)
That should work:
$a = array(...);
call_user_func_array('array_merge', array_column ($a, 'bus_id'))
You can use a for loop:
$newArr = Array();
foreach ($arr as $value) $newArr = array_merge($newArr, $value["bus_id"]);

How remove duplicate multidimensional array by first dimension

i have a 2-dimensionl array such as this:
Array
(
[0] => Array
(
[0] => talk
[1] => amount
)
[1] => Array
(
[0] => base
[1] => amazing
)
[2] => Array
(
[0] => talk
[1] => filter
)
[3] => Array
(
[0] => label
[1] => any
)
[4] => Array
(
[0] => talk
[1] => amount
)
[5] => Array
(
[0] => tour
[1] => any
)
)
how remove duplicate value by first dimension and result such as this:
Array
(
[0] => Array
(
[0] => talk
[1] => amount
)
[1] => Array
(
[0] => base
[1] => amazing
)
[2] => Array
(
[0] => label
[1] => any
)
[3] => Array
(
[0] => tour
[1] => any
)
)
there is much help about remove duplicate values in the array, but I didn't see any sample about remove duplicates by specific dimension.
<?php
function uniqueArray($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = uniqueArray($value);
}
}
return $result;
}
?>
This will helps you
Note: not tested

Compare 2 arrays that exist in the same multidimensional array

I have an associative array as such:
Array
(
[0] => Array
(
[0] => Array
(
[0] => name=>Jose
[1] => email=>jo#example.com
)
[1] => Array
(
[0] => name=>Adriana
[1] => email=>add#example.com
)
)
[1] => Array
(
[0] => Array
(
[0] => name=>Jose
[1] => email=>juniper#example.com
)
[1] => Array
(
[0] => name=>Maria
[1] => email=>maria#example.com
)
)
)
I want to collect all email addresses of all members. Jose has 2 email addresses, Maria 1, and Adriana 1. How do I compare 2 arrays that co-exist in a multidimensional array?
I want something like:
Array
(
[0] => Array
(
[0] => name=>Jose
[1] => firstemail=>jo#example.com
[2] => secondemail=>juniper#example.com
)
[1] => Array
(
[0] => name=>Adriana
[1] => firstemail=>add#example.com
)
[2] => Array
(
[0] => name=>Maria
[1] => firstemail=>maria#example.com
)
)
thanks in advance!
Like this
$finalarray = array();
foreach ($inputarray as $subarray) {
foreach($subarray as $entry) {
if (!array_key_exists($entry["name"], $finalarray)) {
$finalarray[$entry["name"]] = array();
}
array_push($finalarray[$entry["name"]], $entry["email"]);
}
}

Categories