PHP multi array search and assign value to variable - php

I have searched the forums up and down, tested as much as I can and still cannot find an answer to this problem. I need to assign a value to the following variables:
$custID = contactId;
$custFieldCityID = [customFieldValues][x][customFieldId]
$custFieldCity = [customFieldValues][x][value][x]
$custFieldStateID = [customFieldValues][x][customFieldId]
$custFieldState = [customFieldValues][x][value][x]
The order of the customFieldValues always changes (city can be in position 2, next array is in position 0, etc) and the customFieldId value is always different.
Here is the array I am getting (cannot be changed):
Array
(
[contactId] => V
[name] => Mrs Mder
[email] => xxx#verizon.net
[customFieldValues] => Array
(
[0] => Array
(
[customFieldId] => I
[name] => city
[value] => Array
(
[0] => Fairfax
)
[values] => Array
(
[0] => Fairfax
)
[type] => text
[fieldType] => text
[valueType] => string
)
[1] => Array
(
[customFieldId] => 8
[name] => postal_code
[value] => Array
(
[0] => 22032
)
[values] => Array
(
[0] => 22032
)
[type] => text
[fieldType] => text
[valueType] => string
)
[2] => Array
(
[customFieldId] => M
[name] => state
[value] => Array
(
[0] => VA
)
[values] => Array
(
[0] => VA
)
[type] => text
[fieldType] => text
[valueType] => string
)
[3] => Array
(
[customFieldId] => V
[name] => street
[value] => Array
(
[0] => 123 Holden Street
)
[values] => Array
(
[0] => 123 Holden Street
)
[type] => text
[fieldType] => text
[valueType] => string
)
[4] => Array
(
[customFieldId] => s
[name] => phone
[value] => Array
(
[0] => +11234567890
)
[values] => Array
(
[0] => +11234567890
)
[type] => text
[fieldType] => text
[valueType] => phone
)
)
)
So far I have tried this:
foreach($returncustdata as $row) {
foreach($row as $k) {
$fieldID= $k['customFieldValues']['customFieldId'];
$fieldname = $k['customFieldValues']['name'];
$fieldvalue = $k['customFieldValues']['value'][0];
}
}

You can index your customFieldValues by name for simpler access:
Use array_column with 2nd argument null and 3rd argument name to index that array by name field making it associative array.
To get first element from array no matter of it's key you can use current(). If it's always in 0th position, then no need for this.
$myArray['customFieldValues'] = array_column($myArray['customFieldValues'], null, 'name');
$custID = $myArray['contactId'];
$custFieldCityID = $myArray['customFieldValues']['city']['customFieldId'];
$custFieldCity = current($myArray['customFieldValues']['city']['value']);
$custFieldStateID = $myArray['customFieldValues']['state']['customFieldId'];
$custFieldState = current($myArray['customFieldValues']['state']['value']);

Related

how to add array value at specfic index in php codenighter

I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)

Get Array in foreach loop from string

I have an Array of Strings, each String that exist in Advanced Custom Fields groups should be returned in foreach loop. Final results should be a single array of all values.
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
//$keysToImports = implode(", ",$resultsFileToImports);
$keysToImports = 'group_lubuvna_contact, group_lubuvna_subscriber';
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return ( strpos($el['key'], $keysToImports) !== false );
});
}
The above code returns only if one string exists in $keysToImports. Once there are more than one value it doesn't work. I am sure i am missing something, but can't find any solution in here!
It shows me empty Array:
Array ( )
Maybe there is a different way how to get the arrays without strpos?
Final Array should looks like following:
Array ( [0] => Array ( [ID] => 0 [key] => group_lubuvna_contact [title] => ACF Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781382 [_valid] => 1 ) [1] => Array ( [ID] => 0 [key] => group_lubuvna_subscriber [title] => Lubuvna - Subscriber Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => post ) ) [1] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) [2] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => lubuvna_subscriber ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781369 [_valid] => 1 ) )
Try changing:
$keysToImports = 'group_lubuvna_contact', 'group_lubuvna_subscriber';
// then return
return ( strpos($el['key'], $keysToImports) !== false );
to
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
// then return
return in_array($el['key'], $keysToImports);
Full code update:
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return in_array($el['key'], $keysToImports);
});
}

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}

PHP foreach to get array keys and values

I think this is a tough one! Experts only?
Ok, I have some variables (returned from get_defined_vars):
Array
(
[lead] => Array
(
[2] => fstory
[4] => him
[5] => trtr
[1] => 508b38ee02f502.23680245.png
)
[form] => Array
(
[id] => 3
)
[fields] => Array
(
[0] => Array
(
[adminLabel] => formname
[id] => 2
)
[1] => Array
(
[adminLabel] => hisher
[id] => 4
[2] => Array
(
[adminLabel] => fname
[id] => 5
)
[3] => Array
(
[adminLabel] => sign
[id] => 1
)
)
I need to get the array fields key to be the [fields] [adminLabel] and the value to be the [lead] [#].
So in this example the array would have key=value
formname = fstory
fname = trtr
hisher = his
sign = 508b38ee02f502.23680245.png
Make any sense? Possible?
Try this. It is untested.
$result_values = $array['lead'];
$results = array();
foreach ($array['form']['fields'] as $value) {
if (is_array($value)) {
$results[$value['adminLabel']] = $result_values[$value['id']];
}
}
print_r($results);

php move nodes to parent array

How could I move all nodes of the 'fields' array into its parent array '113', whilst unsetting 'fields' ?
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[fields] => Array
(
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
)
Assuming your top level array ($something['a']) is the variable $a:
foreach($a as $key => $values){
if(isset($values['fields']))
{
$a[$key] = array_merge($a[$key], (array) $values['fields']);
unset($a[$key]['fields']);
}
}
Alternatively, if you dont want to hit every array element in $a you can just remove the loop and substitute $values with $a[113] and $key with 113.
Also note the casting for the fields element to an array, jsut in case it isnt one with (array) $values['fields']
If you can make this array like this:
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
try to use this code:
$array['a'][113]['jr_streetaddress'] = $array['a'][113]['fields']['jr_streetaddress'];
$array['a'][113]['jr_towncity'] = $array['a'][113]['fields']['jr_towncity'];
unset($array['a'][113]['fields']);

Categories