Recursively iterate over an array to change specific key's value - php

I have an array like this:
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
as you can see there are inner arrays inside $a
Now, I have a list of keys, example:
key1
key4
key6
I need a script that search if those key exists, and if exists change their values.
I Need to change their values with base64_encode($value_of_the_key)
so Maybe a callback that get the current value and convert it using base64_encode() function.
COuld someone help me?
I'm tring to see the current php functions but it seems there is not ones that do this thing.
THanks
EDIT:
Using the follow code i can get the keys in the callback....but the problem is:
How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
function test($item, $key)
{
echo "$key. $item<br />\n";
}
array_walk_recursive($a, 'test');

array_walk_recursive() with callback supplied should help. More info here.

Related

PHP Multidimensional Array Merge based on keys

i have this two arrays:
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
The keys on each of them are the id for the client, the first one is the amount owed and the second one is the amount payed, i want to achieve the following:
$mergedArrays = [
'1' => [
'owed' => 285.52,
'payed' => 123.44
],
'2' => [
'owed' => 427.76,
'payed' => 48.32
]
];
I was wondering if there's a PHP function to do so, i tried with array_merge_recursive but it just makes an array with the four elements together.
Any help would be really appreciated.
you can loop in the first array and merge the second according to keys
foreach($array1 as $key => $val) {
$mergedArrays[$key] = array('owed' => $val, 'payed' => $array2[$key]);
}
sample
$array1 = [
'1' => 285.52,
'2' => 427.76
];
$array2 = [
'1' => 123.44,
'2' => 48.32
];
$final_arr = array_map(function($a1, $a2) {
return array(
'owed' => $a1,
'paid' => $a2
);
}, $array1, $array2);
$final_arr = array_combine(array_keys($array1), $final_arr);
var_dump($final_arr);
Based on the comment, it seems you're looking for built-in PHP functions to do the task for you rather than going for traditional looping. But the looping method provided by Fabio is the simplest one you could go for without any other complicated approaches. I've tried my best to provide you the solution using the core PHP functions. Hope you're happy with it!

take php string which contains square brackets as string not array

I have a weird problem here. I'm using an associative array in php (using cakePHP) which has the following form:
$my_array = array(
'data['a']['b'] => 'value1',
'data['b']['c'] => 'value2',
'data['b']['d'] => 'value3',
'data['e'] => array(
'data['e1']['e2']' => 'value3',
'data['e1']['e3']' => 'value4'));
The problem I'm having is that
'data['e1']['e2']' => 'value3' and 'data['e1']['e3']' => 'value4'
are taken like an array like this:
'data['e1']' => array(
['e2'] => 'value3',
['e3'] => 'value4');
I don't want these to be taken as arrays, I want them to be taken as key and value of the array 'data['e']'. As a matter of fact, I want all the elements of the arrays $my_array and 'data['e']' to be taken as keys and values of the corresponding array (not as arrays).
Any help please?
P.S This seems to happen only when I do a debug on cakePHP, if I don't use cakePHP everything seems to be fine and "data" comes from a cURL posted data to cakePHP
Your code is invalid PHP. My best guess is that it should look like this:
$my_array = array(
$data['a']['b'] => 'value1',
$data['b']['c'] => 'value2',
$data['b']['d'] => 'value3',
$data['e'] => array(
$data['e1']['e2'] => 'value3',
$data['e1']['e3'] => 'value4'));
Please show us the contents (for instance, using print_r) of $data.
POSTed data in a certain syntax is automatically parsed into $_POST as array. If you want to get the raw input, use file_get_contents('php://input'). See http://php.net/manual/en/wrappers.php.php.

Setting variables from a Key Value Array PHP

I have an array:
array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
Is there a built in function in PHP that will make 3 variables e.g. $myVar1, $myVar2, $myVar3 do that when i echo $myVar1; it retuens 'value1'
Obviously I can loop the array and set them accordingly (so please no answers with this), but if there is a internal PHP function that would be great!
extract() is the function:
Import variables into the current symbol table from an array...
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table...
$array = array(
'myVar1' => 'value1',
'myVar2' => 'value1',
'myVar3' => 'value3',
);
echo $array['myVar1'];
echo $array['myVar2'];
Is that what you mean?

referring to sister element inside array in php

I want to do something like the following (key3 is a combination of 1+2):
$a = array(
'key1' => 5,
'key2' => 10,
'key3' => $a['key1']+$a['key2'] // want it to be 15
);
How can I do this? Do i need to refer from outside the array and then merge them? Because this doesn't work!
Thanks in advance,
Maurice
You won't be able to initialize it that way because PHP hasn't finished going through and initializing the entire array yet, so it can't use its other values yet.
You'll have to do it after initializing the first two elements:
$a = array(
'key1' => 5,
'key2' => 10
); // At this point, $a is ready for use
$a['key3'] = $a['key1'] + $a['key2']; // Or simply = array_sum($a);
The solution proposing to update the table afterward are good, but you can also use variables for your data :
$var1 = 5;
$var2 = 10;
$a = array(
'key1' => $var1,
'key2' => $var2,
'key3' => $var1 + $var2
);
Why not do:
$a = array(
'key1' => 5,
'key2' => 10,
'key3' => 0
);
$a['key3'] = $a['key1']+$a['key2'];
It would have the desired result. However, I have a feeling that you are trying to make the third member change when one of the first two change, and I'm afraid you won't be able to accomplish that easily.

Sorting an array by its keys by another array

I need to sort an array by its keys based on the order of the values in another array.
Simple example:
$sort_array = array( 'key1', 'key2' );
$array_that_needs_sorting = array( 'key2' => 'value2', 'key1' => 'value1' );
After sorting, the array should be:
array( 'key1' => 'value1', 'key2' => 'value2' );
If you know the $sort_array keys are all present in the array that needs to be sorted, you can do this:
$sorted = array_merge(array_flip($keys), $unsorted);
where $keys is $sort_array and $unsorted is $array_that_needs_sorting.
You might take a look at Sort an Array by keys based on another Array?. It should give you an idea on how to accomplish that.
array_merge(array_combine($sort_array, array_fill(0, count($sort_array), null))
, $array_that_needs_sorting);

Categories