Strip a IPTC.php result into two variables - php

I have the following code:
<?php
require_once('IPTC.php');
$iptc = new Image_IPTC('001.jpg');
print_r($iptc);
?>
And it returns:
Image_IPTC Object ( [_sFilename] => 001.jpg [_aIPTC] => Array (
[1#090] => Array ( [0] => %G ) [2#000] => Array ( [0] => ) [2#005]
=> Array ( [0] => TITULO NO WINDOWS, TITLE NO BRIDGE ) [2#080] => Array ( [0] => pictureauthor ) [2#085] => Array ( [0] => photographer )
[2#090] => Array ( [0] => mycity ) [2#095] => Array ( [0] => ST )
[2#101] => Array ( [0] => mycountry ) [2#105] => Array ( [0] =>
IWANTTHIS1 ) [2#116] => Array ( [0] => copyrightinfo ) [2#120] => Array ( [0] => IWANTTHIS2 ) ) [_bIPTCParse] => 1
)
This is a dummy question, but how do I put texts "IWANTTHIS1" and "IWANTTHIS2" into 2 different variables to use like this:
echo "title: $variable1 <br />";
echo "descr: $variable2";
Resulting in:
title: IWANTTHIS1
descr: IWANTTHIS2
I'm pretty shure it's extremelly easy for you guys, but I'm still learning all this. I think it's an array inside an array? Can't figure it out.
Thanks.

$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];

Rodaine's answer was giving me "Fatal error: Cannot use object of type Image_IPTC as array". With some research, the final correct answer is:
$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];
I wouldn't be able to achieve it by myself at all, therefore I'm marking Rodaine's answer as correct.
Thank you very much.

Related

add elements inside elements in associative array

I have an array that looks like this:
array
(
[0] => personA
[1] => personB
)
and I want to add elements to each person like this:
array
(
[0] => personA
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => personB
)
I'm using this code:
foreach($proj as $key => $cat)
{
$proj[$key] = $this->ReturnFolders(WWW_ROOT . "img/proyectos/" . $cat);
}
That function returns an array that looks like this:
array
(
[0] => elemA
[1] => elemB
)
But obviously is not working, I get this result:
array
(
[0] => Array
(
[0] => elemA
[1] => elemB
[2] => elemC
)
[1] => Array
)
Your "like this" structure is not possible. You cannot have a single array key have two different values like that (personA and the sub-array).
You'd have to build a more complex structure:
[0] => array(
'name' => 'personA'
'values' => array('elemA', 'elemB', 'elemC')
)

php combine multidimensional arrays

I'm having trouble combining these two arrays so that the keys are kept together. The problem (I think) I'm having is that the arrays don't match in their structures, and the array keys are integers in one and names in the other. I feel like I need to have one array (feel free to correct me) so that I can display the prices coherently on the page, but I can't wrap my head around how to do it. I tried an array_merge, but it looses the indexed tlds sub-array:
$result = array();
foreach($cats[0]['domorder'] as $domorder) {
$result = array_merge($domorder, $prices[0]);
}
Maybe I can somehow (this isn't working either) add a 'price' sub-array that won't be overwritten?
$result = array();
$prc = array();
$prc['price'] = $prices[0];
foreach($prc as $p) {
$result = array_merge($p, $cats[0]['domorder'][0]);
}
Here's basically what I'm working with...my apologies if these are not formatted correctly for questions here.
Array 1, category definitions of hosting/domain name products:
Array
(
[0] => Array
(
[hosting] => Array
(
[0] => vpslinuxin
[1] => resellerhostinglinuxuk
[2] => resellerwindowshostinguk
........etc,etc.........
[34] => hosting
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[0] => biz
)
)
[1] => Array
(
[dominfo] => Array
(
[0] => info
)
)
........etc,etc.........
Array 2, prices associated to the above categorized products:
Array
(
[0] => Array
(
[resellerhostinglinuxuk] => Array
(
[131] => Array
(
[renew] => Array
(
[1] => 43.19
)
[ssl] => 4.79
[add] => Array
(
[1] => 43.19
)
)
........etc,etc.........
[dombiz] => Array
(
[addtransferdomain] => Array
(
[1] => 10.69
)
[restoredomain] => Array
(
[1] => 69.95
)
[addnewdomain] => Array
(
[10] => 10.89
[9] => 10.89
)
........etc,etc.........
Anyone? I feel like this should be a fairly easy merge, but I can't figure out how to make it work.
Edit
Here's an example of how I think it should work:
Array
(
[0] => Array
(
[hosting] => Array
(
[vpslinuxin] => Array
(
[prices] => Array
(
[addons] => Array
(
.......
)
[plans] => Array
(
.......
)
)
)
)
[domorder] => Array
(
[0] => Array
(
[dombiz] => Array
(
[tlds] => Array
(
[0] => biz
)
[prices] => Array
(
[addtransferdomain] => Array
(
.......
)
[restoredomain] => Array
(
.......
)
[addnewdomain] => Array
(
.......
)
[renewdomain] => Array
(
.......
)
)
)
)
)
)
)
thanks for your help Michael but I managed to get it.
I was thinking too hard about it, so after dinner and some relaxing, I decided to simplify what I've been trying. There's no hard/fast rule saying that the two arrays need to be together - ultimately they're going to end up together anyway. So I just appended one to the other, defined by a 'product' and 'price' key:
$result = array();
$result[]['product'] = $cats[0];
$result[]['prices'] = $prices[0];

How can i get the values from this array?

In $attval when use foreach loop to print out the elements I get this output:
Array(
[0]
(
[id]=>1,
[name]=>xxx
)
[0]
(
[id]=>2,
[name]=>abc
)
)
For some reason both indices are the same.
I think I can still get the values using the multidimensional array, but i am confused as to how I can?
Assuming your code is something like this:
$attval = array();
$attval[0] = array("id"=>1,"name"=>"xxx");
$attval[1] = array("id"=>2,"name"=>"abc");
You can access individual properties like this:
$attval[0]['id']; // 1
$attval[1]['name']; // abc
You are showing a print_r of each sub-array, therefore your output should be:
Array
(
[id] => 1
[name] => xxx
)
Array
(
[id] => 2
[name] => abc
)
If you want a full view of the array you could just do:
print_r($attval);
Then you get:
Array
(
[0] => Array
(
[id] => 1
[name] => xxx
)
[1] => Array
(
[id] => 2
[name] => abc
)
)

parsing values in a php array

I am new to PHP, please help me with the stuff. Below is the code written followed by its results. How can i access [count] inside [property] in most effective way.
code
echo '<pre>';
print_r($result);
echo '</pre>';
output
Array
(
[0] => Array
(
[property] => Array
(
[href] => http://www.example.com
[count] => 2
[sample] => Array
(
)
[data] => Array
(
[0] => 100002067610524
[1] => 675985591
)
[users] =>
[active] => 1
)
)
)
Not the most effective, but the only possible ;-)
echo $result[0]['property']['count'];
if you have more than one element then use this
foreach($result as $childArray)
{
echo $childArray['property']['count'];
}
if you have only one element in it then use
$array = $result[0]['property']['count'];

PHP, how to echo specific object data from array?

I use this in wordpress:
$arr=get_post_meta($post->ID, false);
I receive this array:
Array (
[_edit_last] => Array ( [0] => 2)
[year_completed] => Array ( [0] => 2010 )
[designers] => Array ( [0] => )
[developers] => Array ( [0] => )
[producers] => Array ( [0] => )
[_edit_lock] => Array ( [0] => 1298159324 )
[name_en] => Array ( [0] => game 1)
[name_et] => Array ( [0] => game 2 )
[friends_meta] => Array ( [0] => )
)
How do I echo (no for, foreach, etc please) name_en data? Even print_r ($arr->name_en); doesn't work... I suppose it has to be something like - echo $arr->name_en[0]; ???
It is an array or arrays, so:
print_r($arr['name_en']);
or if you only want to get the data:
echo $arr['name_en'][0];
The -> operator is for accessing properties of objects.
echo $arr['name_en'][0] should work.
this should work
echo $arr[0]['year_completed'];
echo $arr[0]['designers'];
etc

Categories