I'm struggling with extracting values from an array. print_r($offers); output is as follow:
Array
(
[0] => Object
(
[id] => 41302512
[amount] => 244
[price] => 10.17
[sellerId] => 1678289
[sellerName] => stan_J23
)
[1] => Object
(
[id] => 41297403
[amount] => 51
[price] => 10.18
[sellerId] => 2510426
[sellerName] => kszonek1380
)
[2] => Object
(
[id] => 41297337
[amount] => 581
[price] => 10.18
[sellerId] => 2863620
[sellerName] => NYski
)
)
However, echo $offers[0]['id']; doesn't work. I need to extract the values for each array node to variables i.e. $id=value_of_key_id and so on. The array has 10 nodes from 0 to 9.
Try echo $offer[0]->{'id'}.
It says it's an object, and you need to get the 'id' key in that way with objects.
See Docs: http://www.php.net/manual/en/language.types.object.php
$offers is an array of objects, not array. You will need to access the elements via $offers[0]->id; and so on
Thanks to all, working code is:
foreach ($offers as $offer) {
$id = $offer->id;
$amount = $offer->amount;
$price = $offer->price;
$sellerId = $offer->sellerId;
$sellerName = $offer->sellerName;
echo "$id<br />$amount<br />$price<br />$sellerId<br />$sellerName<br /><hr /><br />";
}
Related
I have the following problem.
I am trying to read a Json file but I am not getting the desired result. I have the following data.
I would like to read the features and there is a mistake in finding out somewhere.
I get the error message when reading out:
Warning: Illegal string offset 'features'
Output
Array
(
[objectIdFieldName] => OBJECTID
[uniqueIdField] => Array
(
[name] => OBJECTID
[isSystemMaintained] => 1
)
[globalIdFieldName] =>
[geometryProperties] => Array
(
[shapeAreaFieldName] => Shape__Area
[shapeLengthFieldName] => Shape__Length
[units] => esriMeters
)
[geometryType] => esriGeometryPolygon
[spatialReference] => Array
(
[wkid] => 4326
[latestWkid] => 4326
)
[fields] => Array
(
[0] => Array
(
[name] => GEN
[type] => esriFieldTypeString
[alias] => GEN
[sqlType] => sqlTypeOther
[length] => 33
[domain] =>
[defaultValue] =>
)
[1] => Array
(
[name] => cases
[type] => esriFieldTypeInteger
[alias] => Anzahl Fälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
[2] => Array
(
[name] => deaths
[type] => esriFieldTypeInteger
[alias] => Anzahl Todesfälle
[sqlType] => sqlTypeOther
[domain] =>
[defaultValue] =>
)
)
[features] => Array
(
[0] => Array
(
[attributes] => Array
(
[GEN] => Celle
[cases] => 220
[deaths] => 15
)
...........
My Code
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = json_decode($json,true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
foreach($data as $row){
echo $row['features']->$row['attributes']->$row['GEN'];
}
Where am I wrong in reading?
It only has to be read what is in parentheses right?
So actually features-> attributes-> Gen to get the GEN query
Let's try change to this.
foreach($data as $row){
echo $row['features'][0]['attributes']['GEN'];
}
Sorry this is my fault when seem does'nt read clearly your question.
With GEN attribute you don't use loop to get this value.
If you wanna get GEN only your code should be that.
$url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=GEN%20%3D%20%27CELLE%27&outFields=GEN,cases,deaths&outSR=4326&f=json";
$json = file_get_contents($url);
$data = (array) json_decode($json, true); //decode json result as array and thenloop it
print '<pre>';
print_r($data);
echo $data['features']['attributes']['GEN'];
// foreach($data as $row){
// echo $row['features']->$row['attributes']->$row['GEN'];
// }
Working on a personal project that will pull results from an API with full details of each pokemon.
So far I got the contents of the URL and returned the results into a JSON array format.
At the moment I am stuck on trying to retrieve results for[stats] inside from the array in an efficient manner.
private function getGenOnePokemon()
{
// the url of the api
$url = $this->baseUrl;
//get the contents of $url var and decode it into a json array
$json = file_get_contents($url , true);
$pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);
// array output of pokemon
echo '<pre> ';
print_r($pokemon);
echo'</pre>';
//echo out value as speed
foreach($pokemon['results'][0] as $happy)
{
echo $happy['name'] . '<br />';
}
// echo base_stat value for speed with value of 90
echo $pokemon['stats'][0]['base_stat'];
}
However I do not seem to get anywhere much printing values/keys as I need to add something else to have full access to the values?
Would prefer not to directly access results, like I am doing with base_stat as plan on using this logic to pass into HTML View layer later.
Example of print_r dump (not full dump as really long) Full example: https://pokeapi.co/api/v2/pokemon/pikachu
Array
(
[forms] => Array
(
[0] => Array
(
[url] => https://pokeapi.co/api/v2/pokemon-form/25/
[name] => pikachu
)
)
[abilities] => Array
(
[0] => Array
(
[slot] => 3
[is_hidden] => 1
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/31/
[name] => lightning-rod
)
)
[1] => Array
(
[slot] => 1
[is_hidden] =>
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/9/
[name] => static
)
)
)
[stats] => Array
(
[0] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/6/
[name] => speed
)
[effort] => 2
[base_stat] => 90
)
[1] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/5/
[name] => special-defense
)
[effort] => 0
[base_stat] => 50
)
[2] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/4/
[name] => special-attack
)
[effort] => 0
[base_stat] => 50
)
[3] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/3/
[name] => defense
)
[effort] => 0
[base_stat] => 40
)
[4] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/2/
[name] => attack
)
[effort] => 0
[base_stat] => 55
)
[5] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/1/
[name] => hp
)
[effort] => 0
[base_stat] => 35
)
)
Any advice on how to access the data using foreach or other tips greatly appreciated. Thank you!
PHP has a specific function designed to target columnar data from arrays. It is called array_column()
If you want to isolate all of the name elements inside the forms subarray, use this:
$names=array_column($pokemon['forms'],'name');
If you want to isolate all of the base_stat elements inside of the stats subarray, use this:
$base_stats=array_column($pokemon['stats'],'base_stat');
Now you will have $names and $base_stats which are single-dimensional arrays by which you can perform additional processes or return from the function. Clean, intuitive, and simple.
Your $pokemon array doesn't contain a results field. There's only an forms field. So you should iterate over forms to print the names of the forms.
foreach($pokemon['forms'] as $happy) {
echo $happy['name'] . '<br />';
}
You could do the same thing with the stats
foreach($pokemon['stats'] as $stat) {
$base_stat = $stat['base_stat'];
// ...
}
Array
(
[total_results] => 47023
[products] => stdClass Object
(
[product] => Array
(
[0] => stdClass Object
(
[binds] => 1627836:3269460;1627836:3269461;1627836:3269458;1627839:3269490;1627841:3269526
[cid] => 50010368
[name] => 三九光学雷朋太阳镜RayBan超轻树脂镜片运动墨镜RB4039 816/13
[pic_url] => //img06.taobaocdn.com/bao/uploaded/i6/TB1532SOVXXXXcJXXXXN951FFXX_093838.jpg
[price] => 980.00
[product_id] => 717498724
[props] => 20000:55652;1629764:3596338
[status] => 0
[tsc] => CIKCQQU
)
I didn't code long time ago and trying to print this array using for each loop.
Can anyone help please in printing the array items such as the 'price' for example ?
Sure, it's just looping over an object and an array:
foreach ($array['products']->product as $product) {
echo $product->price;
}
I have two arrays
array_1 is multidimensional but the location of the value I need is
array_1['id']
array_2 is also multidimensional and the location is further down inside.
array_2['data']['id']
Now I am trying to execute my code only if (===) those 2 ID'S are identical.
Further Information is that this is a combination of 2 API'S but the code for the API'S is working fine because if I try to get information from both API'S it shows me.
Code:
$orders = $apiSeo->orders->get();
$parcels = $apiSend->parcels->get();
foreach($orders as $id => $order){
if(($orders[$id]['paymentStatus'] == 'paid') || ($orders[$id]['paymentIsPost'] == true)){
echo '<pre>';
var_export($orders[$id]['id']);
var_export($orders[$id]['lastname']);
var_export($orders[$id]['paymentStatus']);
var_export($orders[$id]['paymentIsPost']);
echo '</pre>';
}
}
what have I tried adding:
foreach($parcels as $id2 => $parcel){
if($orders[$id]['id'] === $parcels[$id2]['data']['order_id']){//}
}
and also no succes with array_key_exists or in_array.
My output with var_export or var_dump is nothing when adding these pieces of code. also no error while i have error reporting on. So I Am clueless where the mistake or miscode lies.
print_r request $parcels array
Array
(
[0] => Array
(
[id] => 1440143
[address] => xx
[address_divided] => Array
(
[street] => xx
[house_number] => 8
)
[city] => xx
[company_name] =>
[country] => Array
(
[iso_2] => NL
[iso_3] => NLD
[name] => Netherlands
)
[data] => Array
(
**[seoshop_order_id] => 15802836** HERE!
)
[date_created] => 03-03-2016 07:41:39
[email] => xx#live.nl
[name] => xx
[postal_code] => xx
[reference] => 0
[shipment] => Array
(
[id] => 10
[name] => DHL Complete with signature
)
[status] => Array
(
[id] => 1000
[message] => Ready to send
[history] =>
)
[telephone] => xx
[tracking_number] => JVGL0
[label] => Array
(
[normal_printer] => Array
(
[0] => https://panel.sendcloud.nl/api/v2/labels/normal_printer/1440143?start_from=0
[1] => https://panel.sendcloud.nl/api/v2/labels/normal_printer/1440143?start_from=1
[2] => https://panel.sendcloud.nl/api/v2/labels/normal_printer/1440143?start_from=2
[3] => https://panel.sendcloud.nl/api/v2/labels/normal_printer/1440143?start_from=3
)
[label_printer] => h1
)
[order_number] => ORD03
[carrier] => Array
(
[code] => dhl
)
[tracking_url] => xx
)
print_r request $orders array To big to copy and place all array, not necessary also since no other info is needed.
Array
(
[0] => Array
(
**[id] => 15817386** HERE!
I got it to work, Thanks for anyone that tried to understand me and/or help.!
for($x = 0 ; $x < 50 ; $x++){
if(($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == true)){
if($orders[$x]['id'] == $parcels[$x]['data']['seoshop_order_id']){
echo '<pre>';
var_export($orders[$x]['id']);
var_export($orders[$x]['lastname']);
var_export($orders[$x]['paymentStatus']);
var_export($orders[$x]['paymentIsPost']);
var_export($parcels[$x]['name']);
var_export($parcels[$x]['data']['seoshop_order_id']);
echo '</pre>';
}
}
}
This code builds an array:
$size = sizeof($include_quotes);
for ($i=0; $i<$size; $i++) {
$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
if (is_array($quotes)) $quotes_array[] = $quotes;
}
}
If i
print_r($quotes_array);
i get the following:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 ) [1] => Array ( [id] => 2-0-0 [title] => 1-2 working days [cost] => 3.2916666666667 [icon] => [shipping_ts] => [quote_i] => 1 ) [2] => Array ( [id] => 4-0-0 [title] => 2-3 working days [cost] => 2.4916666666667 [icon] => [shipping_ts] => [quote_i] => 2 ) [3] => Array ( [id] => 8-0-0 [title] => Click & Collect [cost] => 0 [icon] => [shipping_ts] => [quote_i] => 3 ) ) [module] => Shipping [tax] => 20 ) )
In some circumstances, I only want the data in field 0 to be passed onto the next part of the code. However, using
$sliced_quotes_array = array_slice($quotes_array,0,1);
Still returns all the results.
What is the correct method to get just:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 )
Any help greatly appreciated because i have tried numerous different ways and no luck yet.
Using the following still returns the same results
$testarray = array(0 => $quotes_array[0]);
print_r($testarray);
Why not just use the array constructor and explicitly include what you need:
array(0 => $quotes_array[0]);
Here is your array:
When you are saying " I only want the data in field 0 to be passed onto the next part of the code", you meant that you only want this data to be passed next, right? :
Array (
[0] => Array (
[id] => advshipper
[module] => Shipping
[tax] => 20
)
)
Is this what you want?
$newArray = array();
foreach ($quotes_array[0] as $items)
{
if (!is_array($items))
{
$newArray[] = $items;
}
}
$newArray will contain that data.
UPDATE
Okay, gotcha.
You can just use this:
$newArray = $quotes_array[0]['methods'][0];
Having done some reading on arrays and after a bit of trial, i found a solution to my problem.
I used:
unset($quotes_array[0]['methods'][1]);
By changing the index number after methods i was able to drop any shipping options i didn't require, whilst still maintaining the functionality.