How to make single array array object of two array object
I have the following two arrays of objects
How to make single array array object of two array object
I have the following two arrays of objects
Array
(
[0] => stdClass Object
(
[id] => 25
[authorityId] => 2
[grantVillage] => test
[agency] => test
[certificate] => Panding
[amount] => 50000
[startDate] => 2015-12-08
[endDate] => 2015-12-08
[grantArea] => test
[added_date] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 26
[authorityId] => 2
[grantVillage] => testing2
[agency] => testing
[certificate] => Verified
[amount] => 50000
[startDate] => 1970-01-01
[endDate] => 1970-01-01
[grantArea] => test
[added_date] => 0000-00-00
)
)
Array
(
[0] => stdClass Object
(
[name] => kandi Area
)
[1] => stdClass Object
(
[name] => kandi Area
)
)
I want to make it:
Array
(
[0] => stdClass Object
(
[name] => kandi Area
[id] => 25
[authorityId] => 2
[grantVillage] => test
[agency] => test
[certificate] => Panding
[amount] => 50000
[startDate] => 2015-12-08
[endDate] => 2015-12-08
[grantArea] => test
[added_date] => 0000-00-00
)
[1] => stdClass Object
(
[name] => kandi Area
[id] => 26
[authorityId] => 2
[grantVillage] => testing2
[agency] => testing
[certificate] => Verified
[amount] => 50000
[startDate] => 1970-01-01
[endDate] => 1970-01-01
[grantArea] => test
[added_date] => 0000-00-00
)
)
Any help will be appreciated.Thanks in advance.
You can merge the two array objects and can get an array object by following this question :
How to merge two arrays of object in PHP
Once you get the merge array object. You can convert the array object(std class) into array by this code snippet :
$array = json_decode(json_encode($ArrayObject),true);
You can simply use array_merge for this:
$combinedArray = array_merge($array1, $array2);
Edit: Misunderstood your question, the following would work as you simply need to loop through array 2 and add these values to array 1:
<?php
$array1 = array (
0 => array (
"id" => "25",
"authorityId" => "2",
),
1 => array (
"id" => "26",
"authorityId" => "2",
)
);
$array2 = array (
0 => array (
"name" => "kandi Area"
),
1 => array (
"name" => "kandi Area"
)
);
foreach ($array2 as $array2Key => $array2ValuesArray) {
# This works for serialized arrays:
// $array1[$array2Key]['name'] = $array2ValuesArray['name'];
# However this does not relate to the OP which means that this will work for stdClass Objects:
$array1[$array2Key]->name = $array2ValuesArray->name;
}
echo '<pre>';
print_r($array1);
echo '</pre>';
?>
Edit: This results in the following output:
array
(
[0] => stdClass Object
(
[id] => 25
[authorityId] => 2
[name] => kandi Area
),
[1] => stdClass Object
(
[id] => 26
[authorityId] => 2
[name] => kandi Area
)
)
Related
I am Working with MWS for the first time and hoping to create a program that uses the ListMatchingProducts request to average out the prices of every product that matches a query.
It should be a very simple program, but I am having trouble retrieving data.
First I make the call and get amazon's xml sheet, Then I convert the xml to an array.
Print_R shows that the array looks something like this:
Array ( [ListMatchingProductsResult] => Array ( [Products] => Array ( [Product] => Array ( [0] => Array ( [Identifiers] => Array ( [MarketplaceASIN] => Array ( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => 0786866020 ) ) [AttributeSets] => Array ( [ItemAttributes] => Array ( [Author] => Array ( [0] => Stephen C. Lundin [1] => Harry Paul [2] => John Christensen ) [Binding] => Hardcover [Brand] => Hyperion [Color] => White [Creator] => Ken Blanchard [Edition] => 1 [Feature] => Great product! [ItemDimensions] => Array ( [Height] => 8.25 [Length] => 5.50 [Width] => 0.00 [Weight] => 0.54 ) [IsAdultProduct] => false [Label] => Hyperion [Languages] => Array ( [Language] => Array ( [0] => Array ( [Name] => english [Type] => Published ) [1] => Array ( [Name] => english [Type] => Original Language ) [2] => Array ( [Name] => english [Type] => Unknown ) ) ) [ListPrice] => Array ( **[Amount] => 21.00** [CurrencyCode] => USD ) [Manufacturer] => Hyperion [ManufacturerMaximumAge] => 1188.0 [ManufacturerMinimumAge] => 156.0 [NumberOfItems] => 1 [NumberOfPages] => 110 [PackageDimensions] => Array ( [Height] => 0.65 [Length] => 8.60 [Width] => 5.65 [Weight] => 0.58 ) [PackageQuantity] => 1 [PartNumber] => 9780786866021 [ProductGroup] => Book [ProductTypeName] => ABIS_BOOK [PublicationDate] => 2000-03-08 [Publisher] => Hyperion [ReleaseDate] => 2000-03-08 [SmallImage] => Array ( [URL] => http://ecx.images-amazon.com/images/I/51cHo55tbOL._SL75_.jpg [Height] => 75 [Width] => 47 ) [Studio] => Hyperion [Title] => Fish: A Proven Way to Boost Morale and Improve Results ) ) [Relationships] => Array ( ) [SalesRankings] => Array ( [SalesRank] => Array ( [0] => Array ( [ProductCategoryId] => book_display_on_website [Rank] => 4629 ) [1] => Array ( [ProductCategoryId] => 1043856 [Rank] => 2 ) [2] => Array ( [ProductCategoryId] => 2635 [Rank] => 7 ) [3] => Array ( [ProductCategoryId] => 2637 [Rank] => 18 ) ) ) ) [1] ...
I am trying to access the amount part of the array, as this is the price of the object. Eventually, I will need to access the amount of each product and so a loop will likely come into play, but right now i cannot even access one products sales amount.
Here is the code I have been trying
$value = $array->ListMatchingProductsResult->Products->Product[0]->ListPrice->Amount;
print_r($value);
And it is not working. Even calling print_r on $array->ListMatchingProductsResult is not printing an array.
Any help is greatly appreciated!
Thanks,
Matt
You are almost there. Whatever method you are using to convert the XML to a PHP array however, does just that: It creates an associative array of things, not an object. That is why you cannot access it through the ->element operator, but need to use array indexes ['element']
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]['ListPrice']['Amount'];
If you're ever wondering why you're not getting something back, you can successily shorten above expresion until you do. So if a print_r(...) on above expression returns nothing, just remove one square bracket at a time from that print_r until you do get something back. You then know that the last bracket you removed was the culprit.
Array (
[ListMatchingProductsResult] => Array (
[Products] => Array (
[Product] => Array (
[0] => Array (
[Identifiers] => Array (
[MarketplaceASIN] => Array (
[MarketplaceId] => ATVPDKIKX0DER
[ASIN] => 0786866020
)
)
[AttributeSets] => Array (
[ItemAttributes] => Array (
[Author] => Array (
[0] => Stephen C. Lundin
[1] => Harry Paul
[2] => John Christensen
)
[Binding] => Hardcover
[Brand] => Hyperion
[Color] => White
[Creator] => Ken Blanchard
[Edition] => 1
[Feature] => Great product!
[ItemDimensions] => Array (
[Height] => 8.25
[Length] => 5.50
[Width] => 0.00
[Weight] => 0.54
)
[IsAdultProduct] => false
[Label] => Hyperion
[Languages] => Array (
[Language] => Array (
[0] => Array (
[Name] => english
[Type] => Published
)
[1] => Array (
[Name] => english
[Type] => Original Language
)
[2] => Array (
[Name] => english
[Type] => Unknown
)
)
)
[ListPrice] => Array (
[Amount] => 21.00
[CurrencyCode] => USD
)
[Manufacturer] => Hyperion
...
BTW, using <pre>...</pre> is helpful when trying to make sense of print_r() or var_dump().
P.S. You owe me a new space key.
Here is an sdk library that uses the ListMatchingProducts of Amazon MWS. You will be able to get the info in accessing info required.
https://github.com/choomz/amazon-mws-sdk/blob/master/search/src/MarketplaceWebServiceProducts/Samples/ListMatchingProductsSample.php
To show php errors have this set in your php.in
display_errors = On
You can also give this on top of the php script
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
I have an array as follow in php. It contains of two array of std objects.
How can I sort it based on report_date element?
as you see report_date is unix time.
Array(
[current] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1440963000
[id] => 3
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
)
[saved] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441135800
[id] => 1
)
))
thanks in advance.
Updated: I have two separate arrays which hold some data returned from search() function of SolrPhpClient. The arrays contain data in form of Apache_Sole_Document objects. These objects in turn contain the actual fields and values. I merge these two arrays to get a single array holding all items using array_merge() of PHP
The array will have some duplicate items which needs to be removed.
I am not sure how to achieve it in this structure.
The array structure is as such:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[4] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[5] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
As you can see there is a [id] field and a [name] field.
I would like to remove duplicates from the array comparing the [id] field.
The final array after removing duplicates should look like this:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
How can I achieve this? somebody please help!
I guess you could iterate over the array and remove the duplicates, but that doesn't seem like a neat solution to me.
I'm not familiar with the SolrPhpClient, but Solr does support grouping on fields. You can group on the id by adding this part to your request:
group=true&group.field=id
The documents returned will be grouped on the id. For more details check this page.
Update:
I looked at the SolrPhpClient documentation and see that you can add additional parameters to your request like this:
$additionalParameters = array(
'fq' => 'a filtering query',
'facet' => 'true',
'facet.field' => array(
'field_1',
'field_2'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
I assume you can add the grouping parameters to this:
$additionalParameters = array(
'group' => 'true',
'group.field' => 'id'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
For more details on this, check this page
I am using the Quizlet API 2.0, and I am pretty new to this
How do I read a value(s) from something like this:
stdClass Object ( [id] => 102269 [name] => Learn Spanish with Cats! [set_count] => 3 [user_count] => 10 [created_date] => 1308035691 [is_public] => 1 [has_password] => [has_access] => 1 [has_discussion] => 1 [member_add_sets] => 1 [description] => This set is exclusively for Spanish flashcard sets with relevant cat images as the set definitions. [sets] => Array ( [0] => stdClass Object ( [id] => 6081999 [url] => http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/ [title] => Lesson 4 (with cats+dogs) [created_by] => wsvincent [term_count] => 33 [created_date] => 1311984796 [modified_date] => 1312490710 [has_images] => 1 [subjects] => Array ( [0] => spanish cats dogs ) [visibility] => public [editable] => groups [has_access] => 1 ) [1] => stdClass Object ( [id] => 5855751 [url] => http://quizlet.com/5855751/paso-a-paso-book-1-chapter-4-flash-cards/ [title] => Paso a Paso Book 1 Chapter 4 [created_by] => catdawg426 [term_count] => 30 [created_date] => 1307761267 [modified_date] => 1307819129 [has_images] => 1 [subjects] => Array ( [0] => spanish ) [visibility] => public [editable] => only_me [has_access] => 1 ) [2] => stdClass Object ( [id] => 5873819 [url] => http://quizlet.com/5873819/los-gatos-de-viaje-flash-cards/ [title] => Los Gatos de Viaje! [created_by] => tiffreja [term_count] => 21 [created_date] => 1307996657 [modified_date] => 1307996796 [has_images] => 1 [subjects] => Array ( [0] => spanish [1] => language [2] => foreign ) [visibility] => public [editable] => only_me [has_access] => 1 ) ) [members] => Array ( [0] => stdClass Object ( [username] => philfreo [role] => creator [email_notification] => 1 ) [1] => stdClass Object ( [username] => milkncookies [role] => member [email_notification] => 1 ) [2] => stdClass Object ( [username] => Icypaw [role] => member [email_notification] => ) [3] => stdClass Object ( [username] => luckycat10 [role] => member [email_notification] => ) [4] => stdClass Object ( [username] => jeffchan [role] => member [email_notification] => ) [5] => stdClass Object ( [username] => catchdave [role] => member [email_notification] => 1 ) [6] => stdClass Object ( [username] => tiffreja [role] => member [email_notification] => 1 ) [7] => stdClass Object ( [username] => catdawg426 [role] => member [email_notification] => 1 ) [8] => stdClass Object ( [username] => ihaque [role] => member [email_notification] => 1 ) [9] => stdClass Object ( [username] => jalenack [role] => member [email_notification] => 1 ) ) )
For instance, if I want to get the name of that first set, "Learn Spanish with Cats", how do I echo it via variable?
It already converts the JSON to an array I think:
$data = json_decode($json);
Your object is not an array, but rather, well, an Object. So use the -> operator to access its properties:
echo $data->name;
It contains a property which itself is an array of additional objects. For example, to get the URL of id 6081999, you would do:
echo $data->sets[0]->url;
// http://quizlet.com/6081999/lesson-4-with-catsdogs-flash-cards/
Here is a simple solution to convert a stdClass Object in array in php with get_object_vars
Look at : https://www.php.net/manual/en/function.get-object-vars.php
Example :
dump($array);
$var = get_object_vars($array);
dump($var);
Or replace dump() function by print_r()
Use function key
eg echo key($array)
I have looked something before, when you use the json_decode()
$data = json_decode();
U can send some parameters, the first of them is "$json", it will be the json string
{"1":"first","2":"second"}
But that json decode with one parameter return an Object and the default value of the second parameter is "false". If you want that back in array you only need to use the second parameter and send "true".
$data =json_decode($json,true);
And you can recibe it like an array. :)
In case you got stdClass Object in the array, for example
$user = $result1->fetch_object() then set $user into a variable
$val = $user->user_id (make sure user_id is your database column name, its the column name). You will get a single value in $val that comes from database.
First, sorry for the lengthly explanation. I have two arrays in PHP. The first array is an array of objects. The second array is an array of arrays. Basically, I want to loop through, and merge the object with its matching array, and return a merged object.
See the following print_r() of the array of objects structures:
Array
(
[0] => stdClass Object
(
[gear] => helloworld
[status] => running
[started] => 40 Minutes Ago
[start] => index.js
[route] => 127.0.0.1:3000
[parameters] => Array
(
)
)
[1] => stdClass Object
(
[gear] => test
[status] => stopped
[started] =>
[start] => index.js
[route] =>
[parameters] => Array
(
)
)
[2] => stdClass Object
(
[gear] => test2
[status] => stopped
[started] =>
[start] => index.js
[route] =>
[parameters] => Array
(
[0] => first
[1] => second
[2] => third
)
)
)
See the following print_r() of the array of arrays structures:
Array
(
[0] => Array
(
[gear] => helloworld
[machine_id] => E6z5ekvQ
[created_by] => 10010
[modified_by] => 10010
[created] => 2011-09-22T16:30:11-07:00
[modified] => 2011-09-22T16:30:11-07:00
)
[1] => Array
(
[gear] => test
[machine_id] => E6z5ekvQ
[created_by] => 10010
[modified_by] => 10010
[created] => 2011-09-22T16:44:25-07:00
[modified] => 2011-09-22T16:44:25-07:00
)
[2] => Array
(
[gear] => test2
[machine_id] => E6z5ekvQ
[created_by] => 10010
[modified_by] => 10010
[created] => 2011-09-22T16:45:43-07:00
[modified] => 2011-09-22T16:45:43-07:00
)
)
So basically the matching key for both is gear. So we should match the gear from the first object, with the second gear in the array, and return something like:
stdClass Object
(
[gear] => helloworld
[status] => running
[started] => 40 Minutes Ago
[start] => index.js
[route] => 127.0.0.1:3000
[parameters] => Array
(
)
[machine_id] => E6z5ekvQ
[created_by] => 10010
[modified_by] => 10010
[created] => 2011-09-22T16:30:11-07:00
[modified] => 2011-09-22T16:30:11-07:00
)
Notice, that the gear is merged into one property of the object, obviously gear does not appear twice. Ideas?
If you could index the array by gear or some unique value, it would be a lot easier.
$indexed = array();
// create an array using 'gear' as the index
foreach($arrayValue as $value) {
$indexed[$value['gear']] = $value;
}
// loop over each object
foreach($objectArray as $obj) {
$value = $indexed[$obj->gear]; // find the corresponding array
foreach($value as $name => $val) {
$obj->$name = $val; // assign each array index/value pair to the object
}
}
If possible to get your code to return the array with the index by default, you can remove the first foreach loop.
Hope that helps.