I had php multi dimensional array
Array
(
[0] => Array
(
[WorkHrs] => 9826
[Focus_Date] => 2010-02-10
)
[1] => Array
(
[WorkHrs] => 9680
[Focus_Date] => 2010-02-11
)
)
and I want to convert it in Javascript to
myArray = [['2010-02-10', 9826],['2010-02-11', 9680]];
$jsArray = array();
foreach($myArray as $array) {
$jsArray[] = array($array['Focus_Date'], (int) $array['WorkHrs']);
}
echo json_encode($jsArray);
echo json_encode(array_map(array_values, $arr));
EDIT: To get it in the specified order:
function to_focus_work_array($arr)
{
return array($arr['Focus_Date'], $arr['WorkHrs']);
}
echo json_encode(array_map('to_focus_work_array', $arr));
json_encode
That's pretty much exactly what json_encode does. Input is a PHP-array (other datatypes accepted), output is what you describe.
have you tried the json_encode()?
refer to http://php.net/manual/en/function.json-encode.php
Related
i have an array in php like the one given below
$each_session_time=array("2700","3356","3278","5452");
the issue is i want to convert each element in this array into time
i tried using
date("h:i:s",$each_session_array);
but it didn't work because date function doesn't work with arrays...
so can anyone help me in converting the each element into time....I shall be very thankful to you
$each_session_time=array();
for ($i=0; $i<sizeof($array_in_time_str) ; $i++) {
$each_session_time[$i]=$array_out_time_str[$i]-$array_in_time_str[$i];
}
If I understood your request, then this is what you need : using array_map here is a demo
<?php
$each_session_time=array(2700,33522,2222278,1111452);
$res = array_map(function($elem){
return date ("H:i:s",$elem);
},$each_session_time);
print_r($res);
?>
you can try this.
$each_session_time=array("2700","3356","3278","5452");
foreach ($each_session_time as $key => $value) {
$timeArray[] = date("h:i:s",$value);
}
echo "<pre>";print_r($timeArray);
Output:
Array
(
[0] => 06:15:00
[1] => 06:25:56
[2] => 06:24:38
[3] => 07:00:52
)
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
Array
(
[0] => 5
[1] => 4
[2] => 1
[3] => 2
)
Expecting result
Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
$results = [];
foreach($array as $key=>$value){
$results[$key] = arsort($value);
}
echo "<pre>";
print_r($results);
Please suggest how do we can sort associative array i did try but does not work for me please guide
As per your "expected results' it seems like you don't wish to maintain the keys. If that's the case then you can just use sort.
Something like this..
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
sort($array);
print_r($array);
Just do
sort($array);
Also check the PHP documentation if you need further customization: http://php.net/manual/en/function.sort.php
var_dump( array_reverse($array,false));
you don't need to use foreach or sort ,you can just use array_reverseinstead ,avery simple way
You don't need to iterate using foreach for sorting
Just use sort for sorting array
$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
sort($array);
print_r($array);
This will not maintain array keys and if you want arrays keys to be same just replace sort with asort in above code
I have these code:
$arr = array($check);
When I try to
echo "<pre>";
die(print_r($arr));
it gives me result in the Firebug
<pre>Array
(
[0] => Array
(
[0] => 2
[1] => 1
)
)
Now, I want an output that will give me '2,1'.
How could I do that in PHP? Do I need to use implode?
Yes you need to implode:
echo implode(',', $check);
You don't need to put it inside an array:
$arr = array($check); // no need
i have this multidimensional array.
Array
(
[0] => Array
(
[car] => Toyota
)
[1] => Array
(
[car] => Ford
)
[2] => Array
(
[car] => Isuzu
)
[3] => Array
(
[car] => Chevrolet
)
)
i want to put them into indexed array like this..
Array = ("Toyota", "Ford", "Isuzu", "Chevrolet");
$result = array_map(function($item)
{ return $item['car']; }, $array);
Some functional approach. array_map
P.S. PHP has no indexed arrays
foreach($yourArray as $carArray)
{
$result[]=$carArray["car"];
}
And your understanding of indexed arrays is wrong. That example output you've shown contains all those values, not indexes, and since you didn't specify an index it will start from 0 and so on.
Option using array_map(), assuming your initial array is $arr
$new_arr = array_map(function($x){return $x['car'];}, $arr);
See demo
try using foreach and array_values, then save it into empty array. Hope it helps.
I have two arrays, one is generated by using explode() on a comma separated string and the other is generated from result_array() in Codeigniter.
The results when doing print_r are:
From explode():
Array
(
[0] => keyword
[1] => test
)
From database:
Array
(
[0] => Array
(
[name] => keyword
)
[1] => Array
(
[name] => test
)
)
I need them to match up so I can use array_diff(), what's the best way to get them to match? Is there something other than result_array() in CI to get a compatible array?
You could create a new array like this:
foreach($fromDatabase as $x)
{
$arr[] = $x['name'];
}
Now, you will have two one dim arrays and you can run array_dif.
$new_array = array();
foreach ($array1 as $line) {
$new_array[] = array('name' => $line);
}
print_r($new_array);
That should work for you.