I have an array coming as below which is dynamic. Now I want to edit a specific array for "BHD0000000002". I am getting the following array through ajax request. However I cannot edit the inner array element. For example, I am trying to edit the inner array value for the key "BHD0000000002" but unable to fix the issues. Can anyone help sir/madam?
This is the original array:
$budget_detail_array=Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 12212121
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
I want to edit the above array as :
Array
(
[0] => Array
(
[BHD0000000001] => 10000
)
[1] => Array
(
[BHD0000000002] => 5000
)
[2] => Array
(
[BHD0000000003] => 212121
)
[3] => Array
(
[BHD0000000004] => 212121
)
[4] => Array
(
[BHD0000000005] => 212121
)
[5] => Array
(
[BHD0000000006] => 2121
)
[6] => Array
(
[BHD0000000007] => 2121
)
[7] => Array
(
[BHD0000000008] => 21221
)
[8] => Array
(
[BHD0000000009] => 2112212
)
)
So far I have tried with the following code but it is not working. Anyone can help?
foreach($budget_detail_array as $key=>$value){
foreach($value as $keyval=>$val){
if($keyval=='BHD0000000002'){
$val=5000;
}
}
/*if($value[$budget_id]){
$value[$budget_id]=100;
}else{
$value[$budget_id]=700;
}*/
}
print_r($budget_detail_array);exit;
In order to modify an associative array value you can do this:
$budget_detail_array[1]['BHD0000000002'] = 5000;
And then check the result again
print_r($budget_detail_array);
Related
I managed to write the following function which took me all afternoon. I used various sources and managed to cobble something together. I'm a newbie. It works, but I'm sure it can be coded better and more efficiently. If anybody has any ideas..
Here is my array:
Array
(
[0] => Array
(
[database] => oneclick_themes
[disk_usage] => 16384
[users] => Array
(
[0] => oneclick_themes
)
)
[1] => Array
(
[database] => oneclick_wp1
[disk_usage] => 123230
[users] => Array
(
[0] => oneclick_wp1
)
)
[2] => Array
(
[users] => Array
(
[0] => oneclick_wp10
)
[database] => oneclick_wp10
[disk_usage] => 123230
)
[3] => Array
(
[users] => Array
(
[0] => oneclick_wp11
)
[disk_usage] => 123222
[database] => oneclick_wp11
)
[4] => Array
(
[users] => Array
(
[0] => oneclick_wp12
)
[disk_usage] => 123230
[database] => oneclick_wp12
)
[5] => Array
(
[users] => Array
(
[0] => oneclick_wp13
)
[disk_usage] => 123222
[database] => oneclick_wp13
)
[6] => Array
(
[users] => Array
(
[0] => oneclick_wp14
)
[database] => oneclick_wp14
[disk_usage] => 123222
)
[7] => Array
(
[users] => Array
(
[0] => oneclick_wp2
)
[disk_usage] => 123226
[database] => oneclick_wp2
)
[8] => Array
(
[users] => Array
(
[0] => oneclick_wp3
)
[disk_usage] => 0
[database] => oneclick_wp3
)
[9] => Array
(
[database] => oneclick_wp4
[disk_usage] => 123230
[users] => Array
(
[0] => oneclick_wp4
)
)
[10] => Array
(
[users] => Array
(
[0] => oneclick_wp5
)
[database] => oneclick_wp5
[disk_usage] => 0
)
[11] => Array
(
[users] => Array
(
[0] => oneclick_wp6
)
[database] => oneclick_wp6
[disk_usage] => 0
)
[12] => Array
(
[disk_usage] => 123222
[database] => oneclick_wp7
[users] => Array
(
[0] => oneclick_wp7
)
)
[13] => Array
(
[disk_usage] => 123222
[database] => oneclick_wp8
[users] => Array
(
[0] => oneclick_wp8
)
)
[14] => Array
(
[database] => oneclick_wp9
[disk_usage] => 0
[users] => Array
(
[0] => oneclick_wp9
)
)
)
This is my PHP code:
$array = array();
function search_array($array, $val) {
$ArrIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($ArrIterator as $id => $sub) {
$childArray = $ArrIterator->getSubIterator();
if(strstr(strtolower($sub), strtolower($val))) {
$childArray = iterator_to_array($childArray);
$result[] = $childArray['database'];
}
}
return $result;
}
$results = array_filter( search_array( $array, '_wp') );
foreach( $results as $key=> $value ) {
$results[$key] = str_replace( $wp_db_prefix . "wp","", $value);
};
$wp_next_db_name = max($results) + 1;
Here is what it does.
It searches a multi-dimensional array which MYSQL DB info for the "_wp" string within the values.
Then it filters out the user account prefix "oneclick_" with prefix "wp" which leaves me with integers.
Then I find the highest value (max) and increment by 1.
The purpose is for a cPanel plugin to create incremental DB names.
If you only want to look at the values in the database elements of your array, you can use array_column to extract them, and then array_map to iterate over those values, capturing the integer part if they begin with oneclick_wp, finally feeding those results into max to get the maximum value and adding 1:
$wp_db_prefix = 'oneclick';
$max = max(array_map(function ($v) use ($wp_db_prefix) {
return (int)str_replace("{$wp_db_prefix}_wp", '', $v);
},
array_column($array, 'database'))) + 1;
echo $max;
Output
15
Demo on 3v4l.org
I have a query which gives data from one person or from more persons.
The data for one person is presented with JSON as:
Array ( [0] => Array (
[a2a_Person] => Array (
[pid] => Person1
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Aagje )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Baltus ) ) ....
If there are more persons then the data is presented as
Array ( [0] => Array (
[a2a_Person] => Array (
[0] => Array (
[pid] => Person1 [a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Walig )
[a2a_PersonNamePatronym] => Array ( )
[a2a_PersonNamePrefixLastName] => Array ( )
[a2a_PersonNameLastName] => Array ( [a2a_PersonNameLastName] => Verdugt ) ) [a2a_Gender] => Array ( [a2a_Gender] => Man )
[1] => Array (
[pid] => Person2
[a2a_PersonName] => Array (
[a2a_PersonNameFirstName] => Array ( [a2a_PersonNameFirstName] => Huibert ) [a2a_PersonNamePatronym.....
My question is now : how can i determine if I have more Person-records so i can acces the data correct way.
And what is the best way to acces the data. Must I first check the definition and write different code.
Thanks,
Fred
I have the following two arrays...
1) how could i get only the different key->value one?
2) how can i insert to mysql the second array?
// first array
$aa = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
// second array
$bb = Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => NAN
[p_r] => RN
[id] => 1214125
[gender] => m
)
)
[t_b] => Array
(
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] => 21
[p_r] => 25
)
)
[t_r] => Array
(
[0] => Array
(
[I_r] => 19
)
)
I have used the array_diff function but i get NULL.
please some one help?
$aa=(array)$aa;
$bb=(array)$bb;
$result=array_diff($aa,$bb);
It's unclear what you want. Please give an example or your desired output. Here's one possibility:
$ser_aa = array_map(function($e){return serialize($e);}, $aa);
$ser_bb = array_map(function($e){return serialize($e);}, $bb);
$diff = array_diff($ser_aa, $ser_bb);
$out = array_map(function($e){return unserialize($e);}, $diff);
print_r($out);
Output:
Array
(
[t_a] => Array
(
[0] => Array
(
[f_c] => LAL
[p_r] => RN
[id] =>
[gender] => m
)
)
[t_l] => Array
(
[0] => Array
(
[p_lev] => 2
[p_date] =>
[p_r] =>
)
)
)
Here is an example output from when I print out its contents:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
)
)
)
)
[1] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbB] =>
)
)
)
)
[2] => Array
(
[CountryA] => Array
(
[ProvinceB] => Array
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[3] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
What I would like to do is create a function that parses it in some way (and perhaps creates a new array) so that the result will look something like:
Array
(
[0] => Array
(
[CountryA] => Array
(
[ProvinceA] => Array
(
[CityA] => Array
(
[SuburbA] =>
[SuburbB} =>
)
)
[ProvinceB] =>
(
[CityB] => Array
(
[SuburbC] =>
)
)
)
)
[1] => Array
(
[CountryB] => Array
(
[ProvinceD] => Array
(
[CityE] => Array
(
[SuburbE] =>
)
)
)
)
Thanks in advance!!
Change you structure, your array should not look like this :
Array(
[0] => Array([Country A] => data),
[1] => Array([Country B] => data)
)
But more like this :
Array(
[Country A] => data,
[Country B] => data
)
Once you've done this, it will be trivial to add a city in your array :
If the country exists in the array, add to the country, else add it to the array and stop
[Add to country :] If the province exists in the country, add to the province, else add the province to the array and stop
Same for city, suburb... you get the idea
I know I must be doing something simple wrong. When I do this:
echo '<pre>';
print_r($event->when);
echo '</pre>';
I get this:
Array
(
[0] => Zend_Gdata_Extension_When Object
(
[_rootElement:protected] => when
[_reminders:protected] =>
[_startTime:protected] => 2011-06-16T10:00:00.000-05:00
[_valueString:protected] =>
[_endTime:protected] => 2011-06-17T11:00:00.000-05:00
[_rootNamespace:protected] => gd
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] =>
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
[gd] => Array
(
[1] => Array
(
[0] => http://schemas.google.com/g/2005
)
)
[openSearch] => Array
(
[1] => Array
(
[0] => http://a9.com/-/spec/opensearchrss/1.0/
)
[2] => Array
(
[0] => http://a9.com/-/spec/opensearch/1.1/
)
)
[rss] => Array
(
[1] => Array
(
[0] => http://blogs.law.harvard.edu/tech/rss
)
)
)
)
)
I'm then trying to get startTime by doing this:
$StartTime = $event->when->startTime;
But I'm not getting anything.
And yet, when I do this:
pr($event->published);
I get this:
Zend_Gdata_App_Extension_Published Object
(
[_rootElement:protected] => published
[_rootNamespace:protected] => atom
[_rootNamespaceURI:protected] =>
[_extensionElements:protected] => Array
(
)
[_extensionAttributes:protected] => Array
(
)
[_text:protected] => 2011-06-15T03:32:14.000Z
[_namespaces:protected] => Array
(
[atom] => Array
(
[1] => Array
(
[0] => http://www.w3.org/2005/Atom
)
)
[app] => Array
(
[1] => Array
(
[0] => http://purl.org/atom/app#
)
[2] => Array
(
[0] => http://www.w3.org/2007/app
)
)
)
)
and I can do this:
$dateAdded = $event->published->text;
echo $dateAdded;
and I see an output...
According to to the official Zend_Gdata_Extension_When documentation, there's a method called getStartTime() which will give you the time.
If you do $event->when[0]->getStartTime() or $event->when[0]->startTime, you'll get the start time.
startTime is marked protected. You can't reference it from outside like you did. There must be a getter function 'getStartTime()' function in that object that would allow you to reference it publicly.
EDIT: Also it is returning an object array - not an single object, so you would need to reference the it like: $event[0]->getterFunction() or loop through the array with a foreach accessing the individual objects in the loop