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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-
I've got a bunch of content pulled out of several databases and compiled into a nested array like this:
Array
(
[0] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[1] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
Since I cannot sort the content whilst it is retrieved (because it is done using several queries from as many databases) I want to dig down a level into the array and sort by the "published" date whilst maintaining the depth of the array so I end up with...
Array
(
[0] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[1] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
I'm pretty sure array_multisort() is the way to go but I can't think where to start! Could somebody be so kind as to give me a pointer?
Thanks to #Rizier123's policing of my question I figured it out myself. For anyone else finding this thread here's my solution using the example array I supplied above.
//-- get disorganised content from databases here
usort($array, "publishcmp");
//-- output organised content here
function publishcmp($a , $b){
if ($a['published'] == $b['published']) {
return 0;
}
return ($a['published'] < $b['published']) ? -1 : 1;
}
Pretty much taken verbatim from php.net
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 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 ?
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.