sort in explode variable [duplicate] - php

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x

Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live

have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

Related

how to query array within an array [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
When I execute a print_r I get this result...
Array (
[0] => Array (
[A_program_id] => a0F36000008PIYF
[XC_program_logo] => louisville.jpg
)
[1] => Array (
[A_program_id] => a0F36000008qjSp
[XC_program_logo] => alabama.jpg
)
[2] => Array (
[A_program_id] => a0F36000008pRxL
[XC_program_logo] => trinity.jpg
)
)
How do I make a while loop or whatever to properly fetch needed to query something like this:
//zero based
echo"".$rows[0][0]."</td><td>"".$rows[1][0].""</td><td>"".$rows[2][0]."";
echo"".$rows[0][1]."</td><td>"".$rows[1][1].""</td><td>"".$rows[2][1]."";
to show
louisville.jpg alabama.jpg trinity.jpg
a0F36000008PIYF a0F36000008qjSp a0F36000008pRxL
please help thx
One possibility is to use array_column to extract each rows data from the source, and then implode to add the </td><td> between each value:
echo implode('</td><td>', array_column($rows, 'XC_program_logo'));
echo implode('</td><td>', array_column($rows, 'A_program_id'));
Demo on 3v4l.org
This will give the same output as the two echo statements in your question.

Get array of user IDs from multi-dimensional array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example

php unset array element while keeping numbers in order [duplicate]

This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);

Remove the array that is already in another array [duplicate]

This question already has answers here:
difference between two arrays
(7 answers)
Closed 7 years ago.
How can i remove the elements of array that is from another array
Here is my Array Alpha ["1","2","4"]
And the Array Beta ["1"]
How can i remove the elements of Array Alpha that already contains in Array Beta
i.e., After removal it will remove the 1 as it is contained in Array Beta and returns only ["2","4"]
By using array_diff:
array_diff($alpha, $beta);
Working example: http://3v4l.org/fZRGD
$a = [1,2,4];
$b = [1];
$a = array_diff($a,$b);
print_r($a);
Yields:
Array ( [1] => 2 [2] => 4 )
And if you don't want to preserve original keys, you take only array of values:
$a = array_values(array_diff($a,$b));
print_r($a);
Then it gives:
Array ( [0] => 2 [1] => 4 )

php sort array keys in multidimensional array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php

Categories