查找一个字符串并获取相关值

发布于 2024-12-05 05:54:36 字数 465 浏览 1 评论 0原文

我有几个字符串,如何搜索第一个值并从中获取其他值?

print_r?

Array( [0] => Title,11,11 [1] => Would,22,22 [2] => Post,55,55 [3] => Ask,66,66 )

例如:

如果发送此数组值 Title 并获取值 Title,11,11< br> 或者发送 Would 获取值 Would,22,22
或者发送 Post 获取值 Post,55,55
或者发送Ask获取值Ask,66,66

怎么办呢?

I have several strings, how can I search the first value and get other values from it?

print_r or ?:

Array( [0] => Title,11,11 [1] => Would,22,22 [2] => Post,55,55 [3] => Ask,66,66 )

like:

If send for this array value Title and getting values Title,11,11
Or send Would getting values Would,22,22
Or send Post getting values Post,55,55
Or send Ask getting values Ask,66,66

How can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绿萝 2024-12-12 05:54:37

假设:

$arr = Array( [0] => Title,11,11 [1] => Would,22,22 [2] => Post,55,55 [3] => Ask,66,66 )
$string = 'Would'; 

然后

//Call the function with the search value in $string and the actual array
$required_arr[$string] = search_my_array($string, $arr);

function($str , $array)
{
    //Trace the complete array
    for($i = 0; $i<count($array); $i++)
    {  
        //Break the array using explode function based on ','
        $arr_values[$i] = explode(',',$array[i])
        if($str == $arr_values[$i][0]) // Match the First String with the required string
        {
            //On match return the array with the values contained in it
            return array($arr_values[$i][1], $arr_values[$i][2]);
        }
    }
}

现在

$required_arr['Would']    // will hold Array([0] => 22 [1] => 22)

suppose:

$arr = Array( [0] => Title,11,11 [1] => Would,22,22 [2] => Post,55,55 [3] => Ask,66,66 )
$string = 'Would'; 

then

//Call the function with the search value in $string and the actual array
$required_arr[$string] = search_my_array($string, $arr);

function($str , $array)
{
    //Trace the complete array
    for($i = 0; $i<count($array); $i++)
    {  
        //Break the array using explode function based on ','
        $arr_values[$i] = explode(',',$array[i])
        if($str == $arr_values[$i][0]) // Match the First String with the required string
        {
            //On match return the array with the values contained in it
            return array($arr_values[$i][1], $arr_values[$i][2]);
        }
    }
}

Now

$required_arr['Would']    // will hold Array([0] => 22 [1] => 22)
初吻给了烟 2024-12-12 05:54:37

编写一个函数来搜索数组。这应该足够好

<?php
  // test array
  $arr = array('Title,11,11','Would,22,22','Post,55,55','Ask,66,66');
  // define search function that you pass an array and a search string to
  function search($needle,$haystack){
    // loop over each passed in array element
    foreach($haystack as $v){
      // if there is a match at the first position
      if(strpos($v,$needle) === 0)
        // return the current array element
        return $v;
    }
    // otherwise retur false if not found
    return false;
  }
  // test the function
  echo search("Would",$arr);
?>

Write a function to search the array. This should work well enough

<?php
  // test array
  $arr = array('Title,11,11','Would,22,22','Post,55,55','Ask,66,66');
  // define search function that you pass an array and a search string to
  function search($needle,$haystack){
    // loop over each passed in array element
    foreach($haystack as $v){
      // if there is a match at the first position
      if(strpos($v,$needle) === 0)
        // return the current array element
        return $v;
    }
    // otherwise retur false if not found
    return false;
  }
  // test the function
  echo search("Would",$arr);
?>
水溶 2024-12-12 05:54:37

指数重要吗?为什么不..

$arr = array(
  'Title' => array(11, 11),
  'Would' => array(22, 22),
  'Post' => array(55, 55),
  'Ask' => array(66,66)
);

$send = "Title";     // for example
$result = $arr[$send];

are the indices important ? why not ..

$arr = array(
  'Title' => array(11, 11),
  'Would' => array(22, 22),
  'Post' => array(55, 55),
  'Ask' => array(66,66)
);

$send = "Title";     // for example
$result = $arr[$send];
半寸时光 2024-12-12 05:54:37

怎么样使用类似的东西,这样你就不会循环整个数组:

$array = array( "Title,11,11", "Would,22,22", "Post,55,55", "Ask,66,66" );
$key = my_array_search('Would', $array);
$getvalues = explode(",", $array[$key]);

function my_array_search($needle = null, $haystack_array = null, $skip = 0)
{
    if($needle == null || $haystack_array == null)
        die('$needle and $haystack_array are mandatory for function my_array_search()');
    foreach($haystack_array as $key => $eval)
    {
        if($skip != 0)$eval = substr($eval, $skip);
        if(stristr($eval, $needle) !== false) return $key;
    }
    return false;
}

How about using something like, so you don't loop trough entire array:

$array = array( "Title,11,11", "Would,22,22", "Post,55,55", "Ask,66,66" );
$key = my_array_search('Would', $array);
$getvalues = explode(",", $array[$key]);

function my_array_search($needle = null, $haystack_array = null, $skip = 0)
{
    if($needle == null || $haystack_array == null)
        die('$needle and $haystack_array are mandatory for function my_array_search()');
    foreach($haystack_array as $key => $eval)
    {
        if($skip != 0)$eval = substr($eval, $skip);
        if(stristr($eval, $needle) !== false) return $key;
    }
    return false;
}
凉宸 2024-12-12 05:54:36

使用 foreach 循环数组,并将值与 strpos

Loop over the array with foreach and match the value with strpos.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文