Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Extracting values from an array?

Posted on 2012-12-21
4
263 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

809 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