PHP Array Help

I've done this before, but for whatever reason am having trouble here.

My dataset if I var_dump the array:
array (size=2)
  'title' => string 'Nathan' (length=6)
  'link' => string 'Test' (length=4)

Open in new window


I'm trying to grab data in my foreach loop but it's pulling blank variables
var_dump($rssInfo);

    foreach($rssInfo as $rss){
        
        echo $rss['title'];
}

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Is that the exact code you're using?
0
Nathan RileyFounderAuthor Commented:
Yes, copy and pasted directly from the page.
0
Nathan RileyFounderAuthor Commented:
And here is my html form:

<div class="form-group rssRow">
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[title]" placeholder="Title"/>
                            </div>
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[link]" placeholder="Link"/>
                            </div>
                            <div class="clearfix"></div>
                        </div>

<div class="form-group rssRow">
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[title]" placeholder="Title"/>
                            </div>
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[link]" placeholder="Link"/>
                            </div>
                            <div class="clearfix"></div>
                        </div>

<div class="form-group rssRow">
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[title]" placeholder="Title"/>
                            </div>
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[link]" placeholder="Link"/>
                            </div>
                            <div class="clearfix"></div>
                        </div>

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Dave BaldwinFixer of ProblemsCommented:
You're treating it as an array of arrays and it is not, it is just a single array.  See here:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>rssInfo</title>
</head>
<body>
<h1>rssInfo</h1>
<pre>

<?php 
$rssInfo = array('title'=>'Nathan', 'link'=>'Test');

var_dump($rssInfo);

foreach($rssInfo as $rss){
	echo $rss.'<br>';
	}
?>

</pre>
</body>
</html>

Open in new window

0
Julian HansenCommented:
That won't work
In your foreach you are taking the array $rssInfo - and looping through its elements.

Therefore you cannot index the item by the $rss variable

Consider the following

foreach($rssInfo as $rss){
 on first iteration $rss = 'Nathan'
 on second itteration $rss = 'Test'
}

Open in new window

Title has no meaning to $rss because it is a value of the array and not the array itself

This will work

echo $rssInfo['title'];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
Like Julian said... an 'improved' version.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>rssInfo</title>
</head>
<body>
<h1>rssInfo</h1>
<pre>

<?php 
$rssInfo = array('title'=>'Nathan', 'link'=>'Test');

var_dump($rssInfo);

foreach($rssInfo as $rss){
	echo $rss.'<br>';
	}

echo '<br>'.$rssInfo['title']." ".$rssInfo['link'].'<br>';
?>

</pre>
</body>
</html>

Open in new window

0
Julian HansenCommented:
Not sure what your form code is trying to achieve - if the names of the inputs should be the title's and links or if you want the inputs to show the values - assumed the latter
<?php

$rssInfo = array (
	array('title' => 'Nathan', 	'link' => 'Test'),
	array('title' => 'Someone', 'link' => 'Anotherlink'),
	array('title' => 'Bing', 'link' => 'www')
);


foreach($rssInfo as $rss) :
?>
		<div class="form-group rssRow">
			<div class="col-md-6 noPadLeft">
				<input class="form-control" value="<?php echo $rss['title'];?>" placeholder="Title"/>
			</div>
			<div class="col-md-6 noPadLeft">
				<input class="form-control" value="<?php echo $rss['link'];?>" placeholder="Link"/>
			</div>
			<div class="clearfix"></div>
		</div>
<?php
endforeach;
?>

Open in new window

0
Ray PaseurCommented:
If this really involves RSS, there are some artful ways of dealing with it.  RSS is just a specialized form of XML. The standard is curated by a Harvard law professor.
http://cyber.law.harvard.edu/rss/rss.html
http://en.wikipedia.org/wiki/RSS

As such it is susceptible of processing with the PHP SimpleXML class.

If you want the best help with a question like this, please post the data (not the code) that shows us the before and after conditions.  Once we see what you've got and what you want, we can usually give you a tested and working example.
0
Nathan RileyFounderAuthor Commented:
Ok, thanks for the responses.

@Ray - The data I posted in the original question, that's the output when I var_dump are you looking for something else?

@Dave & Julian - Thanks and understood, I guess my issue is that my form post is not posting as an array for some reason.  

My input fields I can add more rows via jquery so I was trying to "name" them accordingly so that any additional fields that are added to the end of the form would just append to the array.  That's why I thought I needed a foreach loop, but it appears that whatever the last row entered in is the only data that is posting.  I think I may have my fields named improperly,.
0
hieloCommented:
>>I'm trying to grab data in my foreach loop but it's pulling blank variables
the second code fragment you posted on your original post would work if your HTML form was coded as:

<input class="form-control" name="rssInfo[][title]" placeholder="Title"/>
<input class="form-control" name="rssInfo[][link]" placeholder="Link"/>

(notice the empty "[]" immediately after rssInfo) which, based on your other question (Q_28683523.html) is most likely what you will need to process multiple sets of RSS-Link combos.
0
Julian HansenCommented:
If you are going to use dynamic variables in forms then do it like so

<input name="name[]" />
<input name="title[]" />

Open in new window

Now you can add as many rows as you like and PHP will automatically return the data to you in indexed arrays
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.