I'm using Laravel 4 and I have the following problem. I make an Ajaxrequest and I have a class GpsController with function a called GPSOnline.
public function GPSOnline () {
$gpsonline=File::get( Config::get('app.driverspath'));
$fstep = explode("|", $gpsonline);
foreach($fstep as $k => $v){
if(strlen($v)>0){
$sstep [] = explode(",", $v);
}
}
$array = array();
foreach ($sstep as $v) {
$tmp['ch'] = substr($v[0],0,1);
$tmp['poz'] = substr($v[0],1);
$tmp['la'] = $v[1];
$tmp['lo'] = $v[2];
$array[] = $tmp;
}
// echo json_encode($array);
return Response::json(array($array));
}
But it didn't work.What's wrong in my code?!
Related
I ran into this question on a different site, after attempting to work on it for an hour (could be my Sunday brains) I gave up. The question is: If there is a function foo:
function foo(){}
The function can be called as (arguments can be >= 2, where the last is always the value and the previous are part of the array).
So calling the function as:
foo('arg1', 'value');
Should result in:
$array['arg1'] = 'value';
The same if it has more than 1 argument:
foo('arg1', 'argx', 'argz', 'value');
Should produce:
$array['arg1']['argx']['argz'] = 'value';
This was my sad attempt:
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = array_shift($items);
// Construct first element
$array = array($array => array());
foreach ($items as $el) {
insert_last($array, $value);
}
return $array;
}
function insert_last(&$array, $value)
{
$copy = $array;
while (true) {
$keys = array_keys($copy);
$last = $copy[$keys[count($copy)-1]];
var_dump($last);
if (empty($last)) {
$last = $value;
break;
}
$copy = $last;
}
var_dump($array, $copy);
}
Pretty sure there is probably an easier solution that I just can't think of at the moment. Thanks!
function foo()
{
$items = func_get_args();
$value = array_pop($items);
$array = [];
$arrayPtr = &$array;
foreach ($items as $element) {
$arrayPtr[$element] = null;
$arrayPtr = &$arrayPtr[$element];
}
$arrayPtr[$element] = $value;
return $array;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
Via recursion using call_user_func_array()
<?php
function foo() {
$items = func_get_args();
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
edit: same thing without func_get_args() but using a variadic function
<?php
function foo(...$items) {
if ( 1==count($items) ) {
return array_shift($items);
}
else {
$key = array_shift($items);
return array( $key=>call_user_func_array('foo', $items) );
}
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
What about something like
function foo()
{
$args = func_get_args();
$items = array_pop($args);
foreach (array_reverse($args) as $item) {
$items = array($item => $items);
}
return $items;
}
var_dump(foo('arg1', 'argx', 'argz', 'value'));
Demo
Is there a simple way to take a single-dimensional array and convert it to a multi-dimensional array based on the spaces, or any character(s), in the keys?
$arr['foo1'] = 'bar1';
$arr['foo2'] = 'bar2';
$arr['foo3 tier1' ] = 'bar3';
$arr['foo4 tier1' ] = 'bar4';
and turn it into
$arr['foo1'] = 'bar1';
$arr['foo2'] = 'bar2';
$arr['foo3']['tier1'] = 'bar3';
$arr['foo4']['tier1'] = 'bar4';
you can always do some sort of foreach loop
$newarr = array();
foreach ($arr as $key => $value) {
$output = explode(' ',$key,2);
if(count($output)) {
$newarr[$output[0]][$output[1]] = $value;
} else {
$newarr[$key] = $value;
}
}
That code will only work for one space but you can expand it to multiple spaces, something like
function pivot($arr,$delimeter) {
$return = array();
foreach ($arr as $key => $value) {
$output = explode($delimeter,$key,2);
if(count($output)) {
if(strpos($output[1],$delimeter) > 0) {
$return[$output[0]] = pivot(array($output[1]=>$value),$delimeter);
} else {
$return[$output[0]][$output[1]] = $value;
}
} else {
$return[$key] = $value;
}
}
return $return;
}
I wasn't so clear in my first question, so i deleted it and here is a reformulation;
I have those arrays:
$open = array(array("FAI1","34"),array("FAI2","34"),array("FAI3","34"));
$click = array(array("FAI2","52"),array("FAI1","68"),array("FAI3","99"));
$unsubscribe = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$def_sent = array(array("FAI1","34",24),array("FAI2","34",23),array("FAI3","34",27));
$SB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
$HB = array(array("FAI2","103"),array("FAI3","67"),array("FAI1","102"));
I searched for a function to merge them and get a result like this:
$result = array(array("FAI1",34,68,102,34,24,102,102)
,array("FAI2","34",23.....),
array("FAI3","34",27....));
and to do this, i used the function, in the php online documentation, and this is the function
function array_merge_recursive() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base);
while (list($key, $value) = #each($array)) {
if (is_array($value) && #is_array($base[$key])) {
$base[$key] = array_merge_recursive($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
But instead of getting the result above i got this:
FAI1|34
FAI2|34
FAI3|34
FAI2|52
FAI1|68
FAI3|99
...
So i need some help to reformulate this function to get the expected result.
Try this function:
function array_merge_rec() {
$arrays = func_get_args();
$result = array();
foreach ($arrays as $arg) {
if (is_array($arg)) {
foreach ($arg as $item) {
if (!isset($result[$item[0]])) {
$result[$item[0]] = $item;
} else {
$result[$item[0]][] = $item[1];
}
}
} else {
echo "$arg skippend because it isn't array\n";
}
}
return array_values($result);
}
Does it help?
Given an array, I would like a flattened version of the array keys. Each array key would need the 'path' of the array, to that point, appended with an underscore.
An example explains this best.
$arr = array("location"=>0,"details"=>array("width"=>0,"height"=>0,"level"=>array("three"=>0)));
function answer($arr) {....}
the answer function would return this:
array("location","details_width","details_height","details_level_three");
UPDATE:
Here is the work in progress. It will accept an array and return the array keys, but with no depth:
function recursive_keys($input)
{
$output = array_keys($input);
foreach($input as $sub){
if(is_array($sub)){
$output = array_merge($output, recursive_keys($sub));
}
}
return $output;
}
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$results = array();
foreach ($ritit as $leafValue) {
$path = array();
foreach (range(0, $ritit->getDepth()) as $depth) {
$path[] = $ritit->getSubIterator($depth)->key();
}
$results[] = join('_', $path);
}
function recursive_keys(array $array, array $path = array()) {
$result = array();
foreach ($array as $key => $val) {
$currentPath = array_merge($path, array($key));
if (is_array($val)) {
$result = array_merge($result, recursive_keys($val, $currentPath));
} else {
$result[] = join('_', $currentPath);
}
}
return $result;
}
Demo here: http://codepad.viper-7.com/WQ3UYI
From:
$arr = array(array('key1'=>'A',...),array('key1'=>'B',...));
to:
array('A','B',..);
$output = array();
foreach ($arr as $array_piece) {
$output = array_merge($output, $array_piece);
}
return array_values($output);
On the other hand, if you want the first value from each array, what you want is...
$output = array();
foreach ($arr as $array_piece) {
$output[] = array_unshift($array_piece);
}
But I'm thinking you want the first one.
Relatively simple conversion by looping:
$newArray = array()
foreach ($arr as $a) {
foreach ($a as $key => $value) {
$newArray[] = $value;
}
}
Or, perhaps more elegantly:
function flatten($concatenation, $subArray) {
return array_merge($concatenation, array_values($subArray));
}
$newArray = array_reduce($arr, "flatten", array());
John's solution is also nice.
Something like this should work
<?
$arr = array(array('key1'=>'A','key2'=>'B'),array('key1'=>'C','key2'=>'D'));
$new_array = array();
foreach ($arr as $key => $value) {
$new_array = array_merge($new_array, array_values($value));
}
var_export($new_array);
?>
If you want all the values in each array inside your main array.
function collapse($input) {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf = array_merge($buf, collapse($i));
}
else $buf[] = $input;
return $buf;
}
Above is a modified unsplat function, which could also be used:
function unsplat($input, $delim="\t") {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf[] = unsplat($i, $delim);
}
else $buf[] = $input;
return implode($delim, $buf);
}
$newarray = explode("\0", unsplat($oldarray, "\0"));