I have a large multidimensional array and I basically want to drop the first level of arrays and build an array the doesn't have the numerical keys:
My current array:
Array
(
[0] => Array
(
[block_header14] => Array
(
[type] => block_header
[caption] => Silver
[collapsable] => 1
[collapsed] =>
)
[section14] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships14
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
[1] => Array
(
[block_header15] => Array
(
[type] => block_header
[caption] => Gold
[collapsable] => 1
[collapsed] =>
)
[section15] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships15
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
)
This what I want to end up with:
Array
(
[block_header14] => Array
(
[type] => block_header
[caption] => Silver
[collapsable] => 1
[collapsed] =>
)
[section14] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships14
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
[block_header15] => Array
(
[type] => block_header
[caption] => Gold
[collapsable] => 1
[collapsed] =>
)
[section15] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships15
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
Edit: I reworked the initial foreach and skipped the extra step: Sorry for the blurry question
foreach ($aMemLevels as $id =>$name) {
$aForm['inputs']['block_header'.$id] = array(
'type' => 'block_header',
'caption' => 'Available to ' . $name . ' Members',
'collapsable' => true,
'collapsed' => false
);
$aForm['inputs']['section'.$id] = array(
'type' => 'checkbox_set',
'name' => 'purchasable_memberships'.$id,
'values' => getMemberships(true),
'value' => $aValue,
);
}
$new_array = array();
foreach($array as $value) {
$new_array = array_merge($new_array, $value);
}
print_r($new_array);
I haven't tested it, but maybe something like this will help?
$new_array = array();
foreach ($multi_array as $inner_arr) {
$new_array = array_merge($new_array, $inner_array);
}
$multi_array being your initial, multidimensional array...
Let me know if that works.
Related
I want to convert the xml data that I have generated from my system into an array array and send it to the API system of the platform I sell individually.
I'd appreciate it if you could review and support.
my array data example:
Array
(
[product] => Array
(
[0] => Array
(
[productSellerCode] => bebe27
[title] => Uzun Kollu Renkli Uyku Tulum Body Zıbın 6 Aylık Bebek
[subtitle] => Stoktan hızlı kargo
[description] => Bebeğiniz İçin Özel Günlerde ve Günlük Olarak Kullanabileceği Mükemmel Takım.Kaliteli Şık ve Zarif Olan Bu Ürünü Sizlere Sunuyoruz.%100 Pamuk olan bu ürünü Bebeğinize Güvenle Giydirebilirsiniz Dokusu Yumuşaktır.
[category] => Array
(
[id] => 1000053
)
[price] => 48.472
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/bebe/IMG_7976.JPG
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Beden
[value] => 3-6 Ay
)
[1] => Array
(
[name] => Marka
[value] => DİĞER
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 2
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 1
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
[1] => Array
(
[productSellerCode] => MS-S010
[title] => 10 cm Samur Kase
[subtitle] => Stoktan hızlı kargo
[description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
[category] => Array
(
[id] => 1016108
)
[price] => 31.426
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Marka
[value] => DİĞER
)
[1] => Array
(
[name] => Materyal
[value] => Seramik
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 3
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 10
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
I want to post an array block here every time.
(
[productSellerCode] => MS-S010
[title] => 10 cm Samur Kase
[subtitle] => Stoktan hızlı kargo
[description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
[category] => Array
(
[id] => 1016108
)
[price] => 31.426
[domestic] => false
[currencyType] => 1
[images] => Array
(
[image] => Array
(
[url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
[order] => 1
)
)
[attributes] => Array
(
[attribute] => Array
(
[0] => Array
(
[name] => Marka
[value] => DİĞER
)
[1] => Array
(
[name] => Materyal
[value] => Seramik
)
)
)
[saleStartDate] => Array
(
)
[saleEndDate] => Array
(
)
[productionDate] => Array
(
)
[expirationDate] => Array
(
)
[productCondition] => 1
[preparingDay] => 3
[discount] => Array
(
[startDate] => Array
(
)
[endDate] => Array
(
)
[type] => Array
(
)
[value] => Array
(
)
)
[shipmentTemplate] => kargo-bizden
[stockItems] => Array
(
[stockItem] => Array
(
[quantity] => 10
[sellerStockCode] => Array
(
)
[attributes] => Array
(
[attribute] => Array
(
[name] => Array
(
)
[value] => Array
(
)
)
)
[optionPrice] => Array
(
)
)
)
)
I think I'm making a mistake in foreach operations.
I can send 1 product as below, I need to send all products in order.
<?php
include "class.n11.php";
$n11Params = [
'appKey' => 'my-api-key',
'appSecret' => 'my-api-secret'
];
$n11 = new N11($n11Params);
$xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php");
$new = simplexml_load_string($xmlfile);
$con = json_encode($new,JSON_UNESCAPED_UNICODE);
$newArr = json_decode($con, true);
foreach ($newArr as &$urun) {
$productSellerCode = $urun[$i]->productSellerCode;
$title = $urun[$i]->title;
$description = $urun[$i]->description;
$categoryid = $urun[$i]->category->id;
$price = $urun[$i]->price;
$preparingDay = $urun[$i]->preparingDay;
$quantity = $urun[$i]->quantity;
$saveProduct = $n11->SaveProduct(
[
'productSellerCode' => $productSellerCode,
'title' => $title,
'subtitle' => 'Stoktan hızlı kargo',
'description' => $description,
'category' =>
[
'id' => $categoryid
],
'attributes' =>
[
'attribute' =>
foreach($urun->attributes as $attribute){
'name' => $attribute['name'],
'value' => $attribute['value']
}
],
'price' => $price,
'domestic' => false,
'currencyType' => 'TL',
'images' =>
[
'image' =>
foreach($urun->images as $image){
'url' => $image['url'],
'order' => $image['order']
}
],
'saleStartDate' => '',
'saleEndDate' => '',
'productionDate' => '',
'expirationDate' => '',
'productCondition' => '1',
'preparingDay' => $preparingDay,
'discount' => '',
'shipmentTemplate' => 'kargo-bizden',
'stockItems' =>
[
'stockItem' =>
[
'quantity' => $quantity,
'sellerStockCode' => '',
'attributes' =>
[
'attribute' => []
],
'optionPrice' => ''
]
]
]
);
} var_dump($saveProduct);
#RiggsFolly, It worked that way yes but only the first ranked product was sent. I couldn't do the loop.
<?php
include "class.n11.php";
$n11Params = ['appKey' => 'my-api-key', 'appSecret' => 'my-api-secret'];
$n11 = new N11($n11Params);
$xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php");
$new = simplexml_load_string($xmlfile);
$con = json_encode($new,JSON_UNESCAPED_UNICODE);
$newArr = json_decode($con, true);
foreach ($newArr['product'] as $product) {
$saveProduct = $n11->SaveProduct($product);
}
var_dump($saveProduct);
I succeeded with the following code.
var_dump($products);
foreach($products as $indis=>$degeri){
foreach($degeri as $icindekiler){
$saveProduct = $n11->SaveProduct($icindekiler);
}
}
var_dump($saveProduct);
simply start you foreach one step down into the array i.e. from $newArr['product'] and each array you process from there will be the array you want to pass as the parameter.
foreach ($newArr['product'] as $product) {
$saveProduct = $n11->SaveProduct($product);
}
Also this may or may not actually exist in your real code but this array is not correctly coded, it has a missing '
$n11Params = [
'appKey' => 'my-api-key', // missing quote added
'appSecret' => 'my-api-secret'
];
Assuming I have an array and recursively want to do some task. I have spent one day for this unable to solve this. May be my logic is not good.
Any help on how to do such thing in an efficient way would save my days.
I have an array with n level deep, looking like:
Array
(
[0] => Array
(
[name] => user1
[email] => user1#demo.com
[depth] => 1
)
[1] => Array
(
[name] => user2
[email] => user2#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user2.1
[email] => user2.1#demo.com
[depth] => 2
)
)
)
[2] => Array
(
[name] => user3
[email] => user3#demo.com
[depth] => 1
[0] => Array
(
[0] => Array
(
[name] => user3.1
[email] => user3.1#demo.com
[depth] => 2
[0] => Array
(
[0] => Array
(
[name] => user3.1.1
[email] => user3.1.1#demo.com
[depth] => 3
)
)
)
[1] => Array
(
[name] => user3.2
[email] => user3.2#demo.com
[depth] => 2
)
)
)
)
I want to change above array in exactly this format:
array(
0 => array(
'name' => 'user1',
),
1 => array(
'name' => 'user2',
'children' => array(
0 => array(
'name' => 'user2.1',
) ,
) ,
) ,
2 => array(
'name' => 'user3',
'children' => array(
0 => array(
'name' => 'user3.1',
'children' => array(
0 => array(
'name' => 'user3.1.1',
) ,
) ,
) ,
1 => array(
'name' => '3.2',
)
) ,
) ,
)
Edited:
I am using this code and working fine if i want to show data in tree format but unable to push data into array as i want.
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
displayArrayRecursively($value, $indent . '-->');
} else {
if ($key == 'name')
{
echo "$indent $value <br>";
}
else {
continue;
}
}
}
}
}
displayArrayRecursively($userListArray);
Can anyone suggest me how to do this?
Thank you
This function will do what you want:
function process_nodes($nodes) {
$new = array();
foreach ($nodes as $node) {
$new[] = array('name' => $node['name']);
if (isset($node[0])) {
$new[count($new)-1]['children'] = process_nodes($node[0]);
}
}
return $new;
}
print_r(process_nodes($data));
Output:
Array
(
[0] => Array
(
[name] => user1
)
[1] => Array
(
[name] => user2
[children] => Array
(
[0] => Array
(
[name] => user2.1
)
)
)
[2] => Array
(
[name] => user3
[children] => Array
(
[0] => Array
(
[name] => user3.1
[children] => Array
(
[0] => Array
(
[name] => user3.1.1
)
)
)
[1] => Array
(
[name] => user3.2
)
)
)
)
I'm trying to group the following array by 'label' to give a total value for each person.
Array
(
[0] => Array
(
[label] => John
[value] => 84
)
[1] => Array
(
[label] => Darren
[value] => 28
)
[2] => Array
(
[label] => John
[value] => 20
)
[3] => Array
(
[label] => Morgan
[value] => 20
)
[4] => Array
(
[label] => Hannah
[value] => 14
)
[5] => Array
(
[label] => Morgan
[value] => 14
)
[6] => Array
(
[label] => Darren
[value] => 10
)
)
This would be the end result:
Array
(
[0] => Array
(
[label] => John
[value] => 104
)
[1] => Array
(
[label] => Darren
[value] => 38
)
[2] => Array
(
[label] => Morgan
[value] => 34
)
[3] => Array
(
[label] => Hannah
[value] => 14
)
)
I'm assuming I need to use foreach to group the labels together but I'm stuck for the best way to do this.
I would do something like this (expecting your array in $array):
// output array
$newArray = array();
// loop over the input array
foreach($array as $entry) {
// loop over the output array
foreach($newArray as &$currentEntry) {
// check if there is a the current label
if($currentEntry['label'] == $entry['label']) {
// if so, add the value and continue (also parent)
$currentEntry['value'] += $entry['value'];
continue 2;
}
}
// if the current label wan't found, it will get here and add an entry
array_push($newArray,$entry);
}
print_r($newArray);
Try this:
<?php
$array = [
['label' => 'John',
'value' => '84'],
['label' => 'Darren',
'value' => '28'],
['label' => 'John',
'value' => '20'],
['label' => 'Morgan',
'value' => '20'],
['label' => 'Hannah',
'value' => '14'],
['label' => 'Morgan',
'value' => '14'],
['label' => 'Darren',
'value' => '10']
];
$final = [];
foreach ($array as $arr)
$final[$arr['label']] = isset($final[$arr['label']]) ? $final[$arr['label']] + $arr['value'] : $arr['value'];
$result = [];
foreach ($final as $label => $value)
$result[] = ['label' => $label, 'value' => $value];
print_r($result);
Output:
Array
(
[0] => Array
(
[label] => John
[value] => 104
)
[1] => Array
(
[label] => Darren
[value] => 38
)
[2] => Array
(
[label] => Morgan
[value] => 34
)
[3] => Array
(
[label] => Hannah
[value] => 14
)
)
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
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;
}