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

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

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

Multidimensional Array to single dimension array with combine keys

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

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

Build associative array based on values of another associative array

I'm looking for an elegant way to turn this array:
Array (
[foo] => 1
[bar] => 1
[zim] => 3
[dib] => 6
[gir] => 1
[gaz] => 3
)
Into this array:
Array (
[1] => Array ( foo, bar, gir ),
[3] => Array ( zim, gaz ),
[6] => Array ( dib )
)
Note:, there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array.
Thanks!
$input = array(
'foo' => 1,
'bar' => 1,
'zim' => 3,
'dib' => 6,
'gir' => 1,
'gaz' => 3
)
$output = array();
foreach ( $input as $k => $v ) {
if ( !isset($output[$v]) ) {
$output[$v] = array();
}
$output[$v][] = $k;
}
I think this will do it just fine:
foreach ($arr1 as $k => $val) $arr2[$val][] = $k;
where $arr1 is the original array outputting the new array to $arr2.

Categories