2

I've looked a bunch on here, and I've seem some similar type of questions, but none of them seemed to work in my situation (or at least I couldn't make them work), so hopefully this isn't an exact repeat.

I built an array that looks as follows:

Array
(
[0] => Array
    (
        [article_id] => 1
        [article_title] => Article 1
        [article_content] => This is some dummy text for the first article.
    )

[1] => Array
    (
        [article_id] => 2
        [article_title] => Article 2
        [article_content] => This is the content for article #2
    )
)

So the array has 3 keys (article_id, article_title, article_content), and what I'm trying to do is get a specific value at a specific index.

So, for example, if the page url is

/article.php?id=1

I first assign the id to a variable:

$linkID = $_GET['id'];

and now I want to search the array to find an article id match (to the $LinkID variable):

$foundAtPos = array_search($linkID, array_column($datas, 'article_id'));

this works fine, and gives me a matching "index" position, which is a great start, and now I want to assign the other two key values to variables (or echo them to the screen, something to show me that I've retrieved the correct id - article_title value).

This is the part where I get really stuck. I can easily display all values from the array, but I'm not looking to do that. I want to get ONLY the specific values that match the corresponding article_id.

Here's a few things I've tried (unsuccessfully):

    foreach ($datas as $keyName) {
        echo $keyName['article_title'];
    }

This gives me all the article titles, but that's not what I'm aiming for.

This bit I got from another stack overflow post, and it seemed like it was close, but I couldn't adapt it correctly.

 foreach($datas as $firstKey => $firstValue) {
  echo array_keys($firstValue, $firstValue['article_title'])[0].': '.$firstValue['article_title'].'</br>';
}

Same problem, gives me all the data, not just the row I'm trying to retrieve.

So I've gone in circles for hours, and it seems like this should be a common thing, but it's eluding me at the moment, so I'd appreciate any suggestions you fine folks might have.

Here's my entire function, for reference.

if (isset($_GET['id'])) {
    $linkID = $_GET['id'];

    foreach ($datas as $keyName) {
        //echo $keyName['article_title'];
    }

   //foreach($datas as $firstKey => $firstValue) {
  //echo array_keys($firstValue, $firstValue['article_title'])[0].': '.$firstValue['article_title'].'</br>';
//}

    //retrieve index of match in array
    $foundAtPos = array_search($linkID, array_column($datas, 'article_id'));

    print_r($datas[$foundAtPos]);


} else {
    header('Location: index.php');
    exit();
}

thanks!

1
  • 1
    try echo $datas[$foundAtPos]['article_title']; Commented Dec 27, 2018 at 2:18

2 Answers 2

1

You can simplify this a lot by just using array_column with null as the $column_key argument.

This will re-index the entire array with the article_id.

Array
(
[1] => Array
    (
        [article_id] => 1
        [article_title] => Article 1
        [article_content] => This is some dummy text for the first article.
    )

[2] => Array
    (
        [article_id] => 2
        [article_title] => Article 2
        [article_content] => This is the content for article #2
    )
)

Then you can use isset on the $linkID of the resulting array.

Example: https://3v4l.org/Fmj9K

if (isset($_GET['id'])) {
    $linkID = $_GET['id'];

    $articles = array_column($datas, null, 'article_id');
    if (isset($articles[$linkID])) {
        $article = $articles[$linkID];
        //optionally convert from an array values to variable values.
        $article_title = $article['article_title'];
        $article_content = $article['article_content'];
    }
}

Or in PHP 7.1+ you can use list with the key names

Example: https://3v4l.org/C2c4K

if (isset($_GET['id'])) {
    $linkID = $_GET['id'];

    $articles = array_column($datas, null, 'article_id');
    if (isset($articles[$linkID])) {
        list('article_title' => $title, 'article_content' => $content) = $articles[$linkID];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice one, I always forget about the third argument to array_column()
Wow, how awesome. While I don't fully understand it yet, your answer will definitely aid in my php learning. Thanks so much for this! :)
0

If you already know the ID then just loop on that sub-array:

$foundAtPos = array_search($linkID, array_column($datas, 'article_id'));
foreach ($datas[$foundAtPos] as $key => $value) {
    echo $key . ': '. $value;
}

or if you want to get a specific key/value pair at some index then you can use the bracket operator:

$foundAtPos = array_search($linkID, array_column($datas, 'article_id'));
echo $datas[$foundAtPos]['article_title'];

2 Comments

Well, I'm not even sure a loop is entirely necessary, it's just I haven't figured out how to get at a specific key/value at a specific index in the array.
so your suggestion gets me the correct row, and produes the following output..article_id: 2article_title: Article 2article_content: This is the content for article #2 (this is what I expect to get), however what I need to do is break this up and get only the article_title or article_content for this row, and I'm stumped as how to do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.