Solved

Extracting values from an array?

Posted on 2012-12-21
4
262 Views
Last Modified: 2012-12-21
Hi, I have an array as follows, I need only array[2] and array[3], how can I extract only these two array? Some time there will be more than two hosts, in essence I need all the array values between array[1] and the array that that has the string "registrations.". How can I do this?

Array
(
    [0] => Privilege: Command
    [1] => Host          username       password                          
    [2] => host01       user1             pw01            
    [3] => host02       user2             pw02  
     
    [4] => 2  registrations.
    [5] =>
    [6] => Follows
)

Thanks of the help.

A
0
Comment
Question by:aej1973
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
brendonfeeley earned 150 total points
ID: 38714102
Something like this should work:

//Loop through array
for($i = 2; $i < sizeof($array); $i++){
	//Look for registrations string. Break if found, push host details to new array if not.
	if(strpos($array[$i], ' registrations.') !== false) {
		break;
	}else{
		array_push($hosts_array, $array[$i]);
	}
}

print_r($hosts_array);

Open in new window

0
 

Author Comment

by:aej1973
ID: 38714367
brendonfeeley, thank you. I am not sure why but I keep getting the following error:

PHP Warning:  array_push() expects parameter 1 to be array, null given in /var/www/registryShowCli.php on line 19
PHP Warning:  array_push() expects parameter 1 to be array, null given in /var/www/registryShowCli.php on line 19

My code is as follows:

 $peer = implode("\n", $peer);//puts the whole thing in an array
 $row = explode("\n", $peer);//puts each line in an array
  print_r($row);
             for($i = 2; $i < sizeof($row); $i++){
                 if(strpos($row[$i], 'registrations.') !== false) {
                    break;
        }else{
                array_push($rows, $row[$i]);
        }
}

     print_r($rows);
0
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38714392
Place this line before the loop:

$rows = array();

The array needs to exist before you can push to it.

You could also replace the array_push with the following:

$rows[] = $row[$i];
0
 

Author Comment

by:aej1973
ID: 38714410
Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Query an Sqlite3 Datatbase return in JSON 6 49
wordpress on root is limiting my .htaccess 404.php options 4 25
ignore other .htaccess 2 44
Cookie not unsetting 7 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question