how to get parent array index from multidimensional array in php - php

I have an array $arr.
when I print this it shows the results as follows.
Array
(
[0] => Array
(
[name] => homeandgarden-Control Type
[1] => Array
(
[0] => product
)
)
[1] => Array
(
[name] => homeandgarden-Depth
[1] => Array
(
[0] => product
)
)
[2] => Array
(
[name] => homeandgarden-Height
[1] => Array
(
[0] => product
)
)
[3] => Array
(
[name] => homeandgarden-Load Type
[1] => Array
(
[0] => product
)
)
[4] => Array
(
[name] => homeandgarden-Machine Type
[1] => Array
(
[0] => product
)
)
[5] => Array
(
[name] => homeandgarden-Type
[1] => Array
(
[0] => product
)
)
[6] => Array
(
[name] => homeandgarden-Width
[1] => Array
(
[0] => product
)
)
[7] => Array
(
[name] => computer & ele
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[8] => Array
(
[name] => computer
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[9] => Array
(
[name] => homeandgardenairconditioner-Type
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
[10] => Array
(
[name] => homeandgardendishwasher-Control Type
[label] =>
[singular_label] =>
[hierarchical] => 1
[show_ui] => 1
[query_var] => 1
[rewrite] => 1
[rewrite_slug] =>
[0] => Array
(
[search_items] =>
[popular_items] =>
[all_items] =>
[parent_item] =>
[parent_item_colon] =>
[edit_item] =>
[update_item] =>
[add_new_item] =>
[new_item_name] =>
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
)
[1] => Array
(
[0] => product
)
)
)
and I have a variable $categ.For example $categ = "homeandgardenairconditioner-Type"
then I want to know the index of the parent array whose "name" is "homeandgardenairconditioner-Type"
i.e --- o/p should be 9
how to get this index. Please help me
Thanks

I think you are looking for this:
function getIndex($name, $array){
foreach($array as $key => $value){
if(is_array($value) && $value['name'] == $name)
return $key;
}
return null;
}

Related

Laravel - How can I add new array key with value to the collection output

I have this query:
$get_products = Cache::rememberForever('all_imports', function () use ($collection) {
return DB::connection('mongodb')->collection( $collection )->paginate( self::NO_OF_PRODUCTS );
});
If I print_r this output:
echo '<pre>';
print_r( $get_products );
echo '</pre>';
I got this request:
Illuminate\Pagination\LengthAwarePaginator Object
(
[items:protected] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[_id] => MongoDB\BSON\ObjectId Object
(
[oid] => 639e0f59889929ce620f4e9c
)
[id] => 35035
[availability] => In stock
[brand] => Bogas Primavara Vara
[condition] => new
[date_add] => 1671303001
[date_upd] => 1673865782
[description] => Rochie dama scurta catifea neagra Antonietta Bogas Rochie dama scurta catifea neagra Antonietta Bogas
[image_link] => https://www.bogas.ro/continut/produse/35035/1000/rochie-dama-scurta-catifea-neagra-antonietta-bogas_126777.jpeg
[link] => https://www.bogas.ro/rochie-dama-scurta-catifea-neagra-antonietta-bogas/?utm_source=googlemerchantcenter&utm_medium=cpc&utm_campaign=direct_link
[price] => 229.99 RON
[product_type] => Rochii
[sale_price] => 149.99 RON
[title] => Rochie dama scurta catifea neagra Antonietta Bogas
[update_history] => Array
(
[0] => Array
(
[id_feed] => 269
[feed_name] => bogas direct
[date_upd] => 1673865782
[field_history] => Array
(
)
)
)
[all_sizes] => Array
(
[0] => XS
[1] => S
[2] => M
[3] => L
[4] => XL
)
[sizes_out_of_stock] => Array
(
)
[additional_image_link] => Array
(
[0] => https://www.bogas.ro/continut/produse/35035/520/rochie-dama-scurta-catifea-neagra-antonietta-bogas_126775.jpeg
[1] => https://www.bogas.ro/continut/produse/35035/520/rochie-dama-scurta-catifea-neagra-antonietta-bogas_126776.jpeg
)
[all_attributes] => Array
(
[0] => Negru
[1] => Negru
[2] => Negru
[3] => Negru
[4] => Negru
)
[ean13] =>
[in_stock_attributes] => Array
(
[0] => Negru
[1] => Negru
[2] => Negru
[3] => Negru
[4] => Negru
)
[specs] => Array
(
[Material] => Catifea
)
[total_optiuni_out_of_stock] => Array
(
)
[clicks_last_30days_present] => 0
[field_update] => Array
(
[clicks_last_30days_present] => 1673872147
[cost_last_30days_present] => 1673872147
[conversions_last_30days_present] => 1673872147
[conversions_value_last_30days_present] => 1673872147
[impressions_last_30days_present] => 1673872147
[roas_last_30days_present] => 1673872147
)
[cost_last_30days_present] => 0
[conversions_last_30days_present] => 0
[conversions_value_last_30days_present] => 0
[impressions_last_30days_present] => 4
[roas_last_30days_present] => 0
)
)
[escapeWhenCastingToString:protected] =>
)
[perPage:protected] => 1
[currentPage:protected] => 1
[path:protected] => http://127.0.0.1:8000/api/price-monitor/matching/your-product/97-0048-2571-870/1
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
[onEachSide] => 3
[options:protected] => Array
(
[path] => http://127.0.0.1:8000/api/price-monitor/matching/your-product/97-0048-2571-870/1
[pageName] => page
)
[total:protected] => 830
[lastPage:protected] => 830
)
Now, I want to added a new array key with value to the output. So, I am doing this:
if( isset( $get_products[0]['additional_image_link'] ) && ! empty( $get_products[0]['additional_image_link'] ) ) {
$get_products[0]['carousel_images'] = $get_products[0]['additional_image_link'];
}
But I got an error message :
Indirect modification of overloaded element of
Illuminate\Pagination\LengthAwarePaginator has no effect

