php array merge with value of parent array - php

I need to merge an array with value of parent array.
$testArr=unserialize('a:6:{s:5:"queue";a:2:{i:6;s:1:"5";i:5;s:1:"2";}s:3:"sum";a:2:{i:6;s:3:"765";i:5;s:3:"2.1";}s:7:"sumAccD";a:2:{i:6;s:3:"543";i:5;s:3:"3.1";}s:7:"sumAccC";a:2:{i:6;s:2:"54";i:5;s:3:"3.3";}s:7:"comment";a:2:{i:6;s:12:"test comment";i:5;s:6:"111222";}s:3:"yt0";s:0:"";}');
$ret = array();
foreach ($testArr as $pkey => $pval) {
if (is_array($pval)) {
foreach ($pval as $pvkey => $pvval) {
$ret[$pvkey] = array($pkey => $pvval);
}
}
}
echo '<pre>', print_r($ret), '</pre>';
In this case it prints out
Array
(
[6] => Array
(
[comment] => test comment
)
[5] => Array
(
[comment] => 111222
)
)
1
Unfortunally it print out only comment. I need to add other rows: queue,sum,sumAccD,sumAccC. Array must look like this:
Array
(
[6] => Array
(
[queue] => 5
[sum] => ''
....
[comment] => test comment
)
[5] => Array
(
[queue] => 2
[sum] => 2.1
....
[comment] => 111222
)
)
1
Please help merge them.
Thanks.

Look at this line:
$ret[$pvkey] = array($pkey => $pvval);
You're assigning the key to a new array every time, overwriting what was previously there.
In your case, 'comment' is the last key that is processed, so that's going to be the only key in the final array.
Instead of this, you could define a new array only once outside the inner for, like this:
$ret[$pvkey] = array();
And then assign your values to that array in the inner for loop as you would normally do (so no more creating arrays there!)

Problem solved by replacing
$ret[$pvkey] = array($pkey => $pvval);
with
$ret[$pvkey][$pkey] = $pvval;

Related

Deleting complex array element

So I want to delete an array element from a JSON array based on an id in a sub-array. I know it sounds weird. Here's an example of the array. I want to delete the entire array [0] based on the [dealer][id] array where the [id] = 20220 in this example.
Array
(
[results] => Array
(
[offset] => 1
[length] => 15
[data] => Array
(
[0] => Array
(
[dealer] => Array
(
[id] => 20220
[name] => apple
)
)
)
)
}
In reality there are a lot more elements in the [results] array. I'm not sure how to go about it.
Any help is greatly appreciated!
Loop thru data key first then check if dealer id matches the searched id
$id = 20220;
foreach ($array['results']['data'] as $key => $value) {
if ($value['dealer']['id'] == $id) {
unset($array['results']['data'][$key]);
}
}
use array_filter,
$array['results']['data'] = array_filter($array['results']['data'], function($v){return $v['dealer']['id'] != 20220;});

Put a key of end of an array in php

