I have an array in PHP which looks like this:
Array
(
[2] => post4.post
[3] => post7.post
[4] => post5.post
[5] => post3.post
[6] => post6.post
[7] => post1.post
[8] => post2.post
)
How could I arrange it so it could look like this?
Array
(
[0] => post7.post
[1] => post6.post
[2] => post5.post
[3] => post4.post
[4] => post3.post
[5] => post2.post
[6] => post1.post
)
The array has a list of the files contained in a folder. In my local server looks like the second example but on a standard server looks like the first one.
Thanks in advance :D
You are looking for rsort.
PHP has great documentation. I suggest you have a look at the array functions and the Sorting Arrays article (which can be easily find via Google ;))
rsort($array) will sort an array in reverse/descending order (from high to low).
For future reference use this page (http://php.net/manual/en/array.sorting.php) to identify the most suited array sorting method for your needs.
You don't want to use rsort because it will reassign array indexes. Instead use arsort().
Related
I have a PHP string array that I would like to convert into a PHP array object.
string ->
"Array(
[0] => Array
(
[item_id] => 315428396
[title] => Luke Heith
[link] => somelinks
[rights] => Array
(
[0] => update
[1] => view
[2] => grant_view
[3] => delete
[4] => comment
[5] => add_task
[6] => subscribe
[7] => grant
[8] => add_file
[9] => add_conversation
[10] => rate
)"
Solution a PHP object array. Does someone know how I can achieve this?
That format is a debugging output format only. It is not possible to 100% convert this back into an actual array; you will always find edge cases where the input will represent ambiguous values. There's also no function to convert this back into an array; you'd have to write one yourself, which is something of an ambitious project.
In short: use a format which can actually be deserialised (like JSON or serialize), this one cannot.
I want to get the registered function names in an array.
Is it possible?
It will be great if I can get that array from my client side code.
I'm not sure if this is what you mean, but try:
print_r(get_defined_functions());
This will give you something like this:
Array
(
[internal] => Array
(
[0] => zend_version
[1] => func_num_args
[2] => func_get_arg
[3] => func_get_args
[4] => strlen
[5] => strcmp
...
[1274] => xmlwriter_write_dtd_attlist
[1275] => xmlwriter_output_memory
[1276] => xmlwriter_flush
)
[user] => Array
(
[0] => foo
[1] => bar
)
)
The user subarray is the one you're looking for.
I need to split an array but its not something I have done before.
The array is taken from a MySQL database by mysql_fetch_array:
Example:
Array (
[0] => Audi
[make] => Audi
[1] => 80 1.3
[model] => 80 1.3
[2] => 1297
[cc] => 1297
[3] => 1297
[choice] => 1297
[4] => 60
[bhp] => 60
[5] => 08/81-03/87
[date] => 08/81-03/87
[6] => EP
[engcode] => EP
[7] => Petrol
[fuel] => Petrol
[8] => -
[notes] => -
[9] => CAM BELT KIT
[type] => CAM BELT KIT
[10] => KTB201
[partno] => KTB201
[11] => AUDI
[itemspec] => AUDI
)
I need to change this to:
Array (
[0] => Audi
[1] => 80 1.3
[2] => 1297
[3] => 1297
[4] => 60
[5] => 08/81-03/87
[6] => EP
[7] => Petrol
[8] => -
[9] => CAM BELT KIT
[10] => KTB201
[11] => AUDI )
I have tried a few methods using filter array but I cannot seem to get it to output correctly, any ideas?
Use the function properly in the first place instead of trying to fix the goof-up after.
You can use:
mysql_fetch_array ( $result, MYSQL_NUM );
It will return only numeric keys.
As far as I can see, those arrays are the same, it's just you have dropped the associative keys in favour of the numeric ones. What process can't you achieve without removing them?
The way I see it, you should be able to use the original array for the majority of processing purposes.
try using mysql_fetch_row instead of mysql_fetch_array
references
http://nz.php.net/manual/en/function.mysql-fetch-row.php
http://nz.php.net/manual/en/function.mysql-fetch-array.php
You can use array_unique to keep only different elements from your array, but I'm not sure this is the better solution.
The "problem" is, according to mysql_fetch_array documentation, that the default fetch mode is MYSQL_BOTH, you should try using MYSQL_NUM :
$array = mysql_fetch_array($result, MYSQL_NUM);
Seems like you using mysql_fetch_array($result, MYSQL_BOTH) statement. Change it to mysql_fetch_array($result, MYSQL_NUM)
mysql_fetch_array already has an option to only return numeric keys:
$row = mysql_fetch_array($result, MYSQL_NUM);
Hope that helps.
(BTW, you wouldn't really call what you were asking "splitting" an array.)
I have an array as follows
Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => 13006678321287904362.jpg )
How can I insert an item to the existing array with out overwriting the existing element in specified index
Consider I would like to create as follows
Array ( [0] => application [1] => modules [2] => Ride [3] => externals [4] => images [5] => uploads [6] => profile [7] => 116 [8] => model [9] => 13006678321287904362.jpg )
Any help please
To elucidate on array_splice:
array_splice($array, $position, 0, $insert);
Where $array is the current array, $position is where you want to add the new item and $insert is the item to add (can be a new array)
You can use array_slice, array_push and array_merge, I don't know if a better solution does exist.
This should work for you if you want to add it to the end of the array
<?php
$array[] = $var;
?>
what would be the efficient way of saving the following array using php (cakephp)?
each value needs to go into a new row in the table?
Array
(
[0] => 6786754654
[1] => 5643564545
[2] => 344544545
[3] => 233245654654
[4] => 453454654654
[5] => 6546542323
[6] => 654654654
[7] => 645654654
etc....
)
thanks
2 choices:
Format the array as required by Model::saveAll()
Loop through the array calling Model::create(), then Model:save()
I'd recommend option 1 as you can use Model::saveAll($data, array('validate' => 'first')); to ensure that all values are valid before saving any of them.