Jump to content

Talking to Arrays - print_r


nimzie

Recommended Posts

I make a call to print_r ($row); in my application. I want to access the item_title and ask

 

if ( $row->item_title == 1 ) {

Do whatever

}

 

I don't seem to have the syntax correct or understand how I would address each level of the array. Any help would be appreciated.

 

Thanks :)

 

Adam

 

 

stdClass Object
(
    [id] => 25
    [title] => Lorem Ipsum Products
    [title_alias] => Lorem Ipsum Products
    [introtext] => <p>Proin justo tellus, elementum sit amet, aliquam ut, convallis nec, lectus. Integer quam justo, nonummy eget, eleifend ut, facilisis vel, est. Curabitur eget leo non tellus ullamcorper pharetra. </p>
    [fulltext] => 
    [state] => 1
    [sectionid] => 9
    [mask] => 0
    [catid] => 24
    [created] => 2007-12-06 12:35:40
    [created_by] => 62
    [created_by_alias] => 
    [modified] => 2007-12-06 22:23:43
    [modified_by] => 62
    [checked_out] => 0
    [checked_out_time] => 0000-00-00 00:00:00
    [publish_up] => 2007-12-06 12:34:34
    [publish_down] => 0000-00-00 00:00:00
    [images] => 
    [urls] => 
    [attribs] => pageclass_sfx=
back_button=
item_title=1
link_titles=
introtext=1
section=0
section_link=0
category=0
category_link=0
rating=
author=
createdate=
modifydate=
pdf=
print=
email=
keyref=
docbook_type=
    [version] => 2
    [parentid] => 0
    [ordering] => 1
    [metakey] => 
    [metadesc] => 
    [access] => 0
    [hits] => 0
    [author] => Administrator
    [usertype] => Super Administrator
    [category] => Private Banking
    [section] => Products
    [groups] => Public
    [sec_pub] => 1
    [cat_pub] => 1
    [sec_access] => 0
    [cat_access] => 0
    [sec_id] => 9
    [cat_id] => 24
    [prev] => 
    [next] => 
    [text] => <p>Proin justo tellus, elementum sit amet, aliquam ut, convallis nec, lectus. Integer quam justo, nonummy eget, eleifend ut, facilisis vel, est. Curabitur eget leo non tellus ullamcorper pharetra. </p>

)

Link to comment
https://forums.phpfreaks.com/topic/80875-talking-to-arrays-print_r/
Share on other sites

use a foreach loop, running this code might help you understand how it works

 

<?php
//the object name is $myObject

foreach ($myObject as $key => $val) {
//goes through the whole object
// you can access each key or title with $key
//you can access each value or item with $val
echo "$key => $val<br>\r\n";
}
?>

I just want to check the setting for item_title in $row which is the object I am outputting in that print_r.

 

I want to check and see if it's set to 1 and if so, do whatever. I'm more wondering the syntax of what I shoudl be doing cause nothing seems to be working...

 

Thanks :)

If this is a simple array, you CAN do this:

 

$value = $some_array["title"];
//do whatever you want with $value...

 

If you're looking for a more generalized function for an object that you convert to an array, w/e... I'm sure something like this helps:

 

function get_value($array,$key){
foreach($array as $index=>$value){
if($index == $key){return $value;}
}
return false;
}

 

Simple function that can get other types of keys for you... So, taking your array for instance (let's call it $some_array)

 

$value = get_value($some_array,"title");
//do whatever you want with $value...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.