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
)
)
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;
}
Am a beginner in yii2. In my program there is an array named Place. It contains the values like:
Array
(
[0] => Array
(
[0] => Aluva
[1] => Paravoor
)
[1] => Array
(
[0] => Ernakulam
[1] => Paravoor
)
[2] => Array
(
[0] => Aluva
[1] => Ernakulam
)
[3] => Array
(
[0] => Kottuvally
[1] => Paravoor
)
)
How can I retrieve each element from this array?
You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});
using foreach ?
foreach($array as $row){
foreach($row as $key => $val){
echo $val.'<br>';
}
}
Sample code -
<?php
$data = array(
0 => array(0=>'Aluva', 1=>'Paravoor'),
1 => array(0=>'Ernakulam', 1=>'Paravoor'),
2 => array(0=>'Aluva', 1=>'Ernakulam'),
3 => array(0=>'Kottuvally', 1=>'Paravoor')
);
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value1) {
echo '<br>'.$value1;
}
}
$var = $Place[0][1];
Where is $var is now "Paravoor"
how can i crawl this array with a for loop or foreach ,
this is the content of a single array:
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If you mean what I think you mean...
try this:
$tempArray = array();
foreach($myarray as $k => $v){
foreach($v as $ke => $val ){
$tempArray[] = $val;
}
}
sort($tempArray);
Or if you want to avoid duplicates try:
$temp = array_unique($myArray);
sort($temp);
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);