This question already has answers here:
PHP - Count duplicate values within two dimensional array, then display only unique values with the count
(2 answers)
Closed 4 years ago.
I have a multidimensional Array like this. Which I have to sort in some way where the same user's data will be stored in one array.
Array
(
[0] => Array
(
[user_email] => example#gmail.com
[available_date] => 2018/06/30
[available_time] => 06.00,06.30,07.00,07.30
)
[1] => Array
(
[user_email] => example#gmail.com
[available_date] => 2018/06/31
[available_time] => 06.30,07.00,07.30,08.00
)
[3] => Array
(
[user_email] => newuser#gmail.com
[available_date] => 2018/06/31
[available_time] => 08.00,08.30,09.00,09.30
)
[4] => Array
(
[user_email] => newuser#gmail.com
[available_date] => 2018/06/30
[available_time] => 08.30,09.00,09.30,10.00
)
)
)
I want the final array to be like this,Where available_date and available time will be merged as an array with a key name for a same user.Maybe this is easy but I am stuck.Any help would be appreciated.
Array
(
[0] => Array
(
[user_email] => example#gmail.com
[date_time] => Array
(
[0]=>Array
(
[date] => 2018/06/30
[time] => 06.00,06.30,07.00,07.30,
)
[1]=>Array
(
[date] => 2018/06/31
[time] => 06.30,07.00,07.30,08.00,
)
)
)
[1] => Array
(
[user_email] => newuser#gmail.com
[date_time] => Array
(
[0]=>Array
(
[date] => 2018/06/31,
[time] => 08.00,08.30,09.00,09.30,
)
[1]=>Array
(
[date] => 2018/06/30,
[time] => 08.30,09.00,09.30,10.00,
)
)
)
)
You could do something like this :
$x =
[
"0" => [
"user_email" => "example#gmail.com",
"available_date" => "2018/06/30",
"available_time" => "06.00,06.30,07.00,07.30",
],
"1" => [
"user_email" => "example#gmail.com",
"available_date" => "2018/06/31",
"available_time" => "06.30,07.00,07.30,08.00,"
],
"3" => [
"user_email" => "newuser#gmail.com",
"available_date" => "2018/06/31",
"available_time" => "08.00,08.30,09.00,09.30",
],
"4" => [
"user_email" => "newuser#gmail.com",
"available_date" => "2018/06/30",
"available_time" => "08.30,09.00,09.30,10.00",
]
];
$result = [];
foreach ($x as $item) {
$temp['date'] = $item['available_date'];
$temp['time'] = $item['available_time'];
$result[$item['user_email']]['date_time'][] = $temp;
}
print_r($result);
And the result is this :
Array ( [example#gmail.com] => Array ( [date_time] => Array ( [0] => Array ( [date] => 2018/06/30 [time] => 06.00,06.30,07.00,07.30 ) [1] => Array ( [date] => 2018/06/31 [time] => 06.30,07.00,07.30,08.00, ) ) ) [newuser#gmail.com] => Array ( [date_time] => Array ( [0] => Array ( [date] => 2018/06/31 [time] => 08.00,08.30,09.00,09.30 ) [1] => Array ( [date] => 2018/06/30 [time] => 08.30,09.00,09.30,10.00 ) ) ) )
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'
];
I have a problem with merging array's. I'll try array merge and combine but nothing is working.
First array
Array (
[type1] => Array (
[userid] => Array (
[0] => 35
[1] => 37
)
[from] => Array (
[0] => 07-06-2017
[1] => 09-06-2017
)
[till] => Array (
[0] => 07-07-2017
[1] => 09-07-2017
)
)
[type3] => Array (
[userid] => Array (
[0] => 13
)
[from] => Array (
[0] => 10-06-2017
)
[till] => Array (
[0] => 10-07-2017
)
)
)
Second array
The second array is filled with room details, but the assigned users are not added yet.
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] =>
[from] =>
[till] =>
)
)
)
)
I'll will put these arrays together to one array, so that the data is insert into the "rented_to" section. How can I get this array into an another array like this:
Array (
[type1] => Array (
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 35
[from] => 07-06-2017
[till] => 07-07-2017
)
[1] => Array
(
[userid] => 37
[from] => 09-06-2017
[till] => 09-07-2017
)
)
)
[type3] => Array
(
[m2] =>
[price] =>
[rooms] => 1
[extra] =>
[rented_to] => Array
(
[0] => Array
(
[userid] => 13
[from] => 10-06-2017
[till] => 10-07-2017
)
)
)
)
The code
$manager_add_new_room_rented_to is an array that contains the value of the first array in my example.
$manager_add_new_room_type_desc = $_POST['manager_add_new_room_type_desc'];
$manager_add_new_room_type_m2 = $_POST['manager_add_new_room_type_m2'];
$manager_add_new_room_type_rent_price = $_POST['manager_add_new_room_type_rent_price'];
$manager_add_new_room_type_rooms = $_POST['manager_add_new_room_type_rooms'];
$manager_add_new_room_type_extra = $_POST['manager_add_new_room_type_extra'];
$manager_add_new_room_rented_to = $_POST['manager_add_new_room_rented_to'];
$add_new_room_information = array();
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => array(
array(
'userid' => '',
'from' => '',
'till' => ''
)
)
);
endforeach;
Loop through $manager_add_new_room_rented_to[$room_type] and create the new array that you want.
foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
$renters = $manager_add_new_room_rented_to[$room_type];
$rented_to = array();
foreach ($renters['userid'] as $index => $userid) :
$rented_to[] = array('userid' => $userid, 'from' => $renters['from'][$index], 'to' => $renters['to'][$index]);
endforeach;
$add_new_room_information[$room_type] = array(
'm2' => $manager_add_new_room_type_m2[$key],
'price' => $manager_add_new_room_type_rent_price[$key],
'rooms' => $manager_add_new_room_type_rooms[$key],
'extra' => $manager_add_new_room_type_extra[$key],
'rented_to' => $rented_to
)
);
endforeach;
I've seen similar question here, but no one with an aggregation which might have something to do with the problem at hand.
This is the code I'm trying to run:
require '../vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
$params = [
'index' => 'search_log',
'type' => 'search_log',
'body' => [
'size' => 0,
'aggs' => [
'popular_terms' => [
'terms' => [
'field' => 'filteredSearch'
]
]
]
]
];
// Execute query
$response = $client->search($params);
// Decode response
$myData = json_decode($response);
var_dump($myData);
The output I get:
NULL
I have the problem narrowed down to the decoding part. If I execute a print_r of the $response, this is what I get (actually all in one line, I've pretty-printed it manually so it's easier to read):
Array (
[took] => 245
[timed_out] =>
[_shards] => Array (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => Array (
[total] => 5124004
[max_score] => 0
[hits] => Array ( )
)
[aggregations] => Array (
[popular_terms] => Array (
[doc_count_error_upper_bound] => 37750
[sum_other_doc_count] => 11388032
[buckets] => Array (
[0] => Array (
[key] => term1
[doc_count] => 385107
)
[1] => Array (
[key] => term2
[doc_count] => 169381
)
[2] => Array (
[key] => term3
[doc_count] => 155258
)
[3] => Array (
[key] => term4
[doc_count] => 150382
)
[4] => Array (
[key] => term5
[doc_count] => 124759
)
[5] => Array (
[key] => term6
[doc_count] => 102589
)
[6] => Array (
[key] => term7
[doc_count] => 98791
)
[7] => Array (
[key] => term8
[doc_count] => 98772
)
[8] => Array (
[key] => term9
[doc_count] => 98091
)
[9] => Array (
[key] => term10
[doc_count] => 94559
)
)
)
)
)
My desired result would be to print just term1, term2, etc, without the rest of stuff that comes with Elasticsearch responses.
I have an array of objects,those all objects are same object. But finally I need to put all those values of an object into single object.These are my objects.
Array
(
[0] => Permissions Object
(
[modules] => Array
(
[0] => 3
[1] => 4
)
[submodules] => Array
(
)
[operations] => Array
(
[3M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[4M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
)
)
[1] => Permissions Object
(
[modules] => Array
(
[0] => 1
[1] => 3
)
[submodules] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 7
[5] => 8
)
[operations] => Array
(
[1] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[2] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[3] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[4] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[7] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[8] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
[3M] => Array
(
[0] => Create
[1] => Read
[2] => Update
[3] => Delete
)
)
)
)
If you don't want to use array_merge, you can use a workaround as follows:
$global; //your array
$merged = array(); //the array you want with the merged objects
foreach ($global as $k => $object) {
foreach ($object as $key => $values) {
foreach ($values as $val)
$merged[$key][] = $val;
}
}
Before or after merging you just have to Remove duplicate values.
$array1 = [
"modules" => [
0 => 2, 1 => 4
],
"submodules" => [
"3m" => [
0 => "create", 1 => "read"
],
"4m" => [0 => "create", 1 => "read"
]
]
];
$array2 = [
"modules" => [
0 => 1,
1 => 3
],
"submodules" => [
"1m" => [
0 => "create", 1 => "read"
],
"10m" => [
0 => "create", 1 => "read"
]
]
];
$merged = array_merge_recursive($array1, $array2);
print_r($merged);
Array ( [modules] => Array (
[0] => 2 [1] => 4 [2] => 1 [3] => 3
)
[submodules] => Array (
[3m] => Array ( [0] => create [1] => read
)
[4m] =>
Array ( [0] => create [1] => read
)
[1m] => Array (
[0] => create [1] => read
)
[10m] => Array (
[0] => create [1] => read
)
)
)
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