Contents of array incorrect - php

I currently have this SQL statement:
SELECT p.*,k.name FROM posts AS p
LEFT JOIN `post-keywords` AS pk ON p.id = pk.id_post
LEFT JOIN keywords AS k ON pk.id_keyword = p.id
ORDER BY p.id DESC LIMIT :num,:count
however, it doesn't work as expected. This is what the server returns:
array(7) {
[0]=>
array(5) {
["id"]=>
string(1) "2"
["title"]=>
string(28) "Everything about Internet"
["text"]=>
string(41) "Article itself"
["date"]=>
string(1) "0"
["name"]=>
NULL
}
[1]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag1"
}
[2]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag2"
}
[3]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag3"
}
[4]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
string(7) "tag4"
}
[5]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
NULL
}
[6]=>
array(5) {
["id"]=>
string(1) "1"
["title"]=>
string(16) "Internet"
["text"]=>
string(32) "Text text text"
["date"]=>
string(10) "1371186330"
["name"]=>
NULL
}
}
How to retrieve an array from the server that contains multiple name fields:
array (5) {
["id"] =>
   string (1) "1"
   ["title"] =>
   string (16) "Internet"
   ["text"] =>
   string (32) "text text text"
   ["date"] =>
   string (10) "1371186330"
   ["name"] =>
   string (10) "word1"
   ["name"] =>
   string (10) "word2"
   ["name"] =>
   string (10) "word3"
}

Currently your query is 'overwriting' fields that have multiple entries with the same name. For example, you are getting p.name and k.name. You query doesn't make a difference, so it gets overwritten to (I presume) k.name.
To make sure you get them both, write something like: SELECT p.name AS p_name, k.name AS k_name

Related

Removing duplicate data in a multi dimension array (associative array)

I have three keys pointing to value of type array, in those array values I have duplicate values. How can I remove them?
array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "4"
["title"]=>
string(9) "Blink-182"
["location"]=>
string(25) "Motorpoint Arena, Cardiff"
["event_date"]=>
string(10) "2017-07-03"
["event_time"]=>
string(8) "18:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.478937400000010"
["geo_long"]=>
string(17) "-3.17172289999996"
["tags"]=>
string(14) "rock,metal"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
["id"]=>
string(1) "1"
["title"]=>
string(15) "Teleman Tickets"
["location"]=>
string(20) "Concorde 2, Brighton"
["event_date"]=>
string(10) "2017-02-20"
["event_time"]=>
string(8) "20:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "50.817321799999990"
["geo_long"]=>
string(17) "-0.12304610000001"
["tags"]=>
string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
["id"]=>
string(1) "6"
["title"]=>
string(6) "Sum 41"
["location"]=>
string(23) "O2 Academy Leeds, Leeds"
["event_date"]=>
string(10) "2017-02-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.802188400000000"
["geo_long"]=>
string(17) "-1.54713770000001"
["tags"]=>
string(21) "alternative,indie"
}
[2]=>
array(9) {
["id"]=>
string(1) "8"
["title"]=>
string(5) "Ghost"
["location"]=>
string(29) "O2 Forum Kentish Town, London"
["event_date"]=>
string(10) "2017-03-26"
["event_time"]=>
string(8) "19:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "51.552197000000010"
["geo_long"]=>
string(17) "-0.14196900000002"
["tags"]=>
string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
["id"]=>
string(2) "10"
["title"]=>
string(11) "Courteeners"
["location"]=>
string(39) "Emirates Old Trafford, Lancashire C.C.C"
["event_date"]=>
string(10) "2017-05-27"
["event_time"]=>
string(8) "16:00:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "53.456428000000000"
["geo_long"]=>
string(17) "-2.28679699999998"
["tags"]=>
string(30) "rock,alternative,indie"
}
}
["sax"]=>
array(1) {
[0]=>
array(9) {
["id"]=>
string(1) "3"
["title"]=>
string(9) "Take That"
["location"]=>
string(22) "The SSE Hydro, Glasgow"
["event_date"]=>
string(10) "2017-05-11"
["event_time"]=>
string(8) "18:30:00"
["url"]=>
string(1) "0"
["geo_lat"]=>
string(18) "55.860156000000000"
["geo_long"]=>
string(17) "-4.28525800000000"
["tags"]=>
string(24) "pop,boy-band,sax"
}
}
}
I'm going to assume that if two bands have the same 'id' then they are identical
$unique_bands = array();
// if the master array is called bands
foreach($bands as $genre){
foreach($genre as $band){
$unqiue_bands[$band['id']] = $band;
}
}
Maybe put $unique_bands = array_values(array_filter($unique_bands)); at the end to reindex the array
You have a solution on the official documentation... A person made a simple function to do exactly what you want.
Example array:
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
?>
Function:
<?php
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
?>
You will have to fix it to work your for array dimension.

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;