I have this array:
Array
(
[0] => Array
(
[date] => 2016-03-08
[value] => Array
(
[key_1] => Array
(
[test_1] => 1
[test_2] => 10
[test_3] => 1000
[test_4] => 200
)
[key_2] => Array
(
[test_1] => 1
[test_2] => 15
[test_3] => 1500
[test_4] => 100
)
)
)
Now I have another array :
Array
(
[key_3] => Array
(
[test_1] =>
[test_2] =>
[test_3] =>
[test_4] => 1
)
)
I want to add this last array in the first array.
I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?
You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.
You can use either
// NB! array_push() just adds the values, key 'key_3' is removed
array_push($ymlParsedData[0]['value'], $a_big_gift);
or
// will keep key 'key_3'
$ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
or
// use array_merge() instead
$ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
A complicated answer, but this might solve your issue:
$key_name = array_keys($a_big_gift)[0];
$ymlParsedData[0]['value'][$key_name] = $a_big_gift[$key_name];
echo '<pre>'; print_r($ymlParsedData); exit;
Note: For making it dynamic and for more than one value of $a_big_gift, you need to loop it and achieve your result.
Try this
array_push($ymlParsedData[0]['value'], $a_big_gift['key_3']);

PHP - Incrementally add array value

Basically I have this from a query:
Array
(
[0] => Array
(
[period] => 2014-03
[val] => 2.19
[type] => TypeX
),
[1] => Array
(
[period] => 2014-03
[val] => 2.02
[type] => TypeY
)
)
Using a foreach loop to populate another array we will call DATA, I want the to get the following:
Array
(
[TypeX] => Array
(
[2014-03] => 2.19
)
[TypeY] => Array
(
[2014-03] => 2.02
)
)
The whole thing is looped because my query needs to run each time for a different period. Current exemple, second loop would run for 2014-04. My problem is when I arrive for the 2nd time at my DATA array, I want this:
Array
(
[TypeX] => Array
(
[2014-03] => 2.19
[2014-04] => 1.10
)
[TypeY] => Array
(
[2014-03] => 2.02
[2014-04] => 4.74
)
)
My code is roughly like this:
$data = array();
foreach($graph_period as $period){
$rows = Queryfunction($period,$WHERE,$byType);
foreach($rows as $row){
$data[$row['type']] = array($row['period']=>$row['val']);
}
}
Because the key of the first level (TypeX, TypeY) are the same, the valei is overwritten. How am I to append the array instead of overwriting ?
foreach($rows as $row){
$data[$row['type']][$row['period']] = $row['val']);
}
You only need to change a tiny bit. Inside the foreach where you set the value:
if( !isset($date[$row['type']]) ) {
$data[$row['type']] = array();
}
$data[$row['type']][$row['period']] = $row['val'];
So, you create the array only if it does not exist.

Create from an array who have multiple arrays - only one key => value

I have a big problem and I can't resolve it,
So I have my array :
Array
(
[0] => Array
(
[id] => 34
[groupe_id] => 4
[object_id] => 4
)
[1] => Array
(
[id] => 35
[groupe_id] => 4
[object_id] => 5
)
)
Now I want to create another array call $test for get the array in this forme:
Array
(
[object_id] = 4
[object_id] = 5
)
I tried but no results:
$test = array();
foreach($aObjectsGroupe as $object){
$test[] = array(
'object_id' => $object['object_id']
);
}
You can't have duplicates of the same key in a PHP array. It kind of defeats the purpose of keys. I can't think of a reason to have identical keys, as you would be unable to reference an individual element of the array by key anyways, because there are more than one.
Why not just make an array called $object_ids, and just have a normal indexed array of the all of the object_ids from the other array?
$object_ids = array();
foreach ($aObjectsGroupe as $object) {
$object_ids[] = $object['object_id'];
}

Most Efficient Way to Delete Nested Array Element

Say I have the following:
Array(
[0] => Array
(
[id] => 1
[item] => first item
)
[1] => Array
(
[id] => 3
[item] => second item
)
[2] => Array
(
[id] => 5
[item] => third item
)
)
I want to delete the item with id = 5. I know I can loop through the array and unset, but I'm hoping for a more direct/efficient solution.
If you cannot make the IDs the keys of the outer array (then you could simply use unset($arr[5]);), looping over the array is indeed the way to dg.
foreach($arr as $key => $value) {
if($value['id'] === 5) {
unset($arr[$key]);
break;
}
}
Another option would be using array_filter - that's less efficient though since it creates a new array:
$arr = array_filter($arr, function($value) {
return $value['id'] !== 5;
});
Why don't you create the array with the keys set as the ID's? E.g:
Array(
[1] => Array
(
[id] => 1
[item] => first item
)
[3] => Array
(
[id] => 3
[item] => second item
)
[5] => Array
(
[id] => 5
[item] => third item
)
)
You can then write:
<?php
unset($array[5]); // Delete ID5
?>
For Multi level nested array
<?php
function remove_array_by_key($key,$nestedArray){
foreach($nestedArray as $k=>$v){
if(is_array($v)){
remove_array_by_key($key,$v);
} elseif($k==$key){
unset($nesterArray[$k]);
}
}
return $nestedArrat;
}
?>
The most efficient way would be to have 2 arrays.
ID => Index
Index => Object (your current array)
Search for ID in your ID => Index helper array and the value will be the Index for your main array, then unset them both.

Categories