Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Multi Dimensional Arrays

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Multi Dimensional Arrays

Postby alecthorne » Wed Dec 26, 2012 4:44 am

I am new to php and especially multi dimensional arrays. The following array is stored in a variable $elements

"c74a0dba-5307-4f90-b04e-fdc88c4dd434": {
"0": {
"value": "Norfolk"
}
},
"91866e91-70d4-405f-900c-475d0c027399": {
"country": {
"0": "GB"
}
},

I can view the whole array using the following

$county = json_decode($elements);
var_dump($county);

which results in

["c74a0dba-5307-4f90-b04e-fdc88c4dd434"]=> object(stdClass)#24 (1) { ["0"]=> object(stdClass)#25 (1) { ["value"]=> string(15) "Nottinghamshire" } } ["91866e91-70d4-405f-900c-475d0c027399"]=> object(stdClass)#26 (1) { ["country"]=> object(stdClass)#27 (1) { ["0"]=> string(2) "GB" } }

I need to extract the county information 'Nottinghamshire' using the unique identifier. My attempt was as follows, but this only results in a server error.

$countyresult = $county->getElement('c74a0dba-5307-4f90-b04e-fdc88c4dd434')->getElementData()->get('value');

What am I doing wrong.

Many thanks, Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am

Re: Multi Dimensional Arrays

Postby throwcode » Wed Dec 26, 2012 8:57 pm

Hi alecthorne,

I'm not sure what you have there. The json_decode() should return an object but its kind of a strange one. My guess is that the following will access the various properties:

$firstProd = $county('c74a0dba-5307-4f90-b04e-fdc88c4dd434');
$secondProd = $firstProd('0');
$val = $secondProd('value');

Then one would hope that $val is equal to 'Norfolk' when you do a var dump.

If it was me I would use a simple PHP object to contain this data.

TC
throwcode
New php-forum User
New php-forum User
 
Posts: 34
Joined: Mon Sep 05, 2011 7:35 am

Re: Multi Dimensional Arrays

Postby throwcode » Wed Dec 26, 2012 9:08 pm

Sorry,

$firstProd = $county->c74a0dba-5307-4f90-b04e-fdc88c4dd434;
$secondProd = $firstProd->0;
$val = $secondProd->value;

I don't know what I was thinking.

TC
throwcode
New php-forum User
New php-forum User
 
Posts: 34
Joined: Mon Sep 05, 2011 7:35 am

Re: Multi Dimensional Arrays

Postby alecthorne » Thu Dec 27, 2012 1:23 pm

TC, firstly thanks for the extremely quick reply.

The data is structured that way due to the ZOO content management program I am using. ZOO are also looking at the question, but no result so far.

I tried your recommendation, but no luck. There is something it does not like, as the rest of the page is not processed after the line firstProd = $county->c74a0dba-5307-4f90-b04e-fdc88c4dd434;. I only showed you part of the array, as the full thing is quite long, so maybe this is why. The var_dump of the full array (for another item) is shown below. The identifier c74a0dba-5307-4f90-b04e-fdc88c4dd434 is the same for each item array.

