Fixing multidimensional array - php

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;

Related

How to rotate element of an array in top to bottom fashion in every iteration

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"
}

Count results of a multidimensional array

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');

Merge array where duplicate in multidimensional array

I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);

How to pair those database rows?

so, I have this database, I need to know how many profile.stateX-profile.licenseX have value and also to pair them. I am able to pull them in a array but I can't find the propper php code to get what I need.
Again, what I need to know is this:
1: how many profile.state1/2/3/4/5-profile.license1/2/3/4/5 pairs I have with value
2:output the pairs
This is the array returned form query:
<pre>
array(16) {
[0]=>
array(2) {
[0]=>
string(15) "profile.address"
[1]=>
string(10) "Bld. Indep"
}
[1]=>
array(2) {
[0]=>
string(21) "profile.certification"
[1]=>
string(7) "cert112"
}
[2]=>
array(2) {
[0]=>
string(16) "profile.license1"
[1]=>
string(5) "12345"
}
[3]=>
array(2) {
[0]=>
string(16) "profile.license2"
[1]=>
string(0) ""
}
[4]=>
array(2) {
[0]=>
string(16) "profile.license3"
[1]=>
string(0) ""
}
[5]=>
array(2) {
[0]=>
string(16) "profile.license4"
[1]=>
string(0) ""
}
[6]=>
array(2) {
[0]=>
string(16) "profile.license5"
[1]=>
string(0) ""
}
[7]=>
array(2) {
[0]=>
string(21) "profile.licensenumber"
[1]=>
string(7) "lice112"
}
[8]=>
array(2) {
[0]=>
string(14) "profile.school"
[1]=>
string(5) "nr, 2"
}
[9]=>
array(2) {
[0]=>
string(14) "profile.state1"
[1]=>
string(4) "Ohio"
}
[10]=>
array(2) {
[0]=>
string(14) "profile.state2"
[1]=>
string(0) ""
}
[11]=>
array(2) {
[0]=>
string(14) "profile.state3"
[1]=>
string(0) ""
}
[12]=>
array(2) {
[0]=>
string(14) "profile.state4"
[1]=>
string(0) ""
}
[13]=>
array(2) {
[0]=>
string(14) "profile.state5"
[1]=>
string(0) ""
}
[14]=>
array(2) {
[0]=>
string(18) "profile.user_state"
[1]=>
string(8) "Roumania"
}
[15]=>
array(2) {
[0]=>
string(11) "profile.zip"
[1]=>
string(3) "123"
}
}
</pre>
I hope this makes sense.
Assuming the database is MySQL:
SELECT t1.profile_key, t1.profile_value, t2.profile_key, t2.profile_value
FROM profile AS t1
JOIN profile AS t2 ON t2.profile_key = CONCAT('profile.license', SUBSTR(t1.profile_key, 14))
WHERE t1.profile_key LIKE 'profile.state%' AND t1.profile_value != ''
AND t2.profile_key LIKE 'profile.license%' AND t2.profile_value != ''
SUBSTR(t1.profile_key, 14) gets the number after profile.state in profile_key column. Then we use CONCAT() to append that to profile.license to get the profile_key for the paired row.

Create an array from another array result - php [duplicate]

This question already has answers here:
MYSQL SUM GROUP BY
(2 answers)
Closed 7 years ago.
I have 2 kind of classes: math and physics.
In the loop , count(lessonclasses) is equal to 2 then 3.
How can I proceed for my expected output ,
My code is :
$result[]=array();
foreach($lessons as $nameLesson)
{
$lessonclasses=$Board->GetLessonsClass($iduser,$nameLesson);
$tabPrice=$redevance->detailPriceForLesson($nameLesson)
$price=$tabPrice[0];
$frequency=$tabPrice[1];
//echo "nb of class per lesson: ".count($lessonclasses);
for($i=0;$i<count($lessonclasses);$i++)
{
//var_dump($lessonclasses);
$name=$lessonclasses[$i][0];
$amount=$lessonclasses[$i][1];
$timelessonstart=$lessonclasses[$i][2];
$timelessonend=$lessonclasses[$i][3];
$result[$i][]=array($name,$price."€/".$frequency,$timelessonstart,$timelessonend,$amount);
//$arrayOptions[$nbLessons][]=$options; //for keep values
//$nbLessons=$nbLessons+1;
}
}
var_dump($result);
I have an array like :
array(3) {
[0]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "10.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "10.00"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "6.00"
[2]=> string(10) "2015-01-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "20.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "100.0"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "16.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[2]=> array(1) {
[0]=> array(5) {
[0]=> string(15) "Maths" [1]=> string(11) "11.00" [2]=> string(10) "2015-05-17" [3]=> string(1) "-" [4]=> string(5) "20.00" } } }
My expected output is :
array(3) {
[0]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "10.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "10.00"
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "20.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "100.0"
}
[2]=> array(5) {
[0]=> string(31) "math"
[1]=> string(10) "11.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "6.00"
[2]=> string(10) "2015-01-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "16.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
} }
SELECT name,sum(qty) FROM tableName group by name;
Simply
SELECT name, SUM(qty) AS total_amount FROM table GROUP BY name
you need to use sum
SELECT name,sum(qty) FROM table_name group by name;
Its working for you..
SELECT name,sum(qty) FROM table_name GROUP BY name;

Categories