找到需要的第一个元素

发布于 2024-11-15 21:08:06 字数 1386 浏览 1 评论 0原文

我有这个数组

Array
(
    [name] => Step1 is here
    [standard] => Array
        (
            [product_id] => 85,99
            [product_name] => Step1 is here
            [product_price] => 976.0000
            [product_description] => :something 
            [product_image] => http://someurl.com/shop_pos/image/data/13D.png
        )

    [professional] => Array
        (
            [product_id] => 61
            [product_name] => Step1 is here
            [product_price] => 289.0000
            [product_description] => somethingothere 
            [product_image] => http://someurl.com/shop_pos/image/data/13B.png
        )
    [premium] => Array
        (
            [product_id] => 677
            [product_name] => Step1 is here
            [product_price] => 289.0000
            [product_description] => somethingothere 
            [product_image] => http://someurl.com/shop_pos/image/data/13A.png
        )

)

是否可以轻松地按我需要的正确顺序进行引用。所以我需要的订单是标准的、专业的、高级的..所以如果其中一个不存在,我可以像这样对另一个进行操作吗?

 if (!isset($my_array['standard'])) {
 $use_me = $my_array['standard']
}elseif(!isset($my_array['professional'])) {
 $use_me = $my_array['professional']
}elseif(!isset($my_array['professional'])) {
    $use_me = $my_array['premium']}
}

我有上面的代码,我认为可以工作,但有更好的方法吗

I have this array

Array
(
    [name] => Step1 is here
    [standard] => Array
        (
            [product_id] => 85,99
            [product_name] => Step1 is here
            [product_price] => 976.0000
            [product_description] => :something 
            [product_image] => http://someurl.com/shop_pos/image/data/13D.png
        )

    [professional] => Array
        (
            [product_id] => 61
            [product_name] => Step1 is here
            [product_price] => 289.0000
            [product_description] => somethingothere 
            [product_image] => http://someurl.com/shop_pos/image/data/13B.png
        )
    [premium] => Array
        (
            [product_id] => 677
            [product_name] => Step1 is here
            [product_price] => 289.0000
            [product_description] => somethingothere 
            [product_image] => http://someurl.com/shop_pos/image/data/13A.png
        )

)

Is there an easy of referencing in the proper order that i need. SO the order i need is standard, professional, premium.. so if one is not present can I do to the other like this

 if (!isset($my_array['standard'])) {
 $use_me = $my_array['standard']
}elseif(!isset($my_array['professional'])) {
 $use_me = $my_array['professional']
}elseif(!isset($my_array['professional'])) {
    $use_me = $my_array['premium']}
}

i have the above code that i think may work but is there a better way

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

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

发布评论

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

评论(5

Bonjour°[大白 2024-11-22 21:08:06

应该可以做到:

$keys = array_slice(array_keys($my_array), 1, 1);
$use_me = $my_array[$keys[0]];

简而言之:

  1. 获取数组键(名称、标准、专业、高级)。 (array_keys)
  2. 获取第二个键(绕过 name,因此它返回 standard 或第二个键是什么)。 (array_slice)
  3. 使用该键引用 $my_array 并将其存储在 $use_me 中。

That should do it:

$keys = array_slice(array_keys($my_array), 1, 1);
$use_me = $my_array[$keys[0]];

In short:

  1. Get the array keys (name, standard, professional, premium). (array_keys)
  2. Get the second key (bypassing name, so it returns standard or whatever the second key is). (array_slice)
  3. Reference $my_array using that key and store it in $use_me.
左秋 2024-11-22 21:08:06

我会这样做:

foreach (array('standard', 'professional', 'premium') as $name) {
    if (isset($my_array[$name])) {
        $use_me = $my_array[$name];
        break;
    }
}

或者更结构化一点:

function selectPlan($array) {
    foreach (array('standard', 'professional', 'premium') as $name) {
        if (isset($array[$name])) return $array[$name];
    }
}

$use_me = selectPlan($my_array);

I would do this:

foreach (array('standard', 'professional', 'premium') as $name) {
    if (isset($my_array[$name])) {
        $use_me = $my_array[$name];
        break;
    }
}

or a little more structured:

function selectPlan($array) {
    foreach (array('standard', 'professional', 'premium') as $name) {
        if (isset($array[$name])) return $array[$name];
    }
}

$use_me = selectPlan($my_array);
回忆凄美了谁 2024-11-22 21:08:06

是的,您可以按照代码中所示的方式进行操作。但是,您的 if 语句逻辑不正确。你的代码应该是这样的:

if (isset($my_array['standard'])) {
    $use_me = $my_array['standard'];
} elseif(isset($my_array['professional'])) {
    $use_me = $my_array['professional'];
} elseif(isset($my_array['premium'])) {
    $use_me = $my_array['premium'];
}

Yes, you can do it the way you have presented in your code. However, your if statement logic is not correct. Your code should be like this:

if (isset($my_array['standard'])) {
    $use_me = $my_array['standard'];
} elseif(isset($my_array['professional'])) {
    $use_me = $my_array['professional'];
} elseif(isset($my_array['premium'])) {
    $use_me = $my_array['premium'];
}
厌味 2024-11-22 21:08:06

尝试使用 array_key_exists

foreach($input as $k=>$v) {

if (array_key_exists('standard', $k)) {
$output [$k] = $k;
}
if (array_key_exists('professional', $k)) {
$output [$k] = $k;
}
if (array_key_exists('premium', $k)) {
$output [$k] = $k;
} 

}

或 u可以通过 array-intersect-key

try with array_key_exists

foreach($input as $k=>$v) {

if (array_key_exists('standard', $k)) {
$output [$k] = $k;
}
if (array_key_exists('professional', $k)) {
$output [$k] = $k;
}
if (array_key_exists('premium', $k)) {
$output [$k] = $k;
} 

}

or u can go through array-intersect-key

妄司 2024-11-22 21:08:06

如果我正确理解您想要执行的操作,并且如果不能保证键的顺序与示例中的顺序完全相同,您可以执行以下操作:

// initialization
$use_me = 'default value';

// go through each package
foreach (array('standard', 'professional', 'premium') as $package) {
    // when we find a package that does exist
    if (isset($my_array[$package])) {
        // mark it as found and exit the loop
        $use_me = $package;
        break;
    }
}

这将遍历所有包并设置 $ use_me 变量到第一个找到的值。如果未找到值,则将其设置为默认值。

If I understand correctly what you want to do, and if the order of the keys is not guaranteed to be exactly like the one in the example, you can do something like:

// initialization
$use_me = 'default value';

// go through each package
foreach (array('standard', 'professional', 'premium') as $package) {
    // when we find a package that does exist
    if (isset($my_array[$package])) {
        // mark it as found and exit the loop
        $use_me = $package;
        break;
    }
}

This will go through all the packages and set the $use_me variable to the first found value. If no value is found, it sets it to a default value.

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