object(stdClass)#2 (18) { ["4d612549-f4cd-4487-ba42-f091ece35391"]=> object(stdClass)#3 (1) { ["0"]=> object(stdClass)#4 (1) { ["value"]=> string(13) "East Kilbride" } } ["5eb77708-72e8-4587-b65e-ee50eb0f9e6d"]=> object(stdClass)#5 (1) { ["0"]=> object(stdClass)#6 (1) { ["value"]=> string(24) "Kelvin Industrial Estate" } } ["520720dc-c480-405a-ac56-bf317f48d860"]=> object(stdClass)#7 (1) { ["0"]=> object(stdClass)#8 (1) { ["value"]=> string(17) "11 Bessemer Drive" } } ["fbaf887d-f0a8-4a5d-81f3-0f86200d6579"]=> object(stdClass)#9 (1) { ["0"]=> object(stdClass)#10 (1) { ["value"]=> string(0) "" } } ["2aef696e-d07b-4712-a4cb-a09cc8cf84ab"]=> object(stdClass)#11 (1) { ["0"]=> object(stdClass)#12 (1) { ["value"]=> string(821) "Acrylic fabrication is at the heart of Adam Plastics expertise. We have state of the art acrylic fabrication technology which includes - laser cutters, CNC machining, heat bending, heat forming, diamond polishing, flame polishing, solvent bonding, Ultrasonic Welding. This wide range of manufacturing capabilities allows us to supply customers anything from a simple cut panel to furniture solutions.We specialise in:• Bespoke acrylic solutions. Utilising top quality brand materials.• Plastic fabrication, profile shaping of all acrylic display and related plastic materials.• Thermoforming plastics including polycarbonate, PET, PETG, polypropylene & PVC.• Ancillary services include oven forming, drape forming, vinyl signage, CNC profile routing, laser cutting and all forms of acrylic fabrication.
" } } ["da16f47d-d6ba-4c7f-8cb5-be5ea3bc1909"]=> object(stdClass)#13 (1) { ["0"]=> object(stdClass)#14 (1) { ["value"]=> string(0) "" } } ["ed7d91ab-ab22-4235-911a-6f25c5a5be78"]=> object(stdClass)#15 (1) { ["0"]=> object(stdClass)#16 (1) { ["value"]=> string(156) "Adam Plastics are manufacturers of plastic products, point of sale, display equipment, acrylic furniture and bespoke products supplying the whole of the UK." } } ["d1e5d72d-bbf2-41e5-80a1-6a26aec038f5"]=> object(stdClass)#17 (1) { ["0"]=> object(stdClass)#18 (1) { ["value"]=> string(0) "" } } ["06c23627-fa0a-47d1-b2d4-b8939230d4b3"]=> object(stdClass)#19 (8) { ["file"]=> string(0) "" ["title"]=> string(0) "" ["link"]=> string(0) "" ["target"]=> string(1) "0" ["rel"]=> string(0) "" ["lightbox_image"]=> string(0) "" ["spotlight_effect"]=> string(0) "" ["caption"]=> string(0) "" } ["9d204ba7-4224-48a0-8897-60ff0357694e"]=> object(stdClass)#20 (1) { ["0"]=> object(stdClass)#21 (1) { ["value"]=> string(7) "G75 0QX" } } ["b832581e-ef49-4cbf-a89b-7d67aa363e94"]=> object(stdClass)#22 (1) { ["0"]=> object(stdClass)#23 (1) { ["value"]=> string(7) "Glasgow" } } ["c74a0dba-5307-4f90-b04e-fdc88c4dd434"]=> object(stdClass)#24 (1) { ["0"]=> object(stdClass)#25 (1) { ["value"]=> string(11) "Strathclyde" } } ["91866e91-70d4-405f-900c-475d0c027399"]=> object(stdClass)#26 (1) { ["country"]=> object(stdClass)#27 (1) { ["0"]=> string(2) "GB" } } ["8c0c0ab4-33dd-4353-827b-cb906a1fe87d"]=> object(stdClass)#28 (1) { ["0"]=> object(stdClass)#29 (1) { ["value"]=> string(12) "01355 574743" } } ["4e5b4c5c-748c-47e1-a09f-66296a5d6c6a"]=> object(stdClass)#30 (1) { ["0"]=> object(stdClass)#31 (1) { ["value"]=> string(12) "01355 574744" } } ["bde031e9-d78b-4d26-bcd4-e82902b70856"]=> object(stdClass)#32 (1) { ["0"]=> object(stdClass)#33 (4) { ["value"]=> string(0) "" ["text"]=> string(0) "" ["subject"]=> string(0) "" ["body"]=> string(0) "" } } ["14eec231-9c9c-45e5-a71b-93b12edeeab4"]=> object(stdClass)#34 (1) { ["0"]=> object(stdClass)#35 (5) { ["value"]=> string(29) "http://www.adamplastics.co.uk" ["text"]=> string(0) "" ["target"]=> string(1) "0" ["custom_title"]=> string(0) "" ["rel"]=> string(0) "" } } ["7c749f46-573c-47f4-88a1-b307b99ad0cd"]=> object(stdClass)#36 (1) { ["location"]=> string(0) "" } }

Maybe it is the way I am extracting this array. I can post the full php if you think this will help.

Again thanks!!

Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am

Re: Multi Dimensional Arrays

Postby alecthorne » Thu Dec 27, 2012 1:34 pm