codeigniter: inserting value from array 1 to array 2?

i have an array like this
array 1
array(3) {
[0]=> string(2) "47"
[1]=> string(2) "48"
[2]=> string(2) "49"
}
i have plan to giving array with name, the array name is number
array 2
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000" }
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000" }
}
I want to inserting array 1 into array 2 which same array key
I want to insert array 1 data into array 2 accordance with the key array . so i I was expecting to be joined both of arrays and became joined array like this
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000"
["number"]=> string(2) "47" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000"
["number"]=> string(2) "48"}
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000"
["number"]=> string(2) "49"}
}
is there any way to create or manipulate into an array like that ? my array is dynamically so number of array can be changed anytime.
I would greatly appreciate it if you could help me
Read up on the basic language control structures and foreach in particular.
foreach ($array2 as $index => $object) {
if (isset($array1[$index])) {
$object->number = $array1[$index];
}
}
Outcome:
array(3) {
[0]=>
object(stdClass)#1 (3) {
["address"]=>
string(9) "Address 1"
["price"]=>
string(6) "120000"
["number"]=>
string(2) "47"
}
[1]=>
object(stdClass)#2 (3) {
["address"]=>
string(9) "Address 2"
["price"]=>
string(6) "150000"
["number"]=>
string(2) "48"
}
[2]=>
object(stdClass)#3 (3) {
["address"]=>
string(9) "Address 3"
["price"]=>
string(6) "180000"
["number"]=>
string(2) "49"
}
}
Here is a Codepad demo

List data in alphabetical order in concerned sections

