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.
Related
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;
}
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
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];
});
I want to join the array keys to a filepath with the value at the end as file itself (the array below is a "filetree")
Array (depth, size and keynames are dynamic):
[0] => bla.tif
[1] => quux.tif
[foo] => Array (
[bar] => Array (
[lorem] => Array (
[1] => ipsum.tif
[2] => doler.tif
)
)
)
[bar] => Array (
[qux] => Array (
[baz] => Array (
[1] => ipsum.tif
[2] => ufo.tif
)
)
)
This result would be fine:
[0] => bla.tif
[1] => quux.tif
[2] => foo/bar/lorem/ipsum.tif
[3] => foo/bar/lorem/doler.tif
[4] => bar/qux/baz/ipsum.tif
[5] => bar/qux/baz/ufo.tif
Maybe there is also a pure PHP solution for that. I tried it with array_map but the results weren't fine enough.
I would use a recursive function to collapse this array. Here's an example of one:
function collapse($path, $collapse, &$result)
{
foreach($collapse AS $key => $value)
{
if(is_array($value))
{
collapse($path . $key . "/", $value, $result);
continue;
}
$result[] = $path . $value;
}
}
And here's how to use:
$result = array();
$toCollapse = /* The multidimentional array */;
collapse("", $toCollapse, $result);
and $result would contain the "imploded" array
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;
}