0

I have an array of objects of this form,

[{value => 1, name => "Ha"},
{value => 2, name => "Hi"},
{value => 3, name => "Ho"},
.....]

and a function that should return the value given a certain name,

the function is:

public function get_value_from_name($myArray, $name){
$arrays = $myArray;

    foreach ($arrays as $arr){
    if ($name == $arr->name){
        return $arr->value;
      }
    }
    return false;
    }
 }

when I use the function and pass it the array and a string this way get_value_from_name($myArray, "Hi") I expect getting a 2 but it returns false, I tried tracing the results, the foreach loops through the whole array but I think that $arr->name doesn't give anything, I am not sure. Can you check if something is wrong with my function, I am new to PHP.

Thanks in advance.

EDIT ********** UPDATE: That's what I get when debugging

public function get_value_from_name($arr, $names){ //$arr: {[2], [2], [2], [2] + 72 more} $names: "Hi"

$arrays = $arr; //{ $arrays: {[2], [2], [2], [2] + 72 more}
  foreach ( $arrays as $array ){ // $arrays: {[2], [2], [2], [2] + 72 more} $array: {value => 79, name => "HiHiHiHiHiHi"}[2]
     if ( $array->name ==  $names) //{$names: "Hi"
     return $array->value; 
     }
  }
return false;
}

My Solution : I managed to make it work this way, But can someone explain why the first function did not work,

public function get_id_from_name($arr, $names){
$arrays = $arr;

$nameArray = array_column($arrays, 'name');
$valueArray = array_column($arrays, 'value');

 foreach (array_combine($valueArray, $nameArray) as $value=> $name) {
   if ($name == $names) {
   return $value;
 }
}
return false;
}

3 Answers 3

1

You have a mistake in code. Its should:

...    
    foreach ($arrays as $arr){
      if ($name == $arr->name){
        return $arr->value;
      }
    } // this is mistake
    return false;
 ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, the function was: foreach ($arrays as $arr){ if ($name == $arr->name){ return $arr->value; } } return false; I typed the code instead of coying pasting it so should it be working ?
0

You can do it this way,

<?php
$obj = array();
$obj1 = new stdClass();
$obj1->value = 1;
$obj1->name = 'Ha';
$obj2 = new stdClass();
$obj2->value = 2;
$obj2->name = 'Hi';
$obj3 = new stdClass();
$obj3->value = 3;
$obj3->name = 'Ho';
$obj = [$obj1,$obj2,$obj3];

function get_value_from_name($arrays, $name){
    foreach ($arrays as $arr){
     if ($name == $arr->name){
        return $arr->value;
     }
   }
 }

 echo get_value_from_name($obj,'Ho');
?>

DEMO: https://3v4l.org/TkjGe

5 Comments

Even When I take off the return false it's not working and In think it should be working with both, I have added a snip on the results of the debugging if that helps. It is reaching the end of the loop without finding any match
that is because $names='Hi' doesn't match with any of your nested object's name
the value: 26 has a name: 'Hi'
seems your array of object is not same that you are saying, Did you checked my demo, it should work for any length ?
You're missing { after you foreach also. Please check my given snippet of code properly
0

I managed to make it work with this function, can someone explain why the first one didn't work:

public function get_id_from_name($arr, $names){
$arrays = $arr;

$nameArray = array_column($arrays, 'name');
$valueArray = array_column($arrays, 'value');

 foreach (array_combine($valueArray, $nameArray) as $value=> $name) {
   if ($name == $names) {
   return $value;
 }
}
return false;
}

Comments

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.