I get all keywords separated by commas from database and create arrays from them then I merge all arrays. From the resulted array I keep only unique items array_unique. The problem is that I can't sort resulted array by value.
Here is my code:
$select_all_keywords = mysqli_query($db_connect, "SELECT `keywords` FROM `bookmarks`") or die(mysqli_error());
$keywords_array = array();
while($keywords = mysqli_fetch_assoc($select_all_keywords))
{
$explode_keywords = explode(", ", $keywords['keywords']);
$keywords_array = array_merge($keywords_array, $explode_keywords);
}
$unique_keywords = array_unique($keywords_array);
sort($unique_keywords);
$unique_keywords = array_values($unique_keywords);
print_array($unique_keywords);
The printed array:
array(23) {
[0]=>
string(10) "Awolnation"
[1]=>
string(7) "Belgium"
[2]=>
string(7) "Gravity"
[3]=>
string(21) "Nervo (Musical Group)"
[4]=>
string(5) "R3..."
[5]=>
string(22) "R3hab (Musical Artist)"
[6]=>
string(5) "Remix"
[7]=>
string(4) "Sail"
[8]=>
string(30) "Tomorrowland (Recurring Event)"
[9]=>
string(21) "Tomorrowland Festival"
[10]=>
string(9) "Unlimited"
[11]=>
string(6) "dasdas"
[12]=>
string(10) "freshbooks"
[13]=>
string(11) "gdsfgdsfgds"
[14]=>
string(6) "mockup"
[15]=>
string(3) "php"
[16]=>
string(11) "programming"
[17]=>
string(10) "revolution"
[18]=>
string(17) "tomorrowland 2013"
[19]=>
string(11) "ummet ozcan"
[20]=>
string(10) "web design"
[21]=>
string(7) "wikihow"
[22]=>
string(3) "xml"
}
I tryed almost all sorting array functions. None of them helped me sort the array by value ASC.
I believe you might be looking for asort(), it will sort the array by value. But still keep the keys the same.
Look at the documentation here.
http://php.net/manual/en/function.asort.php
Related
I have an array of n size
suppose my original array is:
array=(alpha,bravo,charlie,delta,echo,foxtrot);
and i want to rotate the above array in leftward
ex output 1st iteration
array=(bravo,charlie,delta,echo,foxtrot,alpha);
and 2nd iteration
array=(charlie,delta,echo,foxtrot,alpha,bravo);
and i want to do this in every iteration till original array is achieved.
Note :The above array i am getting from MySQL output for a specific query. So the original array will be always array=(alpha,bravo,charlie,delta,echo,foxtrot);
Thanks in Advance for any suggestion and help
$array = array('alpha','bravo','charlie','delta','echo','foxtrot');
for($i=0; $i< count($array);$i++)
{
$firstValue = array_shift($array);
array_push($array, $firstValue);
var_dump($array); //here you get your array with the first value shifted to the end of the array
}
Result:
array(6) {
[0]=>
string(5) "bravo"
[1]=>
string(7) "charlie"
[2]=>
string(5) "delta"
[3]=>
string(4) "echo"
[4]=>
string(7) "foxtrot"
[5]=>
string(5) "alpha"
}
array(6) {
[0]=>
string(7) "charlie"
[1]=>
string(5) "delta"
[2]=>
string(4) "echo"
[3]=>
string(7) "foxtrot"
[4]=>
string(5) "alpha"
[5]=>
string(5) "bravo"
}
array(6) {
[0]=>
string(5) "delta"
[1]=>
string(4) "echo"
[2]=>
string(7) "foxtrot"
[3]=>
string(5) "alpha"
[4]=>
string(5) "bravo"
[5]=>
string(7) "charlie"
}
array(6) {
[0]=>
string(4) "echo"
[1]=>
string(7) "foxtrot"
[2]=>
string(5) "alpha"
[3]=>
string(5) "bravo"
[4]=>
string(7) "charlie"
[5]=>
string(5) "delta"
}
array(6) {
[0]=>
string(7) "foxtrot"
[1]=>
string(5) "alpha"
[2]=>
string(5) "bravo"
[3]=>
string(7) "charlie"
[4]=>
string(5) "delta"
[5]=>
string(4) "echo"
}
array(6) {
[0]=>
string(5) "alpha"
[1]=>
string(5) "bravo"
[2]=>
string(7) "charlie"
[3]=>
string(5) "delta"
[4]=>
string(4) "echo"
[5]=>
string(7) "foxtrot"
}
I have a newbie question here, but I'm really stucked. I have a multidimensional array that comes from a SELECT from a database, what I need is pretty simple, I'm doing a graphic where contains the number of results of each column of every user specified.
Heres some code to clarify your minds:
try {
$pdo = new PDO('mysql:host=***; dbname=**;charset=utf8;', '***', '**');
}catch(PDOException $err){
echo "Erro:\n".$err->getMessage();
}
$sql = "SELECT * FROM results
WHERE ra like '****'";
$statement = $pdo->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
$resultPgto = array_column($result, 'indicado_pgto');
print_r($resultadoPgto);
This returns me this array:
array(3) {
[0]=>
string(18) "Indicacao Invalida"
[1]=>
string(3) "Nao"
[2]=>
string(3) "Nao"
}
I need to count the results that are equal to Sim AND it should returns me 0 but instead when I do something like print_r($resultadoPgto == 'Sim'); it returns '1'...
Other funny fact is, when I try to return results that are equal to Nao (theres 2 there) it returns me 0 or 1...
What I'm missing here? Can someone please help?
EDIT 1 :
Counting the result itself will return the number of keys the array has.
Here is the full Result to your understanding.
array(3) {
[0]=>
array(22) {
["dsadsad"]=>
string(7) "***"
[0]=>
string(7) "****"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(25) "lore"
[2]=>
string(25) "ipsum"
["indicado_inscrito"]=>
string(18) "Indica��o Inv�lida"
[3]=>
string(18) "Indica��o Inv�lida"
["indicado_confirmado"]=>
string(18) "Indica��o Inv�lida"
[4]=>
string(18) "Indica��o Inv�lida"
["indicado_presente"]=>
string(18) "Indica��o Inv�lida"
[5]=>
string(18) "Indica��o Inv�lida"
["indicado_aprovado"]=>
string(18) "Indica��o Inv�lida"
[6]=>
string(18) "Indica��o Inv�lida"
["indicado_matriculado"]=>
string(18) "Indica��o Inv�lida"
[7]=>
string(18) "Indica��o Inv�lida"
["indicado_pgto"]=>
string(18) "Indicacao Invalida"
[8]=>
string(18) "Indicacao Invalida"
["validacao_indicacao"]=>
string(3) "N�o"
[9]=>
string(3) "N�o"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[1]=>
array(22) {
["dsdsdasda"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(23) "lore"
[2]=>
string(23) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[2]=>
array(22) {
["asdasdsad"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(15) "lore"
[2]=>
string(15) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
}
The only field I need information is "indicado_pgto", thats why I did the array_column()
What I really need is compare each value and count them, to return me any position that have the string 'Sim' on it, and even if don't have any 'Sim' string on it, it returns me 1
If you have this array after using array_column() on the SQL results, then the assumption is that your target array looks like this:
array(3) {
[0]=>
string(18) "Indicacao Invalida"
[1]=>
string(3) "Nao"
[2]=>
string(3) "Nao"
}
If so, then you should be able to do something simple like this?
<?php
$counter = 0;
foreach ($resultadoPgto as $result) {
if ($result === 'Nao') {
$counter++;
}
}
print_r($counter);
Note that I used the triple equality operator === instead of the double equality operator==.
The weird behaviour you are experiencing with your print_r($resultadoPgto == 'Sim') returning '1' or '0' could be an side effect of using the double equality operator since it does not take the data type in consideration when evaluating the expression.
This will count the occurances of any field in an array that match a specified value.
function count_occ($array, $field, $content) {
$cnt = 0;
foreach( $array as $a) {
if ($a[$field] = $content) { $cnt++; }
}
return $cnt;
}
echo count_occ($result, 'indicado_pgto', 'Sim');
After I use array_unique on an array, when I var_dump the array, it still has the same content, with duplicates:
array(21) {
[0]=> string(10) "tricou_CRS"
[1]=> string(10) "tricou_CRM"
[2]=> string(11) "tricou_CRXL"
[3]=> string(10) "tricou_CBM"
[4]=> string(10) "tricou_CBL"
[5]=> string(10) "tricou_CWS"
[6]=> string(11) "tricou_CWXL"
[7]=> string(10) "tricou_CRS"
[8]=> string(10) "tricou_CRM"
[9]=> string(11) "tricou_CRXL"
[10]=> string(10) "tricou_CBM"
[11]=> string(10) "tricou_CBL"
[12]=> string(10) "tricou_CWS"
[13]=> string(11) "tricou_CWXL"
[14]=> string(10) "tricou_CRS"
[15]=> string(10) "tricou_CRM"
[16]=> string(11) "tricou_CRXL"
[17]=> string(10) "tricou_CBM"
[18]=> string(10) "tricou_CBL"
[19]=> string(10) "tricou_CWS"
[20]=> string(11) "tricou_CWXL" }
Which obviously has some duplicates. Now, there's not much code that could help, really, because it's just array_unique($myarr);var_dump($myarr);.
So, what am I missing there? Shouldn't array_unique remove the duplicates? Both the type and the content is the same in many of the array's positions.
$uniquearr = array_unique($myarr);
var_dump($uniquearr);
Always read docs first
http://www.php.net/array_unique
array_unique
Return Values
Returns the filtered array.
Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...
I'm trying to make a multidimensional array that should print out like this (without formatting):
array(3) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
},
[1]=> array(5) {
[0]=> int(0)
[1]=> string(10) "Workshop 1"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(15) "Death Note (Live)"
},
[2]=> array(5) {
[0]=> int(0)
[1]=> string(7) "Video 6"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(26) "Takeuchi Fan Panel"
}
}
Notice from the above code that the inner array() length is always 5.
Here is my code below:
$loopsArray = array();
$data=array();
// graphing info come in here.
foreach ($events as $key => $event) {
$el=$event['event_location'] ;
$eln=$event['event_locationName'];
$ed=$event['event_date'];
$es=$event['event_start'];
$ee=$event['event_end'];
$en=$event['event_name'];
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
}
var_dump($data);
Here the print out
array(27) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
}
[1]=> array(10) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
[5]=> int(13)
[6]=> string(11) "Autograph 1"
[7]=> string(18) "2012-06-2419:00:00"
[8]=> string(18) "2012-06-2422:00:00"
[9]=> string(17) "Parents and Anime"
}
//... continues
}
Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).
It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it.
Can you look over my loop and let me know how to fix it?
I have to use this multidimensional array for this code to work in JpGraph.
TIP : write $loopsArray = array(); inside foreach
better approach
instead of
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
try this
$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;