I have an array of data that contains the alphabetically sorted data, but in view I have different sections. Each alphabet represents each section.
I need to list the data in such order that section with A must contain all the names starting with A, section B must contain the names sarting with B and so on.
My work out
I have made an array that gives me the data separates the data with there starting alphabets
foreach ($data['stores_cat_data'] as $store ) {
$store_title = $store['title']."<br />";
$curr = current(str_split($store_title));
if(!preg_match("/^[a-zA-Z]$/", $curr))
{
$storeArray['0-9'][$store_title] = $store;
}
else{
$storeArray[$curr][$store_title] = $store;
}
}
EDITED
my output is
array(46) {
["0-9"]=>
array(21) {
["1 800 Lighting
"]=>
array(7) {
["title"]=>
string(14) "1 800 Lighting"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(49) "http://www.gopjn.com/t/S0BHSUVJQERDREpHSUBHS0xKRg"
["id"]=>
string(3) "539"
["logo"]=>
string(14) "1389213829.gif"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["1-800 CONTACTS
"]=>
array(7) {
["title"]=>
string(14) "1-800 CONTACTS"
["cCommisions_percentage"]=>
string(73) "2.4%(Repeat Customer Repeat Sale) and 7.2%(New Customer First Time Sale) "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.274994&fot=9999&foc=1"
["id"]=>
string(3) "598"
["logo"]=>
string(14) "1389289817.jpg"
["publisher_id"]=>
string(1) "4"
["subid"]=>
string(13) "&fobs={subid}"
}
["1-800-Bakery
"]=>
array(7) {
["title"]=>
string(13) "1-800-Bakery "
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=266793&type=3&subid=0"
["id"]=>
string(2) "22"
["logo"]=>
string(14) "1387403703.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-BASKETS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-BASKETS.COM"
["cCommisions_percentage"]=>
string(5) "8.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.216994&fot=9999&foc=1"
["id"]=>
string(3) "599"
["logo"]=>
string(14) "1389289896.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-FLOWERS.COM
"]=>
array(7) {
["title"]=>
string(17) "1-800-FLOWERS.COM"
["cCommisions_percentage"]=>
string(6) "10.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.225130&fot=9999&foc=1"
["id"]=>
string(4) "1659"
["logo"]=>
string(14) "1390516089.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["1-800-PetMeds
"]=>
array(7) {
["title"]=>
string(13) "1-800-PetMeds"
["cCommisions_percentage"]=>
string(42) "12.00%-16.00% commission on selected goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=56753&type=3&subid=0"
["id"]=>
string(2) "24"
["logo"]=>
string(14) "1387403850.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["100 Percent Pure
"]=>
array(7) {
["title"]=>
string(16) "100 Percent Pure"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=270135&type=3&subid=0"
["id"]=>
string(2) "25"
["logo"]=>
string(14) "1387404086.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Inkjets.com
"]=>
array(7) {
["title"]=>
string(15) "123Inkjets.com "
["cCommisions_percentage"]=>
string(41) "25.6% and 4%(OEM and Office Supply items)"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.217798&fot=9999&foc=1"
["id"]=>
string(3) "596"
["logo"]=>
string(14) "1389289585.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["123Print
"]=>
array(7) {
["title"]=>
string(8) "123Print"
["cCommisions_percentage"]=>
string(5) "6.40%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.276533&fot=9999&foc=1"
["id"]=>
string(3) "597"
["logo"]=>
string(14) "1389289736.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["191 Unlimited
"]=>
array(7) {
["title"]=>
string(13) "191 Unlimited"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BLS0ZGQERDREpHSUBKS0ZGSg"
["id"]=>
string(3) "540"
["logo"]=>
string(14) "1389214452.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["2020ave.com
"]=>
array(7) {
["title"]=>
string(11) "2020ave.com"
["cCommisions_percentage"]=>
string(6) "10.00%"
["url"]=>
string(50) "http://www.pjtra.com/t/TEFNSkZIQUVERUtISkFFREZHSks"
["id"]=>
string(3) "542"
["logo"]=>
string(14) "1389214732.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["24 Hour Fitness
"]=>
array(7) {
["title"]=>
string(15) "24 Hour Fitness"
["cCommisions_percentage"]=>
string(8) "4% to 8%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.256270&fot=9999&foc=1"
["id"]=>
string(3) "601"
["logo"]=>
string(14) "1389290288.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["2bstores.com
"]=>
array(7) {
["title"]=>
string(12) "2bstores.com"
["cCommisions_percentage"]=>
string(5) "5.00%"
["url"]=>
string(51) "http://www.pntrac.com/t/TEFNSUZMQUVERUtISkFFRERKR0s"
["id"]=>
string(3) "543"
["logo"]=>
string(14) "1389214901.png"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["360training
"]=>
array(7) {
["title"]=>
string(12) "360training "
["cCommisions_percentage"]=>
string(6) "12.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.737181&fot=9999&foc=1"
["id"]=>
string(3) "602"
["logo"]=>
string(14) "1389290350.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 All Memory
"]=>
array(7) {
["title"]=>
string(12) "4 All Memory"
["cCommisions_percentage"]=>
string(29) "8.00% commission on all goods"
["url"]=>
string(85) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=99169&type=3&subid=0"
["id"]=>
string(2) "28"
["logo"]=>
string(14) "1387404383.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Drive Hardware
"]=>
array(7) {
["title"]=>
string(22) "4 Wheel Drive Hardware"
["cCommisions_percentage"]=>
string(5) "5.60%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730591&fot=9999&foc=1"
["id"]=>
string(3) "603"
["logo"]=>
string(14) "1389290418.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4 Wheel Parts
"]=>
array(7) {
["title"]=>
string(14) "4 Wheel Parts "
["cCommisions_percentage"]=>
string(5) "4.80%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.730526&fot=9999&foc=1"
["id"]=>
string(3) "604"
["logo"]=>
string(14) "1389290467.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["4imprint
"]=>
array(7) {
["title"]=>
string(8) "4imprint"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.161896&fot=9999&foc=1"
["id"]=>
string(3) "605"
["logo"]=>
string(14) "1389290519.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["525 America
"]=>
array(7) {
["title"]=>
string(11) "525 America"
["cCommisions_percentage"]=>
string(5) "6.00%"
["url"]=>
string(49) "http://www.pntra.com/t/S0BLSkZKQERDREpHSUBKQ0dFRw"
["id"]=>
string(3) "544"
["logo"]=>
string(14) "1389215113.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["60 Minute Payday
"]=>
array(7) {
["title"]=>
string(17) "60 Minute Payday "
["cCommisions_percentage"]=>
string(4) "$48 "
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.164179&fot=9999&foc=1"
["id"]=>
string(3) "606"
["logo"]=>
string(14) "1389290591.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["7 For All Mankind
"]=>
array(7) {
["title"]=>
string(17) "7 For All Mankind"
["cCommisions_percentage"]=>
string(29) "4.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256004&type=3&subid=0"
["id"]=>
string(2) "29"
["logo"]=>
string(14) "1387404461.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
}
["A"]=>
array(83) {
["A.N.S.I
"]=>
array(7) {
["title"]=>
string(7) "A.N.S.I"
["cCommisions_percentage"]=>
string(30) "10.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=208750&type=3&subid=0"
["id"]=>
string(2) "48"
["logo"]=>
string(14) "1387408300.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABC Distributing LLC
"]=>
array(7) {
["title"]=>
string(20) "ABC Distributing LLC"
["cCommisions_percentage"]=>
string(69) "4.00% commission on all goods; [2.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=238767&type=3&subid=0"
["id"]=>
string(2) "30"
["logo"]=>
string(14) "1387404636.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["ABCmouse.com
"]=>
array(7) {
["title"]=>
string(12) "ABCmouse.com"
["cCommisions_percentage"]=>
string(27) "$4.00 flat fee on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=256295&type=3&subid=0"
["id"]=>
string(2) "31"
["logo"]=>
string(14) "1387405955.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abe's of Maine
"]=>
array(7) {
["title"]=>
string(15) "Abe's of Maine "
["cCommisions_percentage"]=>
string(5) "4.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.290656&fot=9999&foc=1"
["id"]=>
string(3) "607"
["logo"]=>
string(14) "1389290715.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abhair
"]=>
array(7) {
["title"]=>
string(6) "Abhair"
["cCommisions_percentage"]=>
string(37) "12.00%-17.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=280480&type=3&subid=0"
["id"]=>
string(2) "33"
["logo"]=>
string(14) "1387406425.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["About Airport Parking
"]=>
array(7) {
["title"]=>
string(21) "About Airport Parking"
["cCommisions_percentage"]=>
string(6) "50.00%"
["url"]=>
string(49) "http://www.pntrs.com/t/S0BMRUVFQERDREpHSUBMRUpHSA"
["id"]=>
string(3) "560"
["logo"]=>
string(14) "1389224403.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Absolute LoJack
"]=>
array(7) {
["title"]=>
string(16) "Absolute LoJack "
["cCommisions_percentage"]=>
string(73) "$1.00 flat fee on selected goods; [15.00% commission on selected goods]; "
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=274953&type=3&subid=0"
["id"]=>
string(2) "34"
["logo"]=>
string(14) "1387406520.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["Abt.com
"]=>
array(7) {
["title"]=>
string(7) "Abt.com"
["cCommisions_percentage"]=>
string(5) "3.00%"
["url"]=>
string(50) "http://www.pjatr.com/t/TEFNSUtIQUVERUtISkFFREVIREc"
["id"]=>
string(3) "547"
["logo"]=>
string(14) "1389215949.jpg"
["publisher_id"]=>
string(1) "3"
["subid"]=>
string(12) "&sid={subid}"
}
["Accessorize
"]=>
array(7) {
["title"]=>
string(11) "Accessorize"
["cCommisions_percentage"]=>
string(29) "5.00% commission on all goods"
["url"]=>
string(86) "http://click.linksynergy.com/fs-bin/click?id=N7Y2q6vTwWg&offerid=215113&type=3&subid=0"
["id"]=>
string(4) "1660"
["logo"]=>
string(14) "1390518973.jpg"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
["AccessoryGeeks.com
"]=>
array(7) {
["title"]=>
string(19) "AccessoryGeeks.com "
["cCommisions_percentage"]=>
string(6) "13.00%"
["url"]=>
string(68) "http://track.flexlinks.com/a.ashx?foid=1049250.227987&fot=9999&foc=1"
["id"]=>
string(3) "608"
["logo"]=>
string(14) "1389291008.gif"
["publisher_id"]=>
string(1) "1"
["subid"]=>
string(11) "&u1={subid}"
}
This should do it:
$storeArray = array();
foreach ($data['stores_cat_data'] as $store) {
$title = $store['title'];
$first_char = substr($title, 0, 1);
if (preg_match("/^[0-9]$/", $first_char)) {
$key = "0-9";
} else {
$key = strtoupper($first_char);
}
if (!isset($storeArray[$key])) {
$storeArray[$key] = array();
}
$storeArray[$key][$title] = $store;
}
var_dump($storeArray);
Your code looks almost correct. The way you determine $curr is a little strange and you have to account for uppercase/lowercase. Also, to avoid array warnings make sure you initialize each letter of the alphabet, as you go.
You might also want to remove all non-alphanumeric characters, like " and - that might mess up the detection. A title of "Best" book shop will be put under 0-9 where B would be better.
A regexp like: preg_replace("/[^a-zA-Z0-9]/", "", $str) should do it.

How should I sort this array?

I'm currently trying to implement a messaging functionality in my system. My goal right now is having a jQuery popup dialog which displays the name of users that have messaged the other person and then clicking on that person's name will show the new messages. I've done the popup dialog bit so my next step is getting and sorting the messages from database.
What I've done now is retrieve the new messages from database. I am unsure of how to proceed as my array of unread messages contains the element of the sender's id more than once. I do not want to loop through my unread messages and then get the same sender id again and again and as such retrieve their details from database again and again.
Here's an example of my array:
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[2]=>
array(7) {
["id"]=>
string(2) "10"
["useridfrom"]=>
string(3) "138"
["useridto"]=>
string(3) "139"
["message"]=>
string(54) "Hi Emma thank you for submitting your homework - Jenny"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1309122193"
["messagetype"]=>
string(6) "direct"
}
}
This is what I've done in PHP so far:
$m = new Messaging();
$json = $m->getUnreadMessages($uid);
/** sort messages by useridfrom **/
uasort($json, array($m, 'messageCompare'));
foreach($json as $j)
{
//do sorting of messages here ??
}
echo json_encode($json);
exit;
I guess ideally, I would want to create a new array out of this where the key is useridfrom (sender) and then their messages and then associated with each message the timecreated. What is the best way to do this?
Thanks in advance.
EDIT
As you can see from the array example there are duplicate useridfrom elements where useridfrom = 241 twice. This means that the user 241 has sent two messages, if there were 3 useridfrom 241 elements then that means he/she sent 3 messages. My goal is two group the useridfrom together as keys in an array so there are no duplicates then associated with that key we have all their messages (message) and the time the message was created (timecreated).
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
So from this example, it should lead to an array of something like what mariomario has suggested:
array{
['241'] => array(
[0] => array(time=>'time1', message => "hello i am 241"),
[1] => array(time=>'time2', message => "nice to meet you"),
),
['330'] => array(
[0] => array(time=>'time1', message => "hello i am 330"),
[1] => array(time=>'time2', message => "goodbye"),
)
}
EDITED:
After a little pondering I'd do this:
$data = array();
foreach ($json as $j) {
if (!array_key_exists($j['useridfrom'], $data)) {
$data[$j['useridfrom']] = array();
}
if (!array_key_exists($j['useridto'], $data[$j['useridfrom']])) {
$data[$j['useridfrom']][$j['useridto']] = array();
}
$data[$j['useridfrom']][$j['useridto']][] = array(
'message' => $j['message'],
'timestamp' => $j['timecreated']
);
}
Keep your data ordered like so ORDER BY timecreated DESC and that concats everything like you want ordered after the time.
This should net an array like so:
'sender1' => (
'recipient1' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
),
(
'message' => 'ANOTHER MESSAGE HERE',
'timecreated' => 'ANOTHER TIME HERE'
)
),
'recipient2' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
)
)
),
'sender2' => (
)
wouldn't you do something like this
$newArray = array()
foreach($json as $j)
{
$array[$j['useridfrom']][] = array('time'=>$j['timecreated'],
'message'=>$j[message]);
}
they you will have a array like this:
array{
['user1'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
),
['user2'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
)
}
but maybe I am understanding you not correct.
Use array_multisort in PHP:
http://php.net/manual/en/function.array-multisort.php

Categories