Get value from a php array [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a shipping API which produces a variable called $quotereply.
print_r ($quotereply) gives me the following output:
Array ( [replycode] => 200 [replymessage] => success [replytype] => quote [quote] => Array ( [services] => Array ( [noofservices] => 2 [service1] => Array ( [name] => TestService1Before12am [description] => Test Service 1 Before 12am [carrier] => Camel [price] => 10 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) [service2] => Array ( [name] => TestService2Anytime [description] => Test Service 2 Anytime [carrier] => Pigeon [price] => 5 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) ) ) [custom] => Array ( [data] => [orderid] => ) )
My question is, how can I extract a value such as [noofservices] from this? I can't really get my head round what I'm looking at, is it an array within an array?
Thanks in advance!

Just try with:
$quotereply['quote']['services']['noofservices']

it is multidimensional array so you should follow array index keys]
echo $quotereply["quote"]["services"]["noofservices"];

Related

How to process the multi dimensional array into specific array? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I want the array to be processed into my final array how am I supposed to achieve this? Below is the array that I have which I got from the loop.
Array
(
Array
(
[part_id] => 2338117
[supplier] => COOLDRIVE DISTRIBUTION
[quantity] => 12
)
Array
(
[part_id] => 2338117
[supplier] => ROLAN
[quantity] => 20
)
Array
(
[part_id] => 51154
[supplier] => ROLAN
[quantity] => 20
)
)
into the final array.
Array
(
[COOLDRIVE DISTRIBUTION] => Array
(
[proudctID] => Array
(
[0] => 2338117
)
)
[ROLAN] => Array
(
[productID] => Array
(
[0] => 2338117
[1] => 51154
)
)
)
You can use something like this in your case. Considering your old array as $oldarr
$newarr = array();
foreach($oldarr as $value){
$newarr[$value['supplier']]['productID'][] = $value['part_id'];
}
print_r($newarr);

convert this multi dimensional 3 level deep array to 2 level deep or 1 level deep array so can be easily accessible) in PHP) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to make it more simple (means I do not want a multi dimensional array)i want to just simply access one row at a time . convert this multi dimensional 3 level deep array to 2 level deep or 1 level deep array so can be easily accessible) in PHP
Array
(
[0] => Array
(
[0] => Array
(
[id] => 2
[name] => 22" 120g Honey Comb
[sku] => Sonia-22
[price] => 100
[qty] => 74
[image] =>
[description] =>
[attribute_value_id] => null
[brand_id] => null
[category_id] => null
[store_id] => 3
[availability] => 1
)
[1] => Array
(
[id] => 4
[name] => Side Fringe2
[sku] => FrSideHH#1B-WU
[price] => 150
[qty] => 195
[image] =>
[description] =>
[attribute_value_id] => null
[brand_id] => ["1"]
[category_id] => ["5"]
[store_id] => 3
[availability] => 1
)
)
)
Your varDump is showing by the looks of it one parent array with many children, so the simplest way of tackling it would be below:
foreach($mainArray[0] as $child){
foreach($child as $row){
echo $row['name'] . "\n";
}
}
If you wanted to use the names as a separate array for use elsewhere then do:
array_push($myNamesArray, $row['name']);
Place it within the child foreach loop above.
This gives you a single dimensional array which you can loop on elsewhere in your code.

