This is array value
$value = Array (
[0] => Array (
[A] => -33.884667407851
[F] => 151.16123199463
)
[1] => Array (
[A] => -33.876686661215
[F] => 151.20414733887
)
)
This is my array and i want the output like this
[ [-33.866139529765626,151.26079559326172],[-33.866139529765626,151.26903533935547] ]
This will do the trick,
$your_array = array (
0 => array (
'A' => -33.884667407851,
'F' => 151.16123199463
) ,
1 => array (
'A' => -33.876686661215,
'F' => 151.20414733887
)
);
$your_output = json_encode(array_map('array_values', $your_array));
Output:
[[-33.884667407851,151.16123199463],[-33.876686661215,151.20414733887]]
Try (where $foo is your starting array):
$result = array();
foreach ($foo as $arr) {
$new_arr = array();
foreach ($arr as $key => $val) {
array_push($new_arr, $val);
}
array_push($result, $new_arr);
}
print_r($result);
Related
I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
I want to convert multidimensional array to single dimension array with combine keys, maybe my question is not understandable, so that i explain with example:
I have array like following :
// JSON
{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":1},"f":{"a":1}}}
Array
(
[a] => Array
(
[a] => Array
(
[a] => 1
)
[b] => Array
(
[a] => 1
)
[c] => Array
(
[a] => 1
)
[d] => Array
(
[a] => 1
)
[e] => Array
(
[a] => 1
[b] => 1
[c] => 1
[d] => 1
)
[d] => Array
(
[a] => 1
)
)
)
Outout that i want
Array
(
[0] => '[a][a][a]'
[1] => '[a][b][a]'
[2] => '[a][c][a]'
[3] => '[a][d][a]'
[4] => '[a][e][a]'
[5] => '[a][e][b]'
[6] => '[a][e][c]'
[7] => '[a][e][d]'
[8] => '[a][f][a]'
)
Hope that understandable
I try many ways but not success, please help
How to Flatten a Multidimensional Array?, this answer is not the solution of my question, please compare
You may try my custom code to get array as you want
<?php
$json = '{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":1},"f":{"a":1}}}';
$array = json_decode($json, true);
$returnarray = [];
$i = 0;
$main = array_keys($array);
foreach($array['a'] as $key => $val) {
$arr = [];
array_push($arr, $main[0]);
$keysarr = array_keys($val);
array_push($arr, $key);
if(count($keysarr) > 1) {
for($j = 0; $j < count($keysarr); $j++) {
array_push($arr, $keysarr[$j]);
$returnarray[$i] = $arr;
array_pop($arr);
$i++;
}
} else {
array_push($arr, $keysarr[0]);
$returnarray[$i] = $arr;
$i++;
}
}
echo "<pre>";
print_r($returnarray);
?>
Not an efficient one but do the job if your array is exactly like that the way you have shown in the example.
<?php
$dataSource = array
(
'a' => array
(
'a' => array
(
'a' => 1
),
'b' => array
(
'a' => 1
),
'c' => array
(
'a' => 1
),
'd' => array
(
'a' => 2
),
'e' => array
(
'a' => 1,
'b' => 1,
'c' => 1,
'd' => 1
),
'f' => array
(
'a' => 1
)
)
) ;
echo "<pre>";
print_r($dataSource);
echo "</pre>";
$resultData = [];
foreach($dataSource as $key => $val)
{
foreach($val as $key1 => $val1)
{
foreach($val1 as $key2 => $val2)
{
$resultData[] = '['.$key.']['.$key1.']['.$key2.']';
}
}
}
echo "<pre>";
print_r($resultData);
echo "</pre>";
This solve my issue, its tricky but work for my case:
$json = '{"a":{"a":{"a":1},"b":{"a":1},"c":{"a":1},"d":{"a":1},"e":{"a":1,"b":1,"c":1,"d":{"a":1}},"f":{"a":1}},"b":{"a":{"a":1}}}';
$data = json_decode($json, true);
function array_str($data = array()) {
$result = [];
$data = explode('&', urldecode(http_build_query($data)));
foreach ($data as $value) {
$main = explode('[', $value)[0];
$result[] = preg_replace('/' . $main . '/', '[' . $main . ']', explode('=', $value)[0], 1);
}
return $result;
}
I want to add two associative arrays to the same array key or take out only from the array the keys I need like the example below, the keys I need in the fields array $field = [a,b];.
Lets say I have the blow array:
array1 = Array
(
[key1] => Array
(
[a] => a
[b] => b
[c] => c
)
[key2] => Array
(
[a] => a
[b] => b
[c] => c
)
)
$field = [a,b];
$x = [];
foreach ($array1 as $key) {
foreach ($fields as $field) {
$x[$key['id']] = array($field => $key[$field]);
}
}
print_r($x);
output:
Array
(
[key1] => Array
(
[b] => b
)
[key2] => Array
(
[b] => b
)
)
I need:
Array
(
[key1] => Array
(
[a] => a
[b] => b
)
[key2] => Array
(
[a] => a
[b] => b
)
)
Basically you are assigning a new array everytime, what you need to do is the below code where in you are only assigning the new index and not the array.
<?php $array1 = array(
'key1' => array
(
'a' => "a",
'b' => "b",
'c' => "c"
),
'key2' => array
(
'a' => "a",
'b' => "b",
'c' => "c"
)
);
$fields = array('a','b');
$x = [];
foreach ($array1 as $key => $value) {
foreach ($fields as $field) {
if(array_key_exists($field,$array1[$key]))
$x[$key][$field] = $value[$field];
}
}
print_r($x);
<?php
$arr = array(
'key1'=>array('a'=>'a','b'=>'b','c'=>'c'),
'key2'=>array('a'=>'a','b'=>'b','c'=>'c'),
'key3'=>array('c'=>'c')
);
$include = array('a','b');
foreach($arr as &$res)
{
$res = array_filter($res, function($key) use ($include) {
return in_array($key, $include);
}, ARRAY_FILTER_USE_KEY);
}
unset($res);
print_r($arr);
?>
Test Results
[akshay#localhost tmp]$ php test.php
Array
(
[key1] => Array
(
[a] => a
[b] => b
)
[key2] => Array
(
[a] => a
[b] => b
)
[key3] => Array
(
)
)
You can do like this, by reconstruct another multidimensional with only keys that are in $field array.
Here is the code :
$field = array('a','b');
$x = array();
foreach ($array1 as $k => $key) {
foreach ($fields as $k2 => $field) {
if(in_array($k2, $field))
$x[$k][$k2] = $field;
}
}
print_r($x);
Please help to solve this issue
i want to change this
[UI Testing] => Array
(
[0] => Array
(
[0] => 0
[1] => 70
)
[1] => Array
(
[0] => 40
[1] => 0
)
)
TO
[UI Testing] => Array
(
[0] => Array
(
[0] => 40
[1] => 70
)
)
Thanks.
Example
$myArray = array();
$myArray[] = array(0,70);
$myArray[] = array(40,0);
$sumArray = array();
foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
array_key_exists( $id, $sumArray ) ? $sumArray[$id] += $value : $sumArray[$id] = $value;
}
}
print_r($sumArray);
Output:
Array
(
[0] => 40
[1] => 70
)
Array can be filtered to remove 0 valued indexes and array_reduce to merge array. An example below:
$arr = array('UI Testing' => array( array(0, 70), array(40, 0),) );
$new_arr['UI Testing'] = array_reduce($arr['UI Testing'], function($old, $new) {
return array_filter($new, function( $v ){ return $v > 0 ? $v : null; }) + $old;
}, []);
print '<pre>';
print_r($new_arr);
print '</pre>';
Output
Array
(
[UI Testing] => Array
(
[0] => 40
[1] => 70
)
)
I have the following array:
$arr = array(
array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
And I would like the change the way it looks to something more like this.
$arr = array(0=>array("date"=>"2014-03-13","a"=>1,"b"=>2),
1=>array("date"=>"2014-03-12","a"=>4,"b"=>3));
Heres what I have so far.
$keys = array();
$vals = array();
foreach($arr as $row){
foreach($row as $key=>$val){
$keys[]=array("date"=>$key);
foreach($val as $keys=>$values){
$vals[]=array($keys=>$values);
}
}
}
The array which gets the dates works fine so in the below example the $keys array works however the $vals array does not work as intended and instead gives me an array similar to this.
Array ( [0] => Array ( [a] => 1 )
[1] => Array ( [b] => 2 )
[2] => Array ( [a] => 4 )
[3] => Array ( [b] => 3 )
[4] => Array ( [a] => 4 )
[5] => Array ( [b] => 3 ) )
Any help to get the array desired is appreciated.
It should be
$result = array();
foreach($arr as $row) {
foreach($row as $date=>$values) {
$values['date'] = $date;
$result[] = $values;
}
}
$arr = array(
array(
"2014-03-13" => array("a"=>1, "b"=>2)
),
array(
"2014-03-12" => array("a"=>4, "b"=>3)
),
);
$result = array();
foreach ($arr as $item) {
foreach ($item as $date => $values) {
$result[] = array_merge(array('date' => $date), $values);
}
}
var_dump($result);
Here is the code:
$arr = array(
0=>array("2014-03-13"=>array("a"=>1,"b"=>2)),
array("2014-03-12"=>array("a"=>4,"b"=>3))
);
$finalarray=array();
foreach($arr as $k=>$v){
foreach($v as $kk=>$vv){
$newarray=array();
$newarray["date"]=$kk;
foreach($vv as $kkk=>$vvv){
$newarray[$kkk]=$vvv;
}
}
$finalarray[]=$newarray;
}
echo "<pre>";
print_r($finalarray);
echo "</pre>";
**Here is the output:**
Array
(
[0] => Array
(
[date] => 2014-03-13
[a] => 1
[b] => 2
)
[1] => Array
(
[date] => 2014-03-12
[a] => 4
[b] => 3
)
)