How to access the values of this API return

Could you please help me? I need to know how to access the values of this response from an API call using PHP.
Array ( [queryIds] => Array ( [0] => lHLLapn7EH ) [timedOutRealtimeData] => [realtimeDataMissing] => [cacheFreshness] => FRESH [prunedResult] => [novaCost] => 0 [novaRequestDuration] => 19 [wasCached] => 1 [minSampleRate] => 1 [timeComputed] => 1558794556710 [novaRuntime] => 199 [hitChunkGroupByLimit] => [throttleTime] => 0 [data] => Array ( [series] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [seriesLabels] => Array ( [0] => Array ( [0] => 0 [1] => C5241; GB ) [1] => Array ( [0] => 0 [1] => C9999; GB ) ) [seriesMeta] => Array ( [0] => Array ( [eventGroupBys] => Array ( [0] => C5241 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C5241; GB ) [1] => Array ( [eventGroupBys] => Array ( [0] => C9999 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C9999; GB ) ) [seriesCollapsed] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [xValues] => Array ( [0] => 2019-05-25 ) ) [partialMergedAndNewUserInformation] => [transformationIds] => Array ( [0] => 2696 ) [backend] => novaV2 )
You can access them using
Array ( [0] => C9999 [1] => GB ) -> $arr['data']['seriesMeta'][0]['eventGroupBys']
Array ( [setId] => [value] => 20) -> $arr['data']['seriesMeta'][0][0]

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

looping through multidimensional array from post and save to mysql

This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise

Converting a php object into associative array

I am trying to read the following php object result of an api call into an associative array. Has someone done this before or can otherwise help me?
I have tried object var dump, and array casting, but this just doesnt seems to work. I know I am doing something fundamentally wrong. Can someone help?
Many thanks.
Google_Service_Directory_Users Object
(
[etag] => "sfgsfgdsfgsdfgjkdjgfd"
[kind] => admin#directory#users
[nextPageToken] =>
[triggerEvent] =>
[usersType:protected] => Google_Service_Directory_User
[usersDataType:protected] => array
[collection_key:protected] => items
[modelData:protected] => Array
(
[users] => Array
(
[0] => Array
(
[kind] => admin#directory#user
[id] => 7642341239423
[etag] => "jasdfjshwer43537345fsdfs"
[primaryEmail] => info#example.com
[name] => Array
(
[givenName] => Info -
[familyName] => Example
[fullName] => Info - Example
)
[isAdmin] =>
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-07-29T08:46:28.000Z
[creationTime] => 2014-07-29T08:31:56.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => info#example.com
[primary] => 1
)
)
[nonEditableAliases] => Array
(
[0] => info#example.com.test-google-a.com
)
[customerId] => fsdfdd4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
[1] => Array
(
[kind] => admin#directory#user
[id] => 3895729453245
[etag] => "fsajdfd64hkj4534h5k3454"
[primaryEmail] => user#example.com
[name] => Array
(
[givenName] => User
[familyName] => Name
[fullName] => User Name
)
[isAdmin] => 1
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-08-26T09:05:49.000Z
[creationTime] => 2012-09-16T08:55:26.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => support#example.com
)
[1] => Array
(
[address] => help#example.com
)
)
[customerId] => fsdafwr4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
)
)
[processed:protected] => Array
(
)
)
First solution:
$array = json_decode(json_encode($nested_object), true);
Second solution:
function object_to_array($data) {
if (is_array($data) || is_object($data)):
$result = array();
foreach ($data as $key => $value)
$result[$key] = object_to_array($value);
return $result;
endif;
return $data;
}
both searched from the internetz

Categories