How to fetch the array record? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I fetch an array from API using
<?php
$json = json_decode(file_get_contents("API_URL"), TRUE);
echo '<pre>';
print_r($json);
?>
I got that result like that
Array
(
[deals] => Array
(
[0] => Array
(
[activeDa
te] => 1430332361000
[bogo] =>
[categories] => Array
(
[0] => Array
(
[id] => 29057
[name] => Bath & Body
)
[1] => Array
(
[id] => 20733
[name] => Cosmetics
)
[2] => Array
(
[id] => 29190
[name] => Skin Care
)
[3] => Array
(
[id] => 20856
[name] => Fragrances
)
[4] => Array
(
[id] => 29059
[name] => Beauty & Personal Care
)
)
[clearance] =>
[couponCode] => HAPPYMOM
[dealImageUrl] => http://cdn.savings.com/logo/1737578.png
[dealUrl] => http://www.savings.com/m/p/19561077/8306099/c?afsrc=1&up=2015-05-01-05-15
[description] => Go through this link to get Assorted Spring Getaway tote for only $20 on orders $40 or more, save 80%. Restrictions may apply. Limited time offer only or when supplies run out.
[discount] => 1
[exclusive] =>
[freeShipping] =>
[homePageStaffPick] =>
[id] => 3862713
[lastUpdated] => 1430332361000
[merchantDisplayUrl] => http://www.bathandbodyworks.com
[merchantId] => 236514
[merchantImageUrl] => http://cdn.savings.com/logo/1737578.png
[merchantName] => Bath and Body Works
[merchantPageStaffPick] =>
[merchantScore] => 17
[merchantUrl] => http://www.savings.com/m/p/19561077/1742990/c?afsrc=1
[minimumSpend] => 0.00
[mobileMonetized] =>
[monetized] => 1
[printable] =>
[promotion] => 80% Off
[rebate] =>
[scope] => SITE_WIDE
[score] => 579
[siteUrls] => Array
(
[0] => http://www.bathandbodyworks.com
)
[startDate] => 1430290800000
[tip] =>
[title] => Get 80% off Assorted Spring Getaway Tote on Orders Over $40 - Only $20
[validated] =>
[voteDown] => 0
[voteUp] => 0
)
My question is how i get the value of [deals][categories][0][name] ?
I want to store value of categories name.
You have your answer in your question itelf. just a little modifiction needed. Please try this:-
echo $yourarrayname['deals'][0]['categories'][0]['name'];
Note:-since category is on the Zero th index of deals. So put zero index before category index.
You already have your answer. The only thing you need to do is make them literal strings and take the first element in the deals array. So:
echo $json['deals'][0]['categories'][0]['name']

merging mutlidimensional array together to have a continous key [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to merge this two arrays together
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.700000
[count] => 300
[text] => Chris
)
)
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.900000
[count] => 400
[text] => Chris
)
[1] => Array
(
[type] => Person
[relevance] => 0.500000
[count] => 200
[text] => Tom
)
)
so i might have a result like this
Array
(
[0] => Array
(
[type] => Person
[relevance] => 0.900000
[count] => 400
[text] => Chris
)
[1] => Array
(
[type] => Person
[relevance] => 0.500000
[count] => 200
[text] => Tom
)
[2] => Array
(
[type] => Person
[relevance] => 0.700000
[count] => 300
[text] => taye
)
)
Like this?
$array_final = array_merge($array_1, $array_2);
refer array_merge at official documentation site
Try using foreach
// $array1 is your original array
foreach($array2 as $val) {
array_push($array2,$val)
}
Use array_merge docs and is faster to check documentation:D
Did you search the PHP Doc ?
what about array_merge_recursive ?

Extract element of array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array I returned via json_decode and want to extract only the image_url below. What php commands would I run from this, to get the image_url below?
[0] =>
stdClass Object (
[id] => 36210508
[name] => Untitled
[description] =>
[times_viewed] => 2
[rating] => 0
[created_at] => 2013-05-31T15:08:36-04:00
[category] => 0
[privacy] =>
[width] => 275
[height] => 183
[votes_count] => 0
[favorites_count] => 0
[comments_count] => 0
[nsfw] =>
[license_type] => 0
[image_url] => http://pcdn.500px.net/36210508/5de3b4bb9cdee9f429e2a329a1d0619dd5b28ce1/4.jpg
[images] =>
Array (
[0] =>
stdClass Object (
[size] => 4
[url] => http://pcdn.500px.net/36210508/5de3b4bb9cdee9f429e2a329a1d0619dd5b28ce1/4.jpg
)
)
[store_download] =>
[store_print] =>
[user] =>
stdClass Object (
[id] => 3677956
[username] => jsniff12
[firstname] => Jacob
[lastname] => Sniff
[city] =>
[country] =>
[fullname] => Jacob Sniff
[userpic_url] => /graphics/userpic.png
[upgrade_status] => 0
[followers_count] => 0
[affection] => 0
)
)
)
Please try to make some effort before posting questions here. You can read about php arrays and objects.
You will be able to get image_url the following way:
echo $array[0]->image_url;
Note: this kind of question is very likely to be closed since it's too specific and too basic.

Categories