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;
}
}
}
Related
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);
});
}
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);
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
I would like to select the Image Url which is ('http://img.site.com//uploaded/articleThumbnails/DA0AE88C-AB01-4919-9D97-5FDE884A8D9E.jpeg') from the array below, but do not know how.
Array
(
[0] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => meta
[attr] => Array
(
[itemprop] => image
[content] => http://img.site.com//uploaded/articleThumbnails/DA0AE88C-AB01-4919-9D97-5FDE884A8D9E.jpeg
)
[children] => Array
(
)
[nodes] => Array
(
)
[parent] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => head
[attr] => Array
(
)
[children] => Array
(
[0] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => script
[attr] => Array
(
[type] => text/javascript
)
[children] => Array
(
)
store your entire array to a variable just for eg. call it as $datas
foreach($datas as $data)
{
echo $data->attr['content'];
}
If you have multiple element then use this
foreach ($yourArray as $val)
{
foreach ($val->attr as $val2)
{
$getyourURL[] = $val2['content'];
}
}
$getyourURL contain your all URLs
Try this
echo $arr[0][attr][content];
Use This
foreach($datas as $data)
{
echo $data[attr][content];
}
or if you want only this field without all in loop use this.
echo $datas[0][attr][content];
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']);