How to convert multi dimensional array to single dimensional array in php ? - php

I want convert this multi dimensional array to single dimensions array
array([ca] => Array (
[0] => Array ( [userid] => 1 )
[1] => Array ( [userid] => 10 )
[2] => Array ( [userid] => 14 )
[3] => Array ( [userid] => 16 )
[4] => Array ( [userid] => 17 )
[5] => Array ( [userid] => 18 )
[6] => Array ( [userid] => 25 )
)
Convert to following array
array ([ca] =>
array(
[0] => 1
[1] => 10
[2] => 14
[3] => 16
[4] => 17
[5] => 18
[6] => 25
))
Thanks in advance

$res = [];
foreach ($array as $key => $val) {
$res['ca'][] = $val['userid'];
}
print_r($res);

And an example with array_walk:
array_walk(
$array['ca'],
function(&$item, $key) {
return $item = $item['userid'];
}
);

Related

How to get value of all same keys of multidimentional array in php

Basically i want to loop through a multidimensional associative array to get a simple indexed array
Here is my master array
Array
(
[0] => Array
(
[user_id] => 2
[children] => Array
(
[0] => Array
(
[user_id] => 5
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 6
[children] => Array
(
)
)
)
)
[1] => Array
(
[user_id] => 3
[children] => Array
(
[0] => Array
(
[user_id] => 7
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 8
[children] => Array
(
)
)
)
)
[2] => Array
(
[user_id] => 4
[children] => Array
(
[0] => Array
(
[user_id] => 9
[children] => Array
(
[0] => Array
(
[user_id] => 10
[children] => Array
(
[0] => Array
(
[user_id] => 11
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 12
[children] => Array
(
)
)
[2] => Array
(
[user_id] => 13
[children] => Array
(
)
)
)
)
)
)
)
)
)
Here is the result i want to achive
$userArray= array(2,3,4,5,6,7,8,9,10,11,12,13);
basically i just want all the user_id key value inside 1 single indexed array.
Till now i have tried this code
$keys = array_keys($masterArray);
for($i = 0; $i < count($masterArray); $i++) {
echo $keys[$i] . "{<br>";
foreach($masterArray[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
You can use array_walk_recursive() which will iterate over the leaf nodes in a multidimensional array, check for the key being user_id and if so, add it to a list of ids...
$ids = [];
array_walk_recursive($masterArray, function ( $value, $key) use (&$ids) {
if ( $key == "user_id" ) {
$ids[] = $value;
}
});
print_r($ids);
which with the sample data, gives...
Array
(
[0] => 2
[1] => 5
[2] => 6
[3] => 3
[4] => 7
[5] => 8
[6] => 4
[7] => 9
[8] => 10
[9] => 11
[10] => 12
[11] => 13
)
You just have to travel recursively to collect all user IDs as below:
<?php
function collectUserIDs($data,&$result){
foreach($data as $current_data){
$result[] = $current_data['user_id'];
collectUserIDs($current_data['children'],$result);
}
}
$result = [];
collectUserIDs($data,$result);
print_r($result);

how to make incremental value in hierarchal array in Php?

Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...

Explode each values of Array element

I have an array like :
Array
(
[2] => 2,6
[3] => 1
[4] => 14
[5] => 10
[6] => 8
)
I want to explode each element of an array and return a new array using array_map, so that I can avoid using loop, and creating extra function to call back.
O/p should be like :
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
You can use
$result = array_map(function($val) {
return explode(',', $val);
}, $input);
Which will result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
This will work on PHP >= 5.3 with support for anonymous functions.
You can also do
$result = array_map('str_getcsv', $input);
This will also result in
Array
(
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 14
)
[5] => Array
(
[0] => 10
)
[6] => Array
(
[0] => 8
)
)
Try following code
$newArr = array_map(function($val, $key){
return explode(",", $val);
}, $arr);
$data = array(2 => '2,6',3 => '1',4 => '14',5 => '10',6 => '8');
foreach($data as $key => $val) {
$new = explode(',',$val);
$data[$key] = $new;
}
$output = $data;
echo '<pre>';
print_r($output);

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Find value assoaction in array

i have a multi dimension array with sub array having repeated values of 'eduHisRowId' like:
Array
(
[0] => Array
(
[eduHisRowId] => 4
[repOrderId] => 15
)
[1] => Array
(
[eduHisRowId] => 5
[repOrderId] => 16
)
[2] => Array
(
[eduHisRowId] => 5
[repOrderId] => 17
)
[3] => Array
(
[eduHisRowId] => 6
[repOrderId] => 18
)
[4] => Array
(
[eduHisRowId] => 7
[repOrderId] => 19
)
[5] => Array
(
[eduHisRowId] => 7
[repOrderId] => 20
)
[6] => Array
(
[eduHisRowId] => 8
[repOrderId] => 21
)
)
Now i want sort out these repeated values such that i could be able to check that the record present on index '[1] => Array' is associated with the record which is present on index '[2] => Array' & this associated relation will also be in array format like:
Array
(
[0] => Array
(
[0] => 4
[1] => Array
(
[0] => 15
)
)
[1] => Array
(
[0] => 15
[1] => Array
(
[0] => 16
[0] => 17
)
)
[2] => Array
(
[0] => 6
[1] => Array
(
[0] => 18
)
)
[3] => Array
(
[0] => 7
[1] => Array
(
[0] => 19
[0] => 20
)
)
[4] => Array
(
[0] => 8
[1] => Array
(
[0] => 21
)
)
)
where 0th index of innre mos array will contain 'eduHisRowId' value & the array on 1st index will contain 'repOrderId' values.
Thanks in advance...
Can I suggest a different solution? What about an array structure that looks like:
Array
(
[4] => Array
(
[0] => 15
)
[5] => Array
(
[0] => 16
[1] => 17
)
)
The keys are the eduHisRowId values and the value is an array of corresponding repOrderId values.
Creating this array would go like follows:
function consolidate($item, $key, $array) {
$rowId = $item['eduHisRowId'];
if(!array_key_exists($rowId, $array)) {
$array[$rowId] = array();
}
$array[$rowId][] = $item['repOrderId'];
}
$result = array();
array_walk($dataArray, 'consolidate', &$result);
$dataArray is your multidimensional array, the resulting array is in $result.
Reference: array_walk(), array_key_exists()

Categories