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);
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"
}
Below is example of my array
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "2"
[2]=>
string(4) "1"
[3]=>
string(4) "3"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
Now I am sorrting using array_multisort($arr["value"],SORT_NUMERIC, SORT_DESC); , which results in below output.
["value"]=>
array(16) {
[0]=>
string(5) "4"
[1]=>
string(4) "3"
[2]=>
string(4) "2"
[3]=>
string(4) "1"
}
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "two-id"
[2]=>
string(4) "one-id"
[3]=>
string(4) "three-id"
}
I want $arr["id"] to be sorted based on same sorting order of $arr["value"] like below
["id"]=>
array(16) {
[0]=>
string(4) "four-id"
[1]=>
string(4) "three-id"
[2]=>
string(4) "two-id"
[3]=>
string(4) "one-id"
}
You can use krsort
$array_1 = Array(4,2,1,3);
$array_2 = Array('four-id','two-id','one-id','three-id');
foreach ($array_1 as $key => $value) {
$array_merged[$value] = $array_2[$key];
}
krsort($array_merged);
foreach ($array_merged as $key => $value) {
$new_array_1[] = $key;
$new_array_2[] = $value;
}
echo '<pre>'.print_r( $new_array_1, true ).print_r( $new_array_2, true ).'</pre>';
I'm looking for a solution to create an associative array from an flat array data in foreach loop:
What i have is a csv/xls file that has header on first row and data in next rows.
Row1: header
Row2,3,4,5: data
the array looks:
array(3) {
[0]=>
array(7) {
[0]=>
string(3) "country"
[1]=>
string(7) "state"
[2]=>
string(3) "city"
[3]=>
string(5) "name"
[4]=>
string(4) "address"
[5]=>
string(6) "gender"
[6]=>
string(6) "status"
}
[1]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Corrientes"
[2]=>
string(12) "Corrientes"
[3]=>
string(12) "Jorge"
[4]=>
string(12) "Avenida Avellaneda 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
[2]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Chaco"
[2]=>
string(12) "Resistencia"
[3]=>
string(12) "Mariano"
[4]=>
string(12) "Avenida Peron 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
}
The result i need to get at the end is:
array(2) {
[0]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Corrientes"
['city']=>
string(12) "Corrientes"
['name']=>
string(12) "Jorge"
['address']=>
string(12) "Avenida Avellaneda 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
[1]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Chaco"
['city']=>
string(12) "Resistencia"
['name']=>
string(12) "Mariano"
['address']=>
string(12) "Avenida Peron 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
}
$array = $your_flat_array;
for ($i = 1; $i < count($array); $i++) {
$new_array[$i-1] = [];
foreach ($array[$i] as $key => $value) {
$new_array[$i-1][$array[0][$key]] = $value;
}
}
print_r($new_array);
create a multidimensional array from an flat array
You already have a multidimensional array, because you got arrays in an array.
What you can do in this specific case is to use array_splice() in combination with array_combine().
Try this:
$oldArray = array(
array( "country", "state", "city", "name" ),
array( "Argentina", "Corrientes", "Corrientes", "Jorge" ),
array( "Argentina", "Chaco", "Resistencia", "Mariano" )
);
$newArray = array_splice( $oldArray, 1 );
foreach( $newArray as $index => $array ) {
$newArray[$index] = array_combine( $oldArray[0], $array );
}
echo "<pre>";
var_dump( $newArray );
OUTPUT:
array(2) {
[0]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(10) "Corrientes"
["city"]=>
string(10) "Corrientes"
["name"]=>
string(5) "Jorge"
}
[1]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(5) "Chaco"
["city"]=>
string(11) "Resistencia"
["name"]=>
string(7) "Mariano"
}
}
All you have to do is remove the first item (header row) from your array:
array_splice($yourArray, 0, 1);
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;
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;