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.
Related
I would like to get subcategory and main category from this arrays which I give below. First I want to get category and then would like to show subcategory under main category.
I have tried this PHP code but unfortunately my code doesn't work which I want. Can you suggest me another code please?
<?php
$resp = $c->execute($req);
var_dump($c->execute($req));
output:
object(SimpleXMLElement)#6 (2) {
["resp_result"]=>
object(SimpleXMLElement)#3 (3) {
["resp_code"]=>
string(3) "200"
["resp_msg"]=>
string(13) "Call succeeds"
["result"]=>
object(SimpleXMLElement)#4 (2) {
["categories"]=>
object(SimpleXMLElement)#7 (1) {
["category"]=>
array(499) {
[0]=>
object(SimpleXMLElement)#8 (2) {
["category_id"]=>
string(1) "2"
["category_name"]=>
string(4) "Food"
}
[1]=>
object(SimpleXMLElement)#9 (3) {
["category_id"]=>
string(9) "201379402"
["category_name"]=>
string(4) "Meat"
["parent_category_id"]=>
string(1) "2"
}
[2]=>
object(SimpleXMLElement)#10 (3) {
["category_id"]=>
string(9) "201379801"
["category_name"]=>
string(18) "Fruits and Berries"
["parent_category_id"]=>
string(1) "2"
}
[3]=>
object(SimpleXMLElement)#11 (3) {
["category_id"]=>
string(9) "201378002"
["category_name"]=>
string(18) "Bread and Pastries"
["parent_category_id"]=>
string(1) "2"
}
[4]=>
object(SimpleXMLElement)#12 (3) {
["category_id"]=>
string(9) "201248407"
["category_name"]=>
string(11) "Canned Food"
["parent_category_id"]=>
string(1) "2"
}
[5]=>
object(SimpleXMLElement)#13 (3) {
["category_id"]=>
string(9) "201378103"
["category_name"]=>
string(8) "Sausages"
["parent_category_id"]=>
string(1) "2"
}
[6]=>
object(SimpleXMLElement)#14 (3) {
["category_id"]=>
string(9) "200183144"
["category_name"]=>
string(12) "Canned Food1"
["parent_category_id"]=>
string(1) "2"
}
[7]=>
object(SimpleXMLElement)#15 (3) {
["category_id"]=>
string(9) "201345805"
["category_name"]=>
string(19) "Alcoholic Beverages"
["parent_category_id"]=>
string(1) "2"
}
[8]=>
object(SimpleXMLElement)#16 (3) {
["category_id"]=>
string(9) "201376903"
["category_name"]=>
string(17) "Fish and Sea Food"
["parent_category_id"]=>
string(1) "2"
}
[9]=>
object(SimpleXMLElement)#17 (3) {
["category_id"]=>
string(9) "200180142"
["category_name"]=>
string(7) "Grocery"
["parent_category_id"]=>
string(1) "2"
}
[10]=>
object(SimpleXMLElement)#18 (3) {
["category_id"]=>
string(9) "200001321"
["category_name"]=>
string(13) "Nut & Kernel1"
["parent_category_id"]=>
string(1) "2"
}
[11]=>
object(SimpleXMLElement)#19 (3) {
["category_id"]=>
string(9) "200001317"
["category_name"]=>
string(12) "Dried Fruit1"
["parent_category_id"]=>
string(1) "2"
}
[12]=>
object(SimpleXMLElement)#20 (3) {
["category_id"]=>
string(9) "200001287"
["category_name"]=>
string(15) "Grain Products1"
["parent_category_id"]=>
string(1) "2"
}
[13]=>
object(SimpleXMLElement)#21 (3) {
["category_id"]=>
string(9) "200001238"
["category_name"]=>
string(7) "Coffee1"
["parent_category_id"]=>
string(1) "2"
}
[14]=>
object(SimpleXMLElement)#22 (3) {
["category_id"]=>
string(9) "201376204"
["category_name"]=>
string(13) "Milk and Eggs"
["parent_category_id"]=>
string(1) "2"
}
[15]=>
object(SimpleXMLElement)#23 (3) {
["category_id"]=>
string(9) "201376702"
["category_name"]=>
string(10) "Ready Meal"
["parent_category_id"]=>
string(1) "2"
}
[16]=>
object(SimpleXMLElement)#24 (3) {
["category_id"]=>
string(9) "201249506"
["category_name"]=>
string(21) "Water/ Juices/ Drinks"
["parent_category_id"]=>
string(1) "2"
}
[17]=>
object(SimpleXMLElement)#25 (3) {
["category_id"]=>
string(9) "201248805"
["category_name"]=>
string(31) "Dried Goods / Local Specialties"
["parent_category_id"]=>
string(1) "2"
}
[18]=>
object(SimpleXMLElement)#26 (3) {
["category_id"]=>
string(9) "201248804"
["category_name"]=>
string(6) "Coffee"
["parent_category_id"]=>
string(1) "2"
}
[19]=>
object(SimpleXMLElement)#27 (3) {
["category_id"]=>
string(9) "200003573"
["category_name"]=>
string(32) "Dried Goods / Local Specialties1"
["parent_category_id"]=>
string(1) "2"
}
[20]=>
object(SimpleXMLElement)#28 (3) {
["category_id"]=>
string(9) "201379901"
["category_name"]=>
string(6) "Cheese"
["parent_category_id"]=>
string(1) "2"
}
[21]=>
object(SimpleXMLElement)#29 (3) {
["category_id"]=>
string(9) "201254404"
["category_name"]=>
string(11) "Dried Fruit"
["parent_category_id"]=>
string(1) "2"
}
[22]=>
object(SimpleXMLElement)#30 (3) {
["category_id"]=>
string(9) "201254306"
["category_name"]=>
string(14) "Grain Products"
["parent_category_id"]=>
string(1) "2"
}
[23]=>
object(SimpleXMLElement)#31 (3) {
["category_id"]=>
string(9) "201253905"
["category_name"]=>
string(12) "Nut & Kernel"
["parent_category_id"]=>
string(1) "2"
}
[24]=>
object(SimpleXMLElement)#32 (3) {
["category_id"]=>
string(9) "201374104"
["category_name"]=>
string(15) "Frozen Products"
["parent_category_id"]=>
string(1) "2"
}
[25]=>
object(SimpleXMLElement)#33 (3) {
["category_id"]=>
string(9) "100007269"
["category_name"]=>
string(3) "Tea"
["parent_category_id"]=>
string(1) "2"
}
[26]=>
object(SimpleXMLElement)#34 (3) {
["category_id"]=>
string(9) "201373004"
["category_name"]=>
string(21) "Vegetables and Greens"
["parent_category_id"]=>
string(1) "2"
}
[27]=>
object(SimpleXMLElement)#35 (2) {
["category_id"]=>
string(1) "3"
["category_name"]=>
string(21) "Apparel & Accessories"
}
[28]=>
object(SimpleXMLElement)#36 (3) {
["category_id"]=>
string(9) "200003274"
["category_name"]=>
string(11) "Sportswears"
["parent_category_id"]=>
string(1) "3"
}
[29]=>
object(SimpleXMLElement)#37 (2) {
["category_id"]=>
string(1) "6"
["category_name"]=>
string(15) "Home Appliances"
}
[30]=>
object(SimpleXMLElement)#38 (3) {
["category_id"]=>
string(3) "628"
["category_name"]=>
string(21) "Other Home Appliances"
["parent_category_id"]=>
string(1) "6"
}
}
}
["total_result_count"]=>
string(3) "499"
}
}
["request_id"]=>
string(13) "15r9xetbc5h1i"
}
I mean:
Main category -> Food
Subcategory-> Meat
-> Fruits and Berries
Main category -> Apparel & Accessories
Subcategory -> Sportswears
Here is my foreach code:
foreach($CategoryResp->resp_result->result->categories->category as $cat){
if(isset($cat->parent_category_id->string) == 'NUL'){
//mother category
echo "<b>".$cat->category_id. ' '.$cat->category_name."</b><br />";
if(isset($cat->parent_category_id)){
//subcategories
echo $cat->category_id. ' '.$cat->category_name."<br />";
}
}
}
I've google sheet with this data:
I'm pulling it from google drive with simple code:
$ranges = [
'Price_1'
];
$params = array(
'ranges' => $ranges
);
$response = $service->spreadsheets_values->batchGet($spreadsheetId, $params);
The response is a complicated object:
object(Google_Service_Sheets_BatchGetValuesResponse)#58 (8) { ["collection_key":protected]=> string(11) "valueRanges" ["spreadsheetId"]=> string(44) "1BQpKLuvlaVeGqIqoGUOynF9IZ5C3zK6gagZiRJO1UEU" ["valueRangesType":protected]=> string(32) "Google_Service_Sheets_ValueRange" ["valueRangesDataType":protected]=> string(5) "array" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["valueRanges"]=> array(1) { [0]=> object(Google_Service_Sheets_ValueRange)#67 (7) { ["collection_key":protected]=> string(6) "values" ["majorDimension"]=> string(4) "ROWS" ["range"]=> string(14) "Price_1!E2:E74" ["values"]=> array(65) { [0]=> array(1) { [0]=> string(2) "28" } [1]=> array(1) { [0]=> string(2) "14" } [2]=> array(1) { [0]=> string(1) "0" } [3]=> array(0) { } [4]=> array(1) { [0]=> string(2) "55" } [5]=> array(1) { [0]=> string(2) "28" } [6]=> array(1) { [0]=> string(1) "0" } [7]=> array(0) { } [8]=> array(0) { } [9]=> array(0) { } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } [14]=> array(0) { } [15]=> array(0) { } [16]=> array(0) { } [17]=> array(0) { } [18]=> array(0) { } [19]=> array(0) { } [20]=> array(1) { [0]=> string(2) "59" } [21]=> array(1) { [0]=> string(2) "49" } [22]=> array(0) { } [23]=> array(1) { [0]=> string(2) "38" } [24]=> array(1) { [0]=> string(2) "38" } [25]=> array(0) { } [26]=> array(1) { [0]=> string(2) "37" } [27]=> array(1) { [0]=> string(2) "25" } [28]=> array(1) { [0]=> string(1) "0" } [29]=> array(0) { } [30]=> array(1) { [0]=> string(2) "79" } [31]=> array(1) { [0]=> string(2) "99" } [32]=> array(0) { } [33]=> array(1) { [0]=> string(2) "75" } [34]=> array(1) { [0]=> string(2) "99" } [35]=> array(0) { } [36]=> array(1) { [0]=> string(2) "79" } [37]=> array(1) { [0]=> string(2) "99" } [38]=> array(0) { } [39]=> array(1) { [0]=> string(3) "160" } [40]=> array(1) { [0]=> string(3) "190" } [41]=> array(0) { } [42]=> array(0) { } [43]=> array(0) { } [44]=> array(0) { } [45]=> array(0) { } [46]=> array(0) { } [47]=> array(0) { } [48]=> array(0) { } [49]=> array(0) { } [50]=> array(0) { } [51]=> array(0) { } [52]=> array(1) { [0]=> string(2) "33" } [53]=> array(0) { } [54]=> array(1) { [0]=> string(2) "59" } [55]=> array(1) { [0]=> string(2) "34" } [56]=> array(1) { [0]=> string(1) "0" } [57]=> array(0) { } [58]=> array(1) { [0]=> string(2) "29" } [59]=> array(1) { [0]=> string(2) "14" } [60]=> array(1) { [0]=> string(1) "0" } [61]=> array(0) { } [62]=> array(1) { [0]=> string(2) "37" } [63]=> array(1) { [0]=> string(2) "25" } [64]=> array(1) { [0]=> string(1) "0" } } ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } } }
and var_dump($response->getValueRanges()[0]['values']); gives this result:
array(74) { [0]=> array(5) { [0]=> string(2) "ID" [1]=> string(10) "Ticket for" [2]=> string(9) "Excursion" [3]=> string(8) "Duration" [4]=> string(14) "Price (Brutto)" } [1]=> array(5) { [0]=> string(3) "01A" [1]=> string(13) "adult ( 12+ )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "28" } [2]=> array(5) { [0]=> string(3) "01B" [1]=> string(18) "child ( age 5-11 )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "14" } [3]=> array(5) { [0]=> string(3) "01C" [1]=> string(18) "toodler ( age 0-4)" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(1) "0" } [4]=> array(0) { } [5]=> array(5) { [0]=> string(3) "02A" [1]=> string(13) "adult, age 8+" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "55" } [6]=> array(5) { [0]=> string(3) "02B" [1]=> string(14) "child, age 3-7" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "28" } [7]=> array(5) { [0]=> string(3) "02C" [1]=> string(16) "toodler, age 0-2" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(1) "0" } [8]=> array(0) { } [9]=> array(3) { [0]=> string(3) "03A" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [10]=> array(3) { [0]=> string(3) "03B" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [11]=> array(3) { [0]=> string(3) "03C" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [12]=> array(0) { } [13]=> array(3) { [0]=> string(3) "04A" [1]=> string(0) "" [2]=> string(9) "Submarine" } [14]=> array(3) { [0]=> string(3) "04B" [1]=> string(0) "" [2]=> string(9) "Submarine" } [15]=> array(3) { [0]=> string(3) "04C" [1]=> string(0) "" [2]=> string(9) "Submarine" } [16]=> array(0) { } [17]=> array(3) { [0]=> string(3) "05A" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [18]=> array(3) { [0]=> string(3) "05B" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [19]=> array(3) { [0]=> string(3) "05C" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [20]=> array(0) { } [21]=> array(5) { [0]=> string(3) "06A" [1]=> string(9) "fisherman" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "59" } [22]=> array(5) { [0]=> string(3) "06B" [1]=> string(9) "spectator" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "49" } [23]=> array(0) { } [24]=> array(5) { [0]=> string(3) "07A" [1]=> string(5) "adult" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [25]=> array(5) { [0]=> string(3) "07B" [1]=> string(13) "child, age 6+" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [26]=> array(0) { } [27]=> array(5) { [0]=> string(3) "08A" [1]=> string(14) "adult, age 12+" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "37" } [28]=> array(5) { [0]=> string(3) "08B" [1]=> string(15) "child, age 3-11" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "25" } [29]=> array(5) { [0]=> string(3) "08C" [1]=> string(16) "toodler, age 0-1" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(1) "0" } [30]=> array(0) { } [31]=> array(5) { [0]=> string(3) "09A" [1]=> string(17) "flight from 800m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "15min" [4]=> string(2) "79" } [32]=> array(5) { [0]=> string(3) "09B" [1]=> string(18) "flight from 1000m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "25min" [4]=> string(2) "99" } [33]=> array(0) { } [34]=> array(5) { [0]=> string(3) "10A" [1]=> string(21) "Single (for 1 person)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "75" } [35]=> array(5) { [0]=> string(3) "10B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "99" } [36]=> array(0) { } [37]=> array(5) { [0]=> string(3) "11A" [1]=> string(21) "Single (for 1 person)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "79" } [38]=> array(5) { [0]=> string(3) "11B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "99" } [39]=> array(0) { } [40]=> array(5) { [0]=> string(3) "12A" [1]=> string(21) "Single (for 1 person)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "160" } [41]=> array(5) { [0]=> string(3) "12B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "190" } [42]=> array(0) { } [43]=> array(3) { [0]=> string(3) "13A" [1]=> string(0) "" [2]=> string(8) "Trekking" } [44]=> array(3) { [0]=> string(3) "13B" [1]=> string(0) "" [2]=> string(8) "Trekking" } [45]=> array(3) { [0]=> string(3) "13C" [1]=> string(0) "" [2]=> string(8) "Trekking" } [46]=> array(0) { } [47]=> array(3) { [0]=> string(3) "14A" [1]=> string(0) "" [2]=> string(7) "Karting" } [48]=> array(3) { [0]=> string(3) "14B" [1]=> string(0) "" [2]=> string(7) "Karting" } [49]=> array(3) { [0]=> string(3) "14C" [1]=> string(0) "" [2]=> string(7) "Karting" } [50]=> array(0) { } [51]=> array(3) { [0]=> string(3) "15A" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [52]=> array(3) { [0]=> string(3) "15B" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [53]=> array(5) { [0]=> string(3) "15C" [1]=> string(0) "" [2]=> string(11) "Jungle park" [3]=> string(0) "" [4]=> string(2) "33" } [54]=> array(0) { } [55]=> array(5) { [0]=> string(3) "16A" [1]=> string(14) "adult, age 12+" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "59" } [56]=> array(5) { [0]=> string(3) "16B" [1]=> string(15) "child, age 2-11" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "34" } [57]=> array(5) { [0]=> string(3) "16C" [1]=> string(16) "toodler, age 0-1" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(1) "0" } [58]=> array(0) { } [59]=> array(5) { [0]=> string(3) "17A" [1]=> string(14) "adult, age 12+" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "29" } [60]=> array(5) { [0]=> string(3) "17B" [1]=> string(15) "child, age 2-11" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "14" } [61]=> array(5) { [0]=> string(3) "17C" [1]=> string(16) "toodler, age 0-1" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(1) "0" } [62]=> array(0) { } [63]=> array(5) { [0]=> string(3) "18A" [1]=> string(14) "adult, age 12+" [2]=> string(11) "Loro parque" [3]=> string(12) "8:30 - 18:45" [4]=> string(2) "37" } [64]=> array(5) { [0]=> string(3) "18B" [1]=> string(15) "child, age 6-11" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(2) "25" } [65]=> array(5) { [0]=> string(3) "18C" [1]=> string(16) "toodler, age 0-1" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(1) "0" } [66]=> array(0) { } [67]=> array(3) { [0]=> string(3) "19A" [1]=> string(0) "" [2]=> string(9) "Antologia" } [68]=> array(3) { [0]=> string(3) "19B" [1]=> string(0) "" [2]=> string(9) "Antologia" } [69]=> array(3) { [0]=> string(3) "19C" [1]=> string(0) "" [2]=> string(9) "Antologia" } [70]=> array(0) { } [71]=> array(3) { [0]=> string(3) "20A" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [72]=> array(3) { [0]=> string(3) "20B" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [73]=> array(3) { [0]=> string(3) "20C" [1]=> string(0) "" [2]=> string(13) "Medieval show" } }
And:
for($i = 1; $i < 5; $i++) {
print_r ($response->getValueRanges()[0]['values'][$i]);
}
Gives: Array ( [0] => 01A [1] => adult ( 12+ ) [2] => Catamaran [3] => 2h [4] => 28 ) Array ( [0] => 01B [1] => child ( age 5-11 ) [2] => Catamaran [3] => 2h [4] => 14 ) Array ( [0] => 01C [1] => toodler ( age 0-4) [2] => Catamaran [3] => 2h [4] => 0 ) Array ( )
In result I need a simple array, which will hold a key[row id], the ID from from ID row and price from last row, somethig like this: $result[$i] = unique_id=1, ID=01A, price=28
How it could be achieved?
So after getting the data for google sheet with:
$arr = $response->getValueRanges()[0]['values'];
You can use this simple for loop to get the data as you need:
// notice start from 1 to avoid the headers
for($i = 1; $i < count($arr); $i++) {
$res[$i] = array("unique_id" => $i, "ID" => $arr[$i][0], "price" => $arr[$i][4]);
}
Now $res will contain you structure.
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.
i'm using codeigniter for create a web site. and i need to take result from mysql table as
this
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(12) ["type"]=> int(0) }
but always i got as a result something efferent from this.
when i'm using result_object() in codeigniter i'm getting something like this
array(12) { [0]=> object(stdClass)#24 (4) { ["id"]=> string(1) "1" ["label"]=> string(15) "Web Development" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [1]=> object(stdClass)#25 (4) { ["id"]=> string(2) "10" ["label"]=> string(19) "Sales and Marketing" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [2]=> object(stdClass)#26 (4) { ["id"]=> string(1) "7" ["label"]=> string(18) "Design and Artwork" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [3]=> object(stdClass)#27 (4) { ["id"]=> string(1) "2" ["label"]=> string(16) "Content Creation" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [4]=> object(stdClass)#28 (4) { ["id"]=> string(1) "4" ["label"]=> string(19) "OSCommerce projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [5]=> object(stdClass)#29 (4) { ["id"]=> string(1) "3" ["label"]=> string(8) "PHP Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [6]=> object(stdClass)#30 (4) { ["id"]=> string(1) "5" ["label"]=> string(22) "Technical Writing Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [7]=> object(stdClass)#31 (4) { ["id"]=> string(1) "6" ["label"]=> string(13) "Forum Posting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [8]=> object(stdClass)#32 (4) { ["id"]=> string(1) "8" ["label"]=> string(20) "Blog Design Projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [9]=> object(stdClass)#33 (4) { ["id"]=> string(1) "9" ["label"]=> string(24) "Freelance Website Design" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [10]=> object(stdClass)#34 (4) { ["id"]=> string(2) "11" ["label"]=> string(29) "Internet Marketing Consulting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } [11]=> object(stdClass)#35 (4) { ["id"]=> string(2) "12" ["label"]=> string(25) "Leads Generation Services" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } }
$this->db->order_by('parent_id','id','ASC');
$query = $this->db->get('dyn_menu');
if ($query->num_rows() > 0) {
$data = $query->result_object();
return $data;
}
how can i get the result as i want?
try using just result(), like:
...
$data = $query->result();
return $data;
...
try this if single record you want, make $is_single = true
if($is_single)
return $query ->row_array();
else
return $query ->result_array();
I received this XML data and var_dumped it.
How do I use this now and fetch one of the records from inside? like $this->{product}->name etc.?
object(SimpleXMLElement)#189 (1) { ["products"]=> object(SimpleXMLElement)#188 (2) { ["#attributes"]=> array(3) { ["total-matched"]=> string(1) "3" ["records-returned"]=> string(1) "3" ["page-number"]=> string(1) "1" } ["product"]=> array(3) { [0]=> object(SimpleXMLElement)#206 (19) { ["ad-id"]=> string(8) "10581256" ["advertiser-id"]=> string(7) "2466656" ["advertiser-name"]=> string(18) "Everydaysource.com" ["advertiser-category"]=> string(21) "MP3 Accessories>Cases" ["buy-url"]=> string(245) "http://www.kqzyfj.com/click-4107027-10581256?url=http%3A%2F%2Fwww.everydaysource.com%2Fproduct%2Fsnap-on-case-compatible-with-apple-ipod-touch-4th-gen-colorful-zebra%2Fdapptoucco62%3Fesaffcojudtf120626%3Daf%2Bcj%2Bdapptoucco62&cjsku=DAPPTOUCCO62" ["catalog-id"]=> string(8) "cjo:2691" ["currency"]=> string(3) "USD" ["description"]=> string(57) "Compatible With Apple®:iPod touch® 4th Generation" ["image-url"]=> string(73) "http://cdn1.everydaysource.com/cdn2/110207/fb/28/d2/dapptoucco62_1_es.jpg" ["in-stock"]=> object(SimpleXMLElement)#98 (0) { } ["isbn"]=> object(SimpleXMLElement)#99 (0) { } ["manufacturer-name"]=> string(14) "EverydaySource" ["manufacturer-sku"]=> string(12) "DAPPTOUCCO62" ["name"]=> string(69) "Snap-on Case Compatible with Apple iPod Touch 4th Gen,Colorful Zebra" ["price"]=> string(4) "8.99" ["retail-price"]=> string(3) "0.0" ["sale-price"]=> string(3) "0.0" ["sku"]=> string(12) "DAPPTOUCCO62" ["upc"]=> string(12) "628569324674" } [1]=> object(SimpleXMLElement)#100 (19) { ["ad-id"]=> string(8) "10402409" ["advertiser-id"]=> string(7) "1636472" ["advertiser-name"]=> string(8) "Eforcity" ["advertiser-category"]=> string(21) "MP3 Accessories>Cases" ["buy-url"]=> string(233) "http://www.jdoqocy.com/click-4107027-10402409?url=http%3A%2F%2Fwww.eforcity.com%2Fsnap-on-case-compatible-with-apple-ipod-touch-4th-gen-colorful-zebra-dapptoucco62.html%3Fefaffcojudtf120626%3Daf%2Bcj%2Bdapptoucco62&cjsku=DAPPTOUCCO62" ["catalog-id"]=> string(8) "cjo:1136" ["currency"]=> string(3) "USD" ["description"]=> string(57) "Compatible With Apple®:iPod touch® 4th Generation" ["image-url"]=> string(68) "http://cdn1.eforcity.com/cdn2/110101/41/86/a3/dapptoucco62_1_efc.jpg" ["in-stock"]=> object(SimpleXMLElement)#99 (0) { } ["isbn"]=> object(SimpleXMLElement)#98 (0) { } ["manufacturer-name"]=> string(8) "eForCity" ["manufacturer-sku"]=> string(12) "DAPPTOUCCO62" ["name"]=> string(69) "Snap-on Case Compatible with Apple iPod Touch 4th Gen,Colorful Zebra" ["price"]=> string(4) "7.99" ["retail-price"]=> string(3) "0.0" ["sale-price"]=> string(3) "0.0" ["sku"]=> string(12) "DAPPTOUCCO62" ["upc"]=> string(12) "628569324674" } [2]=> object(SimpleXMLElement)#101 (19) { ["ad-id"]=> string(8) "10581247" ["advertiser-id"]=> string(7) "2466659" ["advertiser-name"]=> string(15) "iTrimming Corp." ["advertiser-category"]=> string(21) "MP3 Accessories>Cases" ["buy-url"]=> string(165) "http://www.dpbolvw.net/click-4107027-10581247?url=http%3A%2F%2Fwww.itrimming.com%2Fdapptoucco62.html%3Fitaffcojudtf120626%3Daf%2Bcj%2Bdapptoucco62&cjsku=DAPPTOUCCO62" ["catalog-id"]=> string(8) "cjo:2690" ["currency"]=> string(3) "USD" ["description"]=> string(57) "Compatible With Apple®:iPod touch® 4th Generation" ["image-url"]=> string(68) "http://cdn1.itrimming.com/cdn2/110301/c9/cc/33/dapptoucco62_1_it.jpg" ["in-stock"]=> object(SimpleXMLElement)#98 (0) { } ["isbn"]=> object(SimpleXMLElement)#99 (0) { } ["manufacturer-name"]=> string(9) "iTrimming" ["manufacturer-sku"]=> string(12) "DAPPTOUCCO62" ["name"]=> string(69) "Snap-on Case Compatible with Apple iPod Touch 4th Gen,Colorful Zebra" ["price"]=> string(4) "7.59" ["retail-price"]=> string(3) "0.0" ["sale-price"]=> string(3) "0.0" ["sku"]=> string(12) "DAPPTOUCCO62" ["upc"]=> string(12) "628569324674" } } } }
Pastebin of the same : http://pastebin.com/XYN8ysEG
Thank you!
** Edit **
Here is the raw data
object(SimpleXMLElement)#189 (1) {
["products"]=>
object(SimpleXMLElement)#188 (2) {
["#attributes"]=>
array(3) {
["total-matched"]=>
string(1) "4"
["records-returned"]=>
string(1) "4"
["page-number"]=>
string(1) "1"
}
["product"]=>
array(4) {
[0]=>
object(SimpleXMLElement)#206 (19) {
["ad-id"]=>
string(8) "10450940"
["advertiser-id"]=>
string(7) "1427863"
["advertiser-name"]=>
string(12) "Bookbyte.com"
["advertiser-category"]=>
string(5) "Books"
["buy-url"]=>
string(151) "http://www.anrdoezrs.net/click-4107027-10450940?url=http%3A%2F%2Fwww.bookbyte.com%2Fproduct.aspx%3Fisbn%3D1576751740%26noredirect%3Dtrue&cjsku=13671976"
["catalog-id"]=>
string(8) "cjo:1574"
["currency"]=>
string(3) "USD"
["description"]=>
string(34) "This book is in Like New condition"
["image-url"]=>
object(SimpleXMLElement)#77 (0) {
}
["in-stock"]=>
object(SimpleXMLElement)#74 (0) {
}
["isbn"]=>
string(10) "1576751740"
["manufacturer-name"]=>
object(SimpleXMLElement)#75 (0) {
}
["manufacturer-sku"]=>
object(SimpleXMLElement)#72 (0) {
}
["name"]=>
string(53) "Leadership and Self Deception: Getting Out of the Box"
["price"]=>
string(4) "13.5"
["retail-price"]=>
string(5) "14.95"
["sale-price"]=>
string(3) "0.0"
["sku"]=>
string(8) "13671976"
["upc"]=>
object(SimpleXMLElement)#73 (0) {
}
}
[1]=>
object(SimpleXMLElement)#78 (19) {
["ad-id"]=>
string(8) "10450940"
["advertiser-id"]=>
string(7) "1427863"
["advertiser-name"]=>
string(12) "Bookbyte.com"
["advertiser-category"]=>
string(5) "Books"
["buy-url"]=>
string(148) "http://www.jdoqocy.com/click-4107027-10450940?url=http%3A%2F%2Fwww.bookbyte.com%2Fproduct.aspx%3Fisbn%3D1576751740%26noredirect%3Dtrue&cjsku=1084348"
["catalog-id"]=>
string(8) "cjo:1574"
["currency"]=>
string(3) "USD"
["description"]=>
string(41) "This book is in New - Excellent condition"
["image-url"]=>
object(SimpleXMLElement)#73 (0) {
}
["in-stock"]=>
object(SimpleXMLElement)#72 (0) {
}
["isbn"]=>
string(10) "1576751740"
["manufacturer-name"]=>
object(SimpleXMLElement)#75 (0) {
}
["manufacturer-sku"]=>
object(SimpleXMLElement)#74 (0) {
}
["name"]=>
string(53) "Leadership and Self Deception: Getting Out of the Box"
["price"]=>
string(5) "11.21"
["retail-price"]=>
string(5) "14.95"
["sale-price"]=>
string(3) "0.0"
["sku"]=>
string(7) "1084348"
["upc"]=>
object(SimpleXMLElement)#77 (0) {
}
}
[2]=>
object(SimpleXMLElement)#79 (19) {
["ad-id"]=>
string(8) "10450940"
["advertiser-id"]=>
string(7) "1427863"
["advertiser-name"]=>
string(12) "Bookbyte.com"
["advertiser-category"]=>
string(5) "Books"
["buy-url"]=>
string(148) "http://www.kqzyfj.com/click-4107027-10450940?url=http%3A%2F%2Fwww.bookbyte.com%2Fproduct.aspx%3Fisbn%3D1576751740%26noredirect%3Dtrue&cjsku=12530742"
["catalog-id"]=>
string(8) "cjo:1574"
["currency"]=>
string(3) "USD"
["description"]=>
string(34) "This book is in Like New condition"
["image-url"]=>
object(SimpleXMLElement)#77 (0) {
}
["in-stock"]=>
object(SimpleXMLElement)#74 (0) {
}
["isbn"]=>
string(10) "1576751740"
["manufacturer-name"]=>
object(SimpleXMLElement)#75 (0) {
}
["manufacturer-sku"]=>
object(SimpleXMLElement)#72 (0) {
}
["name"]=>
string(53) "Leadership and Self Deception: Getting Out of the Box"
["price"]=>
string(5) "29.25"
["retail-price"]=>
string(5) "14.95"
["sale-price"]=>
string(3) "0.0"
["sku"]=>
string(8) "12530742"
["upc"]=>
object(SimpleXMLElement)#73 (0) {
}
}
[3]=>
object(SimpleXMLElement)#76 (19) {
["ad-id"]=>
string(8) "10586024"
["advertiser-id"]=>
string(7) "2172304"
["advertiser-name"]=>
string(5) "Chegg"
["advertiser-category"]=>
string(45) "Media > Books > Non-Fiction > Education Books"
["buy-url"]=>
string(116) "http://www.dpbolvw.net/click-4107027-10586024?url=http%3A%2F%2Fwww.chegg.com%2Fdetails%2F1576751740&cjsku=1576751740"
["catalog-id"]=>
string(8) "cjo:2718"
["currency"]=>
string(3) "USD"
["description"]=>
string(584) "The disease of self-deception (acting in ways contrary to what one knows is right) underlies all leadership problems in today's organizations, according to the premise of this work. However well intentioned they may be, leaders who deceive themselves always end up undermining their own performance.This straightforward book explains how leaders can discover their own self-deceptions and learn how to escape destructive patterns. The authors demonstrate that breaking out of these patterns leads to improved teamwork, commitment, trust, communication, motivation, and leadership."
["image-url"]=>
string(53) "http://c.chegg.com/covers2/10000/15246_1307127219.jpg"
["in-stock"]=>
object(SimpleXMLElement)#73 (0) {
}
["isbn"]=>
string(10) "1576751740"
["manufacturer-name"]=>
string(9) "chegg.com"
["manufacturer-sku"]=>
object(SimpleXMLElement)#72 (0) {
}
["name"]=>
string(29) "Leadership and Self-Deception"
["price"]=>
string(4) "9.99"
["retail-price"]=>
string(5) "15.95"
["sale-price"]=>
string(3) "0.0"
["sku"]=>
string(10) "1576751740"
["upc"]=>
string(13) "9781576751749"
}
}
}
}
they are Objects (SimpleXMLElement)
$result->products->product[0]->name