Multidimensional Array to single dimension array with combine keys - php

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;
}

Related

How to return each path and its hash from multidimensional array?

I'm trying to return each path of a multidimensional array along with its hash value. I've used this solution:
function newMethod($tree) {
$hash = "";
$final = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed) || is_object($mixed)) {
$results = newMethod($mixed);
foreach ($results as $k => &$v) {
$hash = md5($key . $v);
if(is_array($mixed)) {
$final[][$hash] = array($key => $v);
}
elseif(is_object($mixed)) {
$final[][$hash] = array($key => (object)$v);
}
}
unset($results);
} else {
$hash = md5($key. $mixed);
$final[][$hash] = array($key => $mixed);
}
}
return $final;
}
Using this function provides the hash value for each section of the path separately and doesn't connect them together. How can I change this solution to get an output like this:
Array
(
[0] => Array
(
[Hash of first path] => Array
(
[a] => b
)
)
[1] => Array
(
[Hash of second path] => Array
(
[c] => stdClass Object
(
[d] => Array
(
[1] => 11
)
)
)
)
)
...
The simple tree and a part of output are as follows:
$new = [
'a' => 'b',
'c' => (object)[
'd' => [1 => 11, 2 => 12],
'f' => 'Hello',
],
];
//output
[0] => Array
(
[187ef4436122d1cc2f40dc2b92f0eba0] => Array
(
[a] => b
)
)
[1] => Array
(
[595448f796fd1d2d5f192949d79ba9df] => Array
(
[c] => stdClass Object
(
[f801394efb8c6ca75f1ab6a639556520] => Array
(
[d] => Array
(
[698d51a19d8a121ce581499d7b701668] => Array
(
[1] => 11
)
)
)
)
)
)
...

php - how to add two associative arrays to the same array key

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);

Change key on array_chunk php

I have array based POST like this :
Array
(
[condition] => Array
(
[0] => 1
)
[container] =>
[cleaning] => Y
[owner] => Eagletainer
[last_cargo] => 1
[vessel] =>
[insulation] => 1
[tare] =>
[gross] =>
[capacity] =>
[unit_type] => IMO 1
[date_of_manu] =>
[name_manu] =>
[last25] =>
[cert25] =>
[last5] =>
[cert5] =>
[list2_item_0] => 1
[list2_kondisi_0] => 9
[list3_item_0] => 15
[list3_kondisi_0] => 3
[comments] =>
)
My case is, I want to chunk a lot of those element array into another array for insert_batch in my database.
This is the php code to chunk those array:
public function get_partition($array, $p, $c) {
$partition = array_slice($array, $p);
array_pop($partition);
return $chunk = array_chunk($partition, $c);
}
Now, use it,
$detail = $this->get_partition($this->input->post(), 17, 2);
The result is :
Array
(
[0] => Array
(
[0] => 1
[1] => 9
)
[1] => Array
(
[0] => 15
[1] => 3
)
)
My question in, how to change the key [0] and [1] into another key like [ID] and [CODE_DAMAGE]
I want them looked like this :
Array
(
[0] => Array
(
[ID] => 1
[CODE_DAMAGE] => 9
)
[1] => Array
(
[ID] => 15
[CODE_DAMAGE] => 3
)
)
Re-loop the array and achieve your desired result like this:
$detail = $this->get_partition($this->input->post(), 17, 2);
$new_array = array();
$count = 0;
foreach($detail as $row){
$new_array[$count]['ID'] = $row[0];
$new_array[$count++]['CODE_DAMAGE'] = $row[1];
}
If the indexes were already correct you could pass the optional third parameter: http://php.net/array_chunk
<?php
$array = array(0 => array(0 => 123, 1 => 1234), 1 => array(0 => 123, 1 => 1234));
$updatedArray = array();
foreach ($array as $k => $v) {
$updatedArray[$k]['ID'] = $v[0];
$updatedArray[$k]['CODE_DAMAGE'] = $v[1];
}
?>
Try this, I hope this helps.
Try this:
foreach($detail as $key => $value){
if($key == 0){
$detail['ID'] = $value;
unset($detail[$key]);
}
if($key == 1){
$detail['CODE_DAMAGE'] = $value;
unset($detail[$key]);
}
}
Just an example add your array to this code..it will work fine
$main = Array(Array(1,9),Array(15,3));
$b = array('ID', 'CODE_DAMAGE');
$new_array = array();
foreach($main as $subarray)
{
$new_array[] = array_combine($b, $subarray);
}
echo'<pre>';print_r($new_array);

how to print the multidimensional array value

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);

Manipulate multidimensional array with php

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
)
)

Categories