I'm trying to get information from the $array1 below.
I'm getting with no problems venue's name and location address by doing:
$array2 = array();
$array3 = array();
foreach($array1 as $item){
$array2[] = $item->venue->name;
$array3[] = $item->venue->location->address;
}
But now I need to get the photos url and I don't know how to do it.
Thanks a million!
$array1:
Array
(
[0] => stdClass Object
(
[venue] => stdClass Object
(
[name] => a name
[location] => stdClass Object
(
[address] => main street
)
)
[photos] => stdClass Object
(
[count] => 1
[items] => Array
(
[0] => stdClass Object
(
[url] => http://folder/photo1.jpg
.
.
)))
.
.
$array1[0]->photos->items[0]->url
Remember - you access arrays with [index] parenthesis, objects with -> arrows.
Untested code:
$array2 = array();
$array3 = array();
$photos = array();
foreach($array1 as $item){
$array2[] = $item->venue->name;
$array3[] = $item->venue->location->address;
$item_photo_urls = array();
foreach($item->photos->items as $photo){
$item_photo_urls[] = $photo->url;
}
$photos[] = $item_photo_urls;
}
Now you have a third array called photos , which contains all the photo urls.
Try this :
$url = $item->photos->items[0]->url;
Related
I have this array. I have tried a few things but not getting what I want.
I tried a foreach loop but it does not seem to do it easly and the process takes a long time.
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[display_number] => 100140
[client] => stdClass Object
(
[name] => TAUQIR SHEIKH ET AL
)
)
[1] => stdClass Object
(
[display_number] => 100141
[client] => stdClass Object
(
[name] => YOLANDA SHEIKH ET AL
)
)
I want it to be one simple array
[0] => Array
(
[0] => 100140
[1] => TAUQIR SHEIKH ET AL
)
[1] => Array
(
[0] => 100141
[1] => YOLANDA SHEIKH ET AL
)
Ok So the old code works but now they updated the API and this made it worse. The response is now
(
[data] => Array
(
[0] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[display_number] => 100140
[client] => stdClass Object
(
[name] => TAUQIR SHEIKH ET AL
)
)
[1] => stdClass Object
(
[display_number] => 100141
[client] => stdClass Object
(
[name] => YOLANDA SHEIKH ET AL
)
)
I tried this with the new code... But the array is empty. Where am I going wrong?
//clean and make into an array
$matter_array = array();
if(!empty($response_Decode->data->data) &&
is_array($response_Decode->data->data)) {
foreach ($response_Decode->data->data as $info) {
$d = array();
$d[] = $info->display_number;
$d[] = $info->client->name;
$matter_array[] = $d;
}
}
print_r($matter_array); //For testing
die(); //For testing
I would recommend asking who/what process is populating your dataset first and possibly adjust it there.
If not available then looping is required.
$results = array();
if(!empty($object->data) && is_array($object->data)) {
foreach ($object->data as $info) {
$d = array();
$d[] = $info->display_number;
if(!empty($object->client)) {
$d[] = $object->client->name;
}
$results[] = $d;
}
}
print_r($results);
I'm paranoid with empty(). Code not tested but should get you on the right track.
SO you were close... Thank you.
//clean and make into an array
$matter_array = array();
if(!empty($resp->data) && is_array($resp->data)) {
foreach ($resp->data as $info) {
$d = array();
$d[] = $info->display_number;
$d[] = $info->client->name;
$matter_array[] = $d;
}
}
print_r($matter_array)
Ok so I just simplified the array... AND THAT WORKS!
//clean and make into an array
$response_Decode=$response_Decode->data;
$response_Decode=$response_Decode[0];
//print_r ($response_Decode);
//die(); //For testing
$matter_array = array();
if(!empty($response_Decode->data) && is_array($response_Decode->data)) {
foreach ($response_Decode->data as $info) {
$d = array();
$d[] = $info->display_number;
$d[] = $info->client->name;
$matter_array[] = $d;
}
}
Hi I have an array of object like this
$json = json_decode($featureJson);
//which returns below
Array
(
[0] => stdClass Object
(
[productID] => 1
[productName] => Toyo
[assessments] => Array
(
[0] => stdClass Object
(
[answer] => Yes
)
[1] => stdClass Object
(
[answer] => Yes
)
...
)
)
[1] => stdClass Object
(
[productID] => 2
[productName] => Maze
[assessments] => Array
(
[0] => stdClass Object
(
[answer] => Yes
)
[1] => stdClass Object
(
[answer] => Yes
)
...
)
)
)
and I have another array that needs to match the ID of $json(Array of Objects) and return its productName.
$string = "1,2|2,1";
$IdArray = explode('|', $string);
$foo = '';
foreach ($IdArray as $item) {
$foo .= '{' . $item . '},';
}
echo $foo;
$foo return {1,2},{2,1} and I match $json so will display - {Toyo,Maze},{Maze,Toyo}, how can I do that? I have some hint using array_map()
but still got no idea to match in objects.
It's easier if you separate an array for the names first.
<?php
$featureJson = '[{"productID":1,"productName":"Toyo","assessments":[{"answer":"Yes"},{"answer":"No"}]},{"productID":2,"productName":"Maze","assessments":[{"answer":"Yes"},{"answer":"Yes"}]}]';
$json = json_decode($featureJson);
// Make an array of names
$names = [];
foreach($json as $products){
$names[$products->productID] = $products->productName;
};
$string = "1,2|2,1";
$IdArray = explode('|', $string);
$foo = [];
foreach ($IdArray as $ids) {
$ids = explode(',',$ids);
$fooItem = [];
foreach($ids as $id){
$fooItem[] = $names[$id];
}
$foo[]= '{' . implode(',',$fooItem) . '}'; }
echo implode(',',$foo);
Result:
{Toyo,Maze},{Maze,Toyo}
Check here
I have an array of php objects that looks something like this:
Array
(
[0] => stdClass Object
(
[order_id] => 1513
[order_total] => 12500.00
[sales_rep] => Doe_John
)
[1] => stdClass Object
(
[order_id] => 1046
[order_total] => 3300.00
[sales_rep] => Doe_John
)
[2] => stdClass Object
(
[order_id] => 337
[order_total] => 4500.00
[sales_rep] => Mosby_Ted
)
)
I am trying to get an array that is set up more like this:
Array
(
[0] => stdClass Object
(
[sales_rep] => Doe_John
[total_sales] => 15800.00
)
[1] => stdClass Object
(
[sales_rep] => Mosby_Ted
[total_sales] => 4500.00
)
)
I want to combine all of the objects with the same "sales_rep" and get the sum of their associated "order_total", which you can see an example of in my desired array above. Any thoughts on how to accomplish this? I've been at it for hours now and have not been able to figure out a solution.
Thanks so much for the help!
try this
$tmp = array();
foreach($objs as $obj){ // where `$objs` is your objects
if(!in_array($obj->sales_rep,array_keys($tmp))){
$tmp[$obj->sales_rep] = (object)array(
'sales_rep' => $obj->sales_rep,
'total_sales' => $obj->order_total
);
}else{
$tmp[$obj->sales_rep]->total_sales += $obj->order_total;
}
}
print_r(array_values($tmp));
$obj0 = new StdClass();
$obj0->order_id = 1513;
$obj0->order_total = 12500.00;
$obj0->sales_rep = 'Doe_John';
$obj1 = new StdClass();
$obj1->order_id = 1046;
$obj1->order_total = 3300.00;
$obj1->sales_rep = 'Doe_John';
$obj2 = new StdClass();
$obj2->order_id = 337;
$obj2->order_total = 4500.00;
$obj2->sales_rep = 'Mosby_Ted';
$array = array(
$obj0,
$obj1,
$obj2,
);
$newArray = array();
foreach ($array as $item) {
if (array_key_exists($item->sales_rep, $newArray)) {
$newObj = $newArray[$item->sales_rep];
$newObj->order_total += $item->order_total;
} else {
$newObj = new StdClass();
$newObj->sales_rep = $item->sales_rep;
$newObj->order_total = $item->order_total;
$newArray[$newObj->sales_rep] = $newObj;
}
}
print_r(array_values($newArray));
I have two arrays, e.g
$mainArray = array('a','b','c','d','e','f','g');
$subArray = it contains an array of objects e.g
array( objec1, objec2, object3, object4) ...
within each of the objects, holds the value that matches one of the keys in the
$mainArray.
my Question now is, how am i gonna match and put the correct objects to the mainArray, so that
it should appear like this e.g
$mainArray = array('a'=> array(object3,object2), 'b' => array(object4,object1));
$result = array();
foreach ($subArray as $obj) {
if (!isset($result[$obj->keyOfMainArray])) {
$result[$obj->keyOfMainArray] = array();
}
$result[$obj->keyOfMainArray][] = $obj;
}
Assuming val is your object's property
$mainArray = array('a','b','c','d','e','f','g');
$subArray = array(...);
$result = array();
foreach($subArray as $object) {
$result[$object->val][] = $object;
}
Example result
Array
(
[a] => Array
(
[0] => stdClass Object
(
[val] => a
)
[1] => stdClass Object
(
[val] => a
)
)
[b] => Array
(
[0] => stdClass Object
(
[val] => b
)
)
)
Here, I have the following variable $countries_all:
Array (
[0] => stdClass Object
(
[countries] => India,Afghanestan,Japan,South Africa
)
[1] => stdClass Object
(
[countries] => Singapore,South Africa,India,Pakistan
)
[2] => stdClass Object
(
[countries] => Japan,Australia,India
)
)
But i need to create second array $countries containing only unique values:
Array (
[0] => stdClass Object
(
[countries] => India
)
[1] => stdClass Object
(
[countries] => Afghanestan
)
[2] => stdClass Object
(
[countries] => Japan
)
[3] => stdClass Object
(
[countries] => South Africa
)
[4] => stdClass Object
(
[countries] => Singapore
)
[5] => stdClass Object
(
[countries] => Pakistan
)
[6] => stdClass Object
(
[countries] => Japan
)
[7] => stdClass Object
(
[countries] => Australia
)
)
For splitting by (,) I have used the below method
foreach ($countries_all as $list){
$countrieslist=explode(',', $list->countries);
foreach ($countrieslist as $uni_countries){
echo $uni_countries;
}
}
but how to get unique values?
The array_unique function is what you are looking for.
$countries = array();
foreach ($countries_all as $list){
$countrieslist=explode(',', $list->countries);
foreach ($countrieslist as $country){
$countries[] = $country;
}
}
$countries = array_unique($countries);
$newlist = array();
foreach ($countries_list as $list){
$countrieslist=explode(',', $list->countries);
foreach ($countrieslist as $uni_countries){
echo $uni_countries;
$newlist[] = $uni_countries){
}
}
array_unique($newlist);
$commasplit = function ($list) { return explode(',', $list->countries); };
$countrieslist = array_unique(call_user_func_array('array_merge', array_map($commasplit, $countries_list)));
You may try this
$countrieslist = array();
foreach ($countries_all as $list){
$countrieslist[] = explode(',', $list->countries);
}
$array = array_unique(array_reduce($countrieslist,'array_merge', array()));
An Example.
Try this:
$uniqueCountries = array();
foreach ($countries_list as $list){
$countrieslist=explode(',', $list->countries);
foreach ($countrieslist as $country){
if (!in_array($country, $uniqueCountries)) {
//adding to list
$uniqueCountries[] = $country
} else {
echo $country, ' already in list', PHP_EOL;
}
}
}
$uniqueCoutries contains all found coutries, without duplicates
Instead of asking array_unique or in_array() to perform extra cycles to weed out the duplicates, don't let the duplicate values in in the first place. You can rely on PHP's refusal to have duplicate keys in the same level of an array.
By assigning the single country value not only as the object property, but also as the first level key, you ensure that there be no duplication even if the same country is encountered again while iterating.
This technique will perform faster than earlier posted techniques on this page.
If you don't want the first level keys after iterating, you can re-index the array with array_values().
Code: (Demo)
$result = [];
foreach ($array as $obj) {
foreach (explode(',', $obj->countries) as $country) {
$result[$country] = (object) ['countries' => $country];
}
}
var_export(array_values($result));