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 ?
Related
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']
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"];
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've got an array like the one below, and I would like to count how many times an agentName occurs.
Basically I want to find out how many times hello#url.com and BYE#url.com are occurring. Awnser for the below should be hello#url.com = 3 and BYE#url.com = 2
Array
(
[0] => stdClass Object
(
[text_date] => 2013-12-04 19:27:29
[name] => hello#url.com
)
[1] => stdClass Object
(
[text_date] => 2013-12-07 19:18:32
[name] => hello#url.com
)
[2] => stdClass Object
(
[text_date] => 2013-12-08 09:59:30
[name] => hello#url.com
)
[3] => stdClass Object
(
[text_date] => 2013-12-04 12:23:24
[name] => BYE#url.com
)
[4] => stdClass Object
(
[text_date] => 2013-12-04 13:10:18
[name] => BYE#url.com
)
)
Any ideas if this is possible?
If you are using PHP 5.5, you could do like this
$new_array = array_count_values(array_values($yourarray,'name'));
foreach($new_array as $k=>$v)
{
echo "$k occurred $v times\n";
}
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
he guys,
as you know, in new HTML5 you can upload multiple files pretty easily.
but what is my problem here is how to sort $_FILES array by column "name"?
here is sample array of $_FILES
Array (
[item_file] => Array (
[name] => Array (
[0] => redphotonzillion1.jpg
[1] => redphotonzillion2.jpg
[2] => redphotonzillion3.jpg
[3] => redphotonzillion4.jpg
[4] => redphotonzillion5.jpg
[5] => redphotonzillion6.jpg
)
[type] => Array (
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
[3] => image/jpeg
[4] => image/jpeg
[5] => image/jpeg
)
[tmp_name] => Array (
[0] => /var/www/clients/client0/web6/tmp/phpi4i5fE
[1] => /var/www/clients/client0/web6/tmp/phpzHwagk
[2] => /var/www/clients/client0/web6/tmp/phpYpAgg0
[3] => /var/www/clients/client0/web6/tmp/phpAIdngG
[4] => /var/www/clients/client0/web6/tmp/phpyamugm
[5] => /var/www/clients/client0/web6/tmp/phpFvWBg2
)
[error] => Array (
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
)
[size] => Array (
[0] => 7990
[1] => 8985
[2] => 8014
[3] => 7621
[4] => 7023
[5] => 7151
)
)
)
all i want is to sort this array by name
For this specific case, PHP provides just the right function: array_multisort():
It can sort a multi-dimensional array (or multiple flat arrays) by first sorting the first array and then applying the resulting order to all other arrays as well (see documentation for examples).
array_multisort(
// Array used to sort + optional parameters
$_FILES['item_file']['name'], SORT_ASC, SORT_STRING
// All other arrays to be sorted in the same order
$_FILES['item_file']['type'],
$_FILES['item_file']['tmp_name'],
$_FILES['item_file']['error'],
$_FILES['item_file']['size']
);
This aproach will keep all meta information (type, tmp_name, error, size) assigned to the corresponding index of the file name so it can be used.
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.