如何在数组输出中的下划线之前抓取一个单词

发布于 2024-12-15 14:02:35 字数 426 浏览 0 评论 0原文

这是我当前的数组,它保留值中的第一个字符

Array ( [n] => Array ( [0] => name_john ) [a] => Array ( [0] => age_30 ) )

foreach ($queryArray as $key) {
    $inArray[$key[0]][] = $key;
}

问题:如何在下划线之前抓取一个单词并将数组更改为类似

Array
(
    [name] => Array
        (
            [0] => name_john
        )

    [age] => Array
        (
            [0] => age_30
        )

)

This is my current array which keep a first character from the value

Array ( [n] => Array ( [0] => name_john ) [a] => Array ( [0] => age_30 ) )

foreach ($queryArray as $key) {
    $inArray[$key[0]][] = $key;
}

Question : How do I grab a word before underscore and change the array to be like

Array
(
    [name] => Array
        (
            [0] => name_john
        )

    [age] => Array
        (
            [0] => age_30
        )

)

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

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

发布评论

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

评论(5

紫竹語嫣☆ 2024-12-22 14:02:35

要解析下划线之前的单词,请使用explode:

// $key = name_john
$split_key = explode('_',$key);
// $split_key[0] equals name

To parse out the word before the underscore, use explode:

// $key = name_john
$split_key = explode('_',$key);
// $split_key[0] equals name
与之呼应 2024-12-22 14:02:35
$pieces = explode("_", $key);
echo $pieces[0];
$pieces = explode("_", $key);
echo $pieces[0];
杀お生予夺 2024-12-22 14:02:35
  • 要拆分值,请使用 explode('_', $value) 并获取返回数组的第一项。
  • 要修改 foreach 中的数组,请在数组上添加引用
  • 要更改数组中元素的键,请检查此线程: 在 PHP 中,如何更改数组元素的键?

工作示例: ( <一href="http://codepad.org/tmSJEUmH" rel="nofollow noreferrer">此处为键盘)

<?php

$array = array('n' => Array(0 => 'name_john'), 'a' => Array('0' => 'age_30'));

foreach ($array as &$value) {
    foreach ($value as $oldkey => $name) {
        $chunks = explode('_', $name);
        $newkey = $chunks[0];
        // see https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element
        $value[$newkey] = $value[$oldkey];
        unset($value[$oldkey]);
    }
}
var_dump($array);

输出:

array
  'n' => 
    array
      'name' => string 'name_john' (length=9)
  'a' => &
    array
      'age' => string 'age_30' (length=6)

Working example: (codepad here)

<?php

$array = array('n' => Array(0 => 'name_john'), 'a' => Array('0' => 'age_30'));

foreach ($array as &$value) {
    foreach ($value as $oldkey => $name) {
        $chunks = explode('_', $name);
        $newkey = $chunks[0];
        // see https://stackoverflow.com/questions/240660/in-php-how-do-you-change-the-key-of-an-array-element
        $value[$newkey] = $value[$oldkey];
        unset($value[$oldkey]);
    }
}
var_dump($array);

Output:

array
  'n' => 
    array
      'name' => string 'name_john' (length=9)
  'a' => &
    array
      'age' => string 'age_30' (length=6)
暖树树初阳… 2024-12-22 14:02:35
$inArray[$key[0]][] = substr($key, 0, strpos($key, '_'));

但是如果你使用“key”作为关联来创建数组。索引,就像

Array ( [n] => Array ( ["name"] => john ) [a] => Array ( ["age"] => 30 ) )

然后你可以

foreach ($queryArray as $subArr) {
    foreach($subArr as $subkey => $val){
        $inArray[$subkey][] = $val;
    }
}

并且你应该得到一个像

Array
(
    ["name"] => "john"

    ["age"] => 30
)

这样的数组虽然它不是“所需的”输出,但对于你正在做的事情来说可能更干净,更容易使用。

$inArray[$key[0]][] = substr($key, 0, strpos($key, '_'));

But if you make the array using the "key" as the assoc. index, like

Array ( [n] => Array ( ["name"] => john ) [a] => Array ( ["age"] => 30 ) )

Then you can

foreach ($queryArray as $subArr) {
    foreach($subArr as $subkey => $val){
        $inArray[$subkey][] = $val;
    }
}

and you should get an array like

Array
(
    ["name"] => "john"

    ["age"] => 30
)

Tho its not the "desired" output, might be cleaner and easier to use for what it seems like you're doing.

情域 2024-12-22 14:02:35

尝试将其放入您的 foreach 语句中:)

$string = strstr($key, '_', true); 
echo $string; // prints ewverything before the '_'

Try put this inside your foreach statement :)

$string = strstr($key, '_', true); 
echo $string; // prints ewverything before the '_'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文