Didos Rukundo
Didos Rukundo

Reputation: 45

How to use a foreach loop o an array for searching?

I have my source code here:

$title = "news1";

$a = array();
foreach($page_news as $keys=>$news) // this comes from controller including news1.
{
    $x[] = $news->title;
    $x[] .= $news->url;
}

// I want to get the index of matches where $x[values = "news1"]
$key_title = array_search($title,$a); 

Then my question is : if I get the index of news->title, how can I get the correspondent index of $news->url?

Upvotes: 1

Views: 893

Answers (3)

Nigel Ren
Nigel Ren

Reputation: 57131

If your using PHP 7+, you can use array_column to convert the data into an indexed list of objects...

$indexed = array_column($page_news, null, "title");
echo "url=".$indexed[$title]->url;

BUT if you don't need this data for anything else, you would be better off doing a simple foreach and compare it against each item. This saves converting and storing all of the data which isn't used later ...

$url = "";
foreach ( $page_news as $news ) {
    if ( $news->title = $title )    {
        $url = $news->url;
        break;
    }
}
echo "url=".$url.PHP_EOL;

(this works in most versions of PHP)

Upvotes: 0

Mickaël Leger
Mickaël Leger

Reputation: 3440

Here is some idea to achieve it :

1/ Create an associative array for each title + url :

$title = "news1";

$a = array();
foreach($page_news as $keys = >$news) 
{
    $a[] = array(
        'title' => $news->title,
        'url'   => $news->url
}

Then to find the key of $title :

foreach ($a as $key => $data) {
    if ($data['title'] == $title) {
        $key_title = $key;
    }
}

Then echo $key_title will give you the key.

If you want to avoid the foreach loop, I recommand you to do what Phillip suggest : add the title as key.

2/ If you know the title you want to search before the first foreach loop (in your example you have the title before), maybe you can get it in this loop :

 $title = "news1";

$a = array();

$key = 0;
foreach($page_news as $keys => $news)
{
    $a[$key] = $news->title;

    if ($news->title == $title) {
        $key_title = $key;
    }   
    $key++;

    $a[$key] = $news->url;
    $key++;
}

Then echo $key_title will give you the key and echo $key_title + 1 will give the key of the associated url.

3/ And third idea is to do what the anwser if Philipp suggest with :

$key_title = array_search($title, $a);
$key_url = $key_title + 1;

Upvotes: 0

Philipp
Philipp

Reputation: 15629

As you always add the url after the title, you could simply add one to the index of your title to get the index of the url.

$title = "news1";

$a = array();
foreach($page_news as $keys=>$news) {
    $x[] = $news->title;
    $x[] = $news->url;
}
$key_title = array_search($title, $a);
$key_url = $key_title + 1;

A better way would be to use a multi dimensional array and use the title as the key - in this way, you don't need array_search

$a = array();
foreach($page_news as $keys=>$news) {
    $x[$news->title] = [
        'title' => $news->title,
        'url' => $news->url
    ];
}
$entry = $a[$title];
echo $entry['title'];
echo $entry['url'];

Upvotes: 1

Related Questions