turn multidimensional array to normal - php

I have array like this
Array
(
[0] => Array
(
[text] => 1
)
[1] => Array
(
[0] => Array
(
[text] => 2
)
[1] => Array
(
[0] => Array
(
[text] => 3
)
)
[2] => Array
(
[text] => 4
)
)
[2] => Array
(
[text] => 5
)
)
i dont know number of dimensions, there can be many of them. Also key of target subarrays can be not 'text', but its always text key, not numerical. How do i turn this array into array like this?
Array
(
[0] => Array
(
[text] => 1
)
[1] => Array
(
[text] => 2
)
[2] => Array
(
[text] => 3
)
[3] => Array
(
[text] => 4
)
[4] => Array
(
[text] => 5
)
)
UPDATE: Okay, i didnt explain, and didnt understand whole question by myself. The thing is that array is formed by recursion i have one function:
public static function goToAction($action)
{
$actions = array();
$logic = file_get_contents('../../logic/logic.json');
$logic_array = json_decode($logic, true);
unset($logic);
if (!isset($logic_array[$action])) {
return false;
} else {
foreach ($logic_array[$action] as $action) {
$actions[] = self::parseActionType($action);
}
}
return $actions;
}
and then my array ($data) is forming here
public static function parseActionType($actions)
{
$data = array();
foreach ($actions as $key => $action) {
switch ($key) {
case 'goto': {
$goto_actions = self::goToAction($action);
foreach ($goto_actions as $goto_action) {
$data[]= $goto_action;
}
} break;
....
}
}
so these functions can call each other, and there can be recursion, and as I understood when this happens, this code $data[] = $goto_action puts all of received actions in one element, but I need to put each $goto_action in differend element of $data array

You can use array_walk_recursive to traverse all the value. live demo
$result = [];
array_walk_recursive($array, function($v, $k) use(&$result){
$result[] = [$k => $v];
});

Related

Convert Complex array to simple one (for CSV)

I want to convert my complex array to simpler one for exporting that converted simpler array to the CSV file.
Currently my array structure is like:
Array
(
[0] => Array
(
[_source] => Array
(
[block] => Array
(
[0] => Kurud
)
[district] => Array
(
[0] => Dhamtari
)
[state] => Array
(
[0] => Chhattisgarh
)
)
)
[1] => Array
(
[_source] => Array
(
[block] => Array
(
[0] => North-Bangeluru
)
[district] => Array
(
[0] => Bangalore
)
[state] => Array
(
[0] => Karnataka
)
)
)
)
and I want to convert above array to the below given format:
array(
array("block", "district", "state"),
array("Kurud","Dhamtari","Chhattisgarh"),
array("North-Bangeluru","Bangalore","Karnataka")
)
So keys will be the first element and then each element with his data.
This is what I tried:
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result);
}
else {
$result[$key] = $value;
}
}
print_r(result);
thanks in advance...
How about:
$keys = array_keys($arr[0]["_source"]);
$res[] = $keys;
foreach($arr as $e) {
$temp = [];
foreach($keys as $k)
$temp[] = $e["_source"][$k][0];
$res[] = $temp;
}
Reference: array-keys
Live example: 3v4l

PHP How to restructure an array?

I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.

how to unset empty arrays in php?

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.

Recursive PHP function that copies multidimensional array but replaces empty values

I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:
[field_ter] =>
[field_title] => Array
(
[0] => Array
(
[value] =>
)
)
[field_firstnames] => Array
(
[0] => Array
(
[value] => test9
)
)
[field_birth] => Array
(
[0] => Array
(
[value] =>
)
)
[field_postal] => Array
(
[0] => Array
(
[value] =>
)
)
[group_certificates] => Array
(
[0] => Array
(
[_delta] => 0
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
[1] => Array
(
[_delta] => 1
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
)
What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:
function check_empty(&$array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach($array as $key => $value) {
if(is_array($value)) {
check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.
You need to assign the return value of the recursive check_empty:
function check_empty($array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$new_array[$key] = check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
if(is_array($value)) {
check_empty($value);
}
Here you don't return resulting value to anywhere. Probably that's the problem

PHP Function that returns an array of values of keys

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

Categories