php move nodes to parent array - php

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']);

Related

PHP multi array search and assign value to variable

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']);

Multidimensional arrays, remove the duplicated array in next array

My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}

How to remove parent array index from array

I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);

json array sort by value in php

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order. It sorted using value. I am missing something but not able to figure it out. please review the code. Thanks!
stdClass Object
(
[node_title] => abc
[nid] => 2281
[field_api_order_value] => 201
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 8
[format] =>
[safe_value] => 8
)
)
)
)
)
)
)
stdClass Object
(
[node_title] => abc
[nid] => 2243
[field_api_order_value] => 204
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 3
[format] =>
[safe_value] => 3
)
)
)
)
)
)
) stdClass Object
(
[node_title] => abc
[nid] => 2431
[field_api_order_value] => 242
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
[format] =>
[safe_value] => 1
)
)
)
)
)
)
)
and So on ...
foreach ($view->result as $result) {
$node = $result->_data['nid']['entity'];
$listing_order = $node->field_listing_order[LANGUAGE_NONE][0];
.....
// code goes here and it works well. sorting issue
}
usort ($node->field_listing_order[LANGUAGE_NONE][0], function($a, $b){
return strcmp($a->value, $b->value);
}); ?>
If you need to sort all the nodes using the field_listing_order value, you need to compare the values along the full path, from the first object to the value:
usort($view->result, function($a, $b) {
$la = $a->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
$lb = $b->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
return $la - $lb ;
});
In this case $a and $b are two different nodes which could be compared. That why you should compare the field_listing_order's value of these nodes. The answer is working with $la- $lb

Unset an array using php

I have a problem like i have a multidimensional array and i want to remove a parent array if an inner array contains 'edit'
the array is like this
[fields] => Array
(
[0] => Array
(
[header] => Array
(
[fieldName] => edit
[displayName] => Edit
[width] => 40
[group] => 0,1,2,3
[cssClass] => dgAssetsCentered
)
[visibility] => Array
(
[showOnStart] => 1
[editable] =>
)
[cell] => Array
(
[type] => link
[params] => Array
(
[label] => Edit
)
)
[sort] => Array
(
[sortable] => 1
)
[validator] => Array
(
[name] =>
[params] =>
)
)
[1] => Array
(
[header] => Array
(
[fieldName] => OFFERID
[displayName] => Offer Id
[group] => 0
i want to remove array 0 because inner array contains 'edit' i am able to delete [header] array not able to delete [0] array.please suggest.
thanks
alex.
unset(fields); will clear the entire array.
unset(fields[0]); will clear the element 0 inside fields array
foreach($fields as $key => $field)
{
if(isset($field['header']['fieldName']) && $field['header']['fieldName'] == 'edit')
unset($field[$key]);
}
UPD:
Also, if you wanna remove array, if any element equal 'edit', try this algorithm:
foreach($fields AS $key => $array)
{
foreach($array as $innered)
{
if(array_search('edit', $innered))
{
unset($fields[$key]);
break;
}
}
}

Categories