TC,

Someone did suggest to treat this as an array using the following.

$arr = json_decode($elements,true);echo "<pre>",print_r($arr,true),"</pre>"; $key = "c74a0dba-5307-4f90-b04e-fdc88c4dd434";echo $arr[$key][0]['value'];

This shows the array structure as follows (shortened).

Array
(
[4d612549-f4cd-4487-ba42-f091ece35391] => Array
(
[0] => Array
(
[value] => East Kilbride
)

)

[5eb77708-72e8-4587-b65e-ee50eb0f9e6d] => Array
(
[0] => Array
(
[value] => Kelvin Industrial Estate
)

)

etc....

In this form, is it easier to extract?

Thanks, Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am

Re: Multi Dimensional Arrays

Postby throwcode » Thu Dec 27, 2012 9:47 pm

Hi Alec,

Yes using the json_decode's second argument set to true will return an array rather than the objects we were looking at earlier. I much prefer objects over arrays where possible so I have tried to evaluate what I am seeing that way.

It looks to me that you have composite objects after the extraction. You see the process has generated a lot of object(stdClass), this is typical when a non-object is converted to an object. For example if an array is converted to an object the object(stdClass) that is generated will have properties that are named by keys and corresponding values. That is what I think you are seeing here.

The first object is a container holding 18 objects that are mostly comprised of three keys the first two keys have object values and the third has a string value. You should be able to access the information like a standard object I would think. Try

echo $county->{4d612549-f4cd-4487-ba42-f091ece35391}->{0}->value;

That should return East Kilbride. If that does not work try placing quotes around the values in the curly braces. If that does not work, I'm sorry I'm out of ideas.

TC

Here is what the data looks like broken into objects:

object(stdClass)#2 (18) {

["4d612549-f4cd-4487-ba42-f091ece35391"]=>
object(stdClass)#3 (1) { ["0"]=>
object(stdClass)#4 (1) { ["value"]=> string(13) "East Kilbride" }
}
["5eb77708-72e8-4587-b65e-ee50eb0f9e6d"]=>
object(stdClass)#5 (1) { ["0"]=>
object(stdClass)#6 (1) { ["value"]=> string(24) "Kelvin Industrial Estate" }
}
["520720dc-c480-405a-ac56-bf317f48d860"]=>
object(stdClass)#7 (1) { ["0"]=>
object(stdClass)#8 (1) { ["value"]=> string(17) "11 Bessemer Drive" }
}
["fbaf887d-f0a8-4a5d-81f3-0f86200d6579"]=>
object(stdClass)#9 (1) { ["0"]=>
object(stdClass)#10 (1) { ["value"]=> string(0) "" }
}
["2aef696e-d07b-4712-a4cb-a09cc8cf84ab"]=>
object(stdClass)#11 (1) { ["0"]=>
object(stdClass)#12 (1) { ["value"]=> string(821) "Acrylic fabrication is at the heart of Adam Plastics expertise. We have state of the art acrylic fabrication technology which includes - laser cutters, CNC machining, heat bending, heat forming, diamond polishing, flame polishing, solvent bonding, Ultrasonic Welding. This wide range of manufacturing capabilities allows us to supply customers anything from a simple cut panel to furniture solutions.We specialise in:• Bespoke acrylic solutions. Utilising top quality brand materials.• Plastic fabrication, profile shaping of all acrylic display and related plastic materials.• Thermoforming plastics including polycarbonate, PET, PETG, polypropylene & PVC.• Ancillary services include oven forming, drape forming, vinyl signage, CNC profile routing, laser cutting and all forms of acrylic fabrication.
" }
}
["da16f47d-d6ba-4c7f-8cb5-be5ea3bc1909"]=>
object(stdClass)#13 (1) { ["0"]=>
object(stdClass)#14 (1) { ["value"]=> string(0) "" }
}
["ed7d91ab-ab22-4235-911a-6f25c5a5be78"]=>
object(stdClass)#15 (1) { ["0"]=>
object(stdClass)#16 (1) { ["value"]=> string(156) "Adam Plastics are manufacturers of plastic products, point of sale, display equipment, acrylic furniture and bespoke products supplying the whole of the UK." }
}
["d1e5d72d-bbf2-41e5-80a1-6a26aec038f5"]=>
object(stdClass)#17 (1) { ["0"]=>
object(stdClass)#18 (1) { ["value"]=> string(0) "" }
}
["06c23627-fa0a-47d1-b2d4-b8939230d4b3"]=>
object(stdClass)#19 (8) { ["file"]=> string(0) "" ["title"]=> string(0) "" ["link"]=> string(0) "" ["target"]=> string(1) "0" ["rel"]=> string(0) "" ["lightbox_image"]=> string(0) "" ["spotlight_effect"]=> string(0) "" ["caption"]=> string(0) ""
}
["9d204ba7-4224-48a0-8897-60ff0357694e"]=>
object(stdClass)#20 (1) { ["0"]=>
object(stdClass)#21 (1) { ["value"]=> string(7) "G75 0QX" }
}
["b832581e-ef49-4cbf-a89b-7d67aa363e94"]=>
object(stdClass)#22 (1) { ["0"]=>
object(stdClass)#23 (1) { ["value"]=> string(7) "Glasgow" }
}
["c74a0dba-5307-4f90-b04e-fdc88c4dd434"]=>
object(stdClass)#24 (1) { ["0"]=>
object(stdClass)#25 (1) { ["value"]=> string(11) "Strathclyde" }
}
["91866e91-70d4-405f-900c-475d0c027399"]=>
object(stdClass)#26 (1) { ["country"]=>
object(stdClass)#27 (1) { ["0"]=> string(2) "GB" }
}
["8c0c0ab4-33dd-4353-827b-cb906a1fe87d"]=>
object(stdClass)#28 (1) { ["0"]=>
object(stdClass)#29 (1) { ["value"]=> string(12) "01355 574743" }
}
["4e5b4c5c-748c-47e1-a09f-66296a5d6c6a"]=>
object(stdClass)#30 (1) { ["0"]=>
object(stdClass)#31 (1) { ["value"]=> string(12) "01355 574744" }
}
["bde031e9-d78b-4d26-bcd4-e82902b70856"]=>
object(stdClass)#32 (1) { ["0"]=>
object(stdClass)#33 (4) { ["value"]=> string(0) "" ["text"]=> string(0) "" ["subject"]=> string(0) "" ["body"]=> string(0) "" }
}
["14eec231-9c9c-45e5-a71b-93b12edeeab4"]=>
object(stdClass)#34 (1) { ["0"]=>
object(stdClass)#35 (5) { ["value"]=> string(29) "http://www.adamplastics.co.uk" ["text"]=> string(0) "" ["target"]=> string(1) "0" ["custom_title"]=> string(0) "" ["rel"]=> string(0) "" }
}
["7c749f46-573c-47f4-88a1-b307b99ad0cd"]=> object(stdClass)#36 (1) { ["location"]=> string(0) "" }
}
throwcode
New php-forum User
New php-forum User
 
