0

I'm trying to find and output the value from a specific key.

 $name = 'G';
 $ids = array();

foreach($arrayChart as $key){

$foundAtPosition = strpos($key['Name'], $name);
if ($foundAtPosition === false ||
    $foundAtPosition > 0) {
    continue;
}   

  $ids[] = $key['ID'];
}
echo join(',', $ids) . "\n";

Here is the array

$arrayChart = array(
array(
 "Name" => "Mike G",
 "ID" => "0001234",
 "Hours" => 38
 ),
array(
  "Name" => "Mike L",
  "ID" => "0005678",
  "Hours" => 42
 )
 array(
  "Name" => "John G",
  "ID" => "0003615",
  "Hours" => 42
 ) 
);

This code above was provided by @zedfoxus He helped me with a previous problem.

I want to find the ID number from the key value ID by using the last name letter "G" from key Name. I want to output them in the same line. The problem with the above function is that it doesn't print anything.

Joe
  • 83
  • 9

1 Answers1

0

Please see the answer below.

<?php

$arrayChart = array(
array(
 "Name" => "Mike G",
 "ID" => "0001234",
 "Hours" => 38
 ),
array(
  "Name" => "Mike L",
  "ID" => "0005678",
  "Hours" => 42
 ),
 array(
  "Name" => "John G",
  "ID" => "0003615",
  "Hours" => 42
 ) 
 );
 
 $name = 'G';
 $ids = array();

foreach($arrayChart as $key){

$foundAtPosition = strpos($key['Name'], $name);
if ($foundAtPosition) {
    $ids[] = $key['ID'];
}   
}
echo join(',', $ids) . "\n";
?>
Roby Raju Oommen
  • 343
  • 2
  • 10
  • This does not target the G in the last name. This targets a G anywhere after the first character. – mickmackusa Oct 24 '20 at 04:05
  • This answer is missing its educational explanation. You might notice that the OP is not making any attempt to understand the guidance provided in the previous question. This question merely transfers the last question's answer and uses new sample data. This is a "requirements dump" with no personal toil or attempt to research. By posting a code-only answer, you are perpetuating Help Vampirism. – mickmackusa Oct 24 '20 at 04:18
  • 1
    I'm understood. It doesn't answer my question perfectly. I was only searching for an answers to print my results. Is not that I will post this question again to get the answer. This is enough for me to figure it out myself.. – Joe Oct 24 '20 at 04:23
  • 1
    Use the following one if you want to match only the lastname: $namevalue = $key['Name']; $fullnamearray = explode(" ",$namevalue); $lastname = $fullnamearray[1]; $foundAtPosition = strpos($lastname, $name); – Roby Raju Oommen Oct 24 '20 at 04:28