I have an array structure like this
Array
(
[0] => Array
(
[C:/xampp/htdocs/rosoka/file] => Array()
)
[1] => Array
(
[C:/xampp/htdocs/rosoka/file/2018-03-02] => Array()
)
[2] => Array
(
[C:/xampp/htdocs/rosoka/file/2018-03-03] => Array()
)
)
how to add some an array on key specific like
[C:/xampp/htdocs/rosoka/file] => Array('duck','buffalo')
1.You can do it like below:-
$array[0]['C:/xampp/htdocs/rosoka/file'] = ['duck','buffalo'];
Output:-https://eval.in/984253
2.Or if some values are already present there:-
$array[0]['C:/xampp/htdocs/rosoka/file'][] = 'buffalo';
Output:-https://eval.in/984254
3.Or if you want to search key first and then try to add:-
foreach($array as $key=>$value){
if(array_keys($value)[0] == 'C:/xampp/htdocs/rosoka/file'){
$array[$key]['C:/xampp/htdocs/rosoka/file'][] = 'buffalo';
}
}
Output:-https://eval.in/984255
For your requirement, you can assign an Array value to key specific like below:
$array[0]['C:/xampp/htdocs/rosoka/file'] = Array('duck','buffalo');
$array[1]['C:/xampp/htdocs/rosoka/file/2018-03-02'] = Array('duck','buffalo');
If you use $array[0]['C:/xampp/htdocs/rosoka/file'][] at left side like this it will again become an Array, hence accessing values will be changed. There is no need of having [] again.
Related
Let's say I have an array like this:
[["code1": '528'], ["code2": '292'], ["code1": '108']]
I am looping through the array to check if a specific key exists. If it doesn't then I am adding it to another array:
$arr[$codename] = $code
However, if the key already exists, then I want to append the $code value to the existing key's values. I'm not sure how to do this part.
I want the new array to look like this:
[["code1": '528', '108'], ["code2", '292']]
You can try:
$array = [['code1' => 528], ['code2' => 292], ['code1' => 108]];
$newArray = [];
foreach ($array as $value) {
$newArray[array_key_first($value)][] = $value[array_key_first($value)];
}
print_r($newArray);
Result:
Array
(
[code1] => Array
(
[0] => 528
[1] => 108
)
[code2] => Array
(
[0] => 292
)
)
I have a huge array in which keys are also not constant in most of the cases, but there are 3 keys that always constant (#name,#default_value,#value) and #default_value and #value is different i want to get these kind of sub arrays in 1 simple array , for this purpose i am using recursion in whole array and checking out if these 3 keys are present there i can print these values inside recursion easily but I am not able to get those values in return. So that i can precess them further.
$form=array();
$form['field_one']['#name']="field_one";
$form['field_one']['#default_value']="old";
$form['field_one']['#value']="new";
$form['field_two']['#name']="field_two";
$form['field_two']['#default_value']="old";
$form['field_two']['#value']="new";
$form['field_three']['#name']="field_three";
$form['field_three']['#default_value']="old";
$form['field_three']['#value']="old";
$form['thiscouldbeanotherarray']['idk']['field_four']['#name']="field_four";
$form['thiscouldbeanotherarray']['idk']['field_four']['#default_value']="old";
$form['thiscouldbeanotherarray']['idk']['field_four']['#value']="new";
$arr_get = get_updatedvalues($form,array());
var_dump($arr_get);
function get_updatedvalues($form,$fields) {
if (!is_array($form)) {
return;
}
foreach($form as $k => $value) {
if(str_replace('#','',$k) =='default_value' && ($form['#default_value'] != $form['#value'] ) ){
$fields['field'][$form['#name']] = array("name" => $form['#name'],"old_value" =>$form['#default_value'],"new_value" =>$form['#value']);
var_dump($fields);
}
get_updatedvalues($value,$fields);
}
return $fields;
}
If you run this code it will give you $arr_get > array (size=0), there i need all three values
If I understand correctly, you need to pass fields as a reference in order to change it:
function get_updatedvalues($form, &$fields) {
To do that, however, you'll need to change your initial call so that you have a variable to pass as the reference:
$array = [];
$arr_get = get_updatedvalues($form,$array);
Running this I get:
Array
(
[field] => Array
(
[field_one] => Array
(
[name] => field_one
[old_value] => old
[new_value] => new
)
[field_two] => Array
(
[name] => field_two
[old_value] => old
[new_value] => new
)
[field_four] => Array
(
[name] => field_four
[old_value] => old
[new_value] => new
)
)
)
How do you count items in an array that's part of an array? I'm using Advance Custom Fields plugin in WordPress and my array print_r(get_field('irl_today_entry')) output looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
How do you only count items in [irl_today_social]? I've tried a lot of options that do not work.
If you have multiple entries with irl_today_social you might also use array_map and array_column
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, "irl_today_social"));
print_r($res);
Output
Array
(
[0] => 2
)
See a php demo
You can use array_reduce,
array_reduce(get_field('irl_today_entry'),function($a,$b){
return count($a["irl_today_website"]) + count($b["irl_today_website"]);
});
Loop through your array, get the count of your desired index, and add that to a counter.
$count = 0;
foreach(get_field('irl_today_entry') as $entry){
$count = $count + count(entry['irl_today_social']);
}
Simply you can use "Count" Function
Here is the Solution:
Step 1: First get your array items of ['irl_today_entry'].
Step 2 Then Count your ['irl_today_social'] items.
Example:
foreach(get_field('irl_today_entry') as $entry){
$socialData = count(entry['irl_today_social']);
}
Thanks
according to my understaing just simply try this
echo count($your_array[1]['irl_today_social']);
I have a question for you, I need to through an array with other arrays in php but i through only the last array, my array is:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd+dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds+dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk+stw
)
)
My php:
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
With this code I through only the last array [2], but how to through array?please help me
I need to get the array:
Array
(
[0] => Array
(
[syn_id] => 17070
[syn_label] => fd dfd
)
[1] => Array
(
[syn_id] => 17068
[syn_label] => fds dsfds
)
[2] => Array
(
[syn_id] => 17069
[syn_label] => klk stw
)
)
$a_ddata = json_decode(method(), true); $i=0;
foreach ($a_ddata as $a_data)
{
$a_data_f[$i]['syn_id'] = $a_data['syn_id'];
$a_data_f[$i]['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
$i++;
}
This should be your answer..
When you iterate through something using foreach, by default PHP makes a copy of each element for you to use within the loop. So in your code,
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as $a_data)
{
// $a_data is a separate copy of one of the child arrays in $a_ddata
// this next line will modify the copy
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
// but at the end of the loop the copy is discarded and replaced with a new one
}
Fortunately the manual page for foreach gives us a way to override this behavior with the reference operator &. If you place it between the as keyword and your loop variable, you're able to update the source array within your loop.
$a_ddata = json_decode(method(), true);
foreach ($a_ddata as &$a_data)
{
// $a_data is now a reference to one of the elements to $a_ddata
// so, this next line will update $a_ddata's individual records
$a_data['syn_label'] = urldecode(utf8_decode($a_data['syn_label']));
}
// and you should now have the result you want in $a_ddata
This should help:
$a_data['syn_label'][] = urldecode(utf8_decode($a_data['syn_label']));
For each iteration you are only replacing $a_data['syn_label']. By adding [] you are making it a multi dimension array which increments for every iteration.
Given
[0] => Array
(
[0] => ask.com
[1] => 2320476
)
[1] => Array
(
[0] => amazon.com
[1] => 1834593
)
[2] => Array
(
[0] => ask.com
[1] => 1127456
)
I need to remove duplicate values solely based on first value, regardless of what any other subsequent values may be. Notice [0][1] differs from [2][1] yet I consider this as a duplicate because there are two matching first values. The other data is irrelevant and shouldn't be considered in comparison.
Try this, assuming that $mainArray is the array you have.
$outputArray = array(); // The results will be loaded into this array.
$keysArray = array(); // The list of keys will be added here.
foreach ($mainArray as $innerArray) { // Iterate through your array.
if (!in_array($innerArray[0], $keysArray)) { // Check to see if this is a key that's already been used before.
$keysArray[] = $innerArray[0]; // If the key hasn't been used before, add it into the list of keys.
$outputArray[] = $innerArray; // Add the inner array into the output.
}
}
print_r($outputArray);