Posts: 34
Joined: Mon Sep 05, 2011 7:35 am

Re: Multi Dimensional Arrays

Postby alecthorne » Fri Dec 28, 2012 11:47 am

Oh TC you have made me an extremely happy man!!!

That worked a treat, and was the last hurdle to getting my site online.

If your interested, the project is www.linkindustry.co.uk

Again, especially as php is not my speciality, thanks!!!

Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am

Re: Multi Dimensional Arrays

Postby alecthorne » Tue Jan 29, 2013 12:30 pm

TC,

Could I bother you one last time regarding my last problem? Your suggestion worked a treat, thank you, with an example below.

$result = @mysql_query ("SELECT state, id, type, name, elements, params FROM yoo_zoo_item WHERE state=$checkstate AND type='$itemtype' AND id=$count");
$zooelements = json_decode($elements);
$county = $zooelements->{"c74a0dba-5307-4f90-b04e-fdc88c4dd434"}->{"0"}->value;

But if I had a new value ie $newcounty = “Norfolk”; how would I go about replacing the value for the array element with the identifier c74a0d…. with $newcounty ?, and then update the mysql result with the complete appended array ‘elements’?

Any pointers would help.

Thanks, Alec
alecthorne
New php-forum User
New php-forum User
 
Posts: 7
Joined: Wed Dec 26, 2012 4:21 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron