首页 \ 问答 \ 如何将新键分配给PHP关联数组(How to assign new keys to a PHP associative array)

如何将新键分配给PHP关联数组(How to assign new keys to a PHP associative array)

我有这个PHP创建一个作业数组作为$ jobs。 但是我希望键是[ref]而不是0,1,2等

$xml_feed = new DOMDocument();
$xml_feed->load('http://jobs.recruitsosimple.com/job/762/xml');
$jobs = array();
foreach ($xml_feed->getElementsByTagName('job') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'ref' => $node->getElementsByTagName('referencenumber')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('url')->item(0)->nodeValue,
        );
    array_push($jobs, $item);
}

创建的数组是:

Array
(
    [0] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [1] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

但是我想要钥匙

Array
(
    [MT29] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [MT39] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

我怎样才能做到这一点?


I have this PHP that creates an array of jobs as $jobs. However I'd like the keys to be [ref] rather than 0, 1, 2 etc

$xml_feed = new DOMDocument();
$xml_feed->load('http://jobs.recruitsosimple.com/job/762/xml');
$jobs = array();
foreach ($xml_feed->getElementsByTagName('job') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'ref' => $node->getElementsByTagName('referencenumber')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('url')->item(0)->nodeValue,
        );
    array_push($jobs, $item);
}

The array created is:

Array
(
    [0] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [1] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

But I would like the keys to be

Array
(
    [MT29] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [MT39] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

How can I do this?


原文:https://stackoverflow.com/questions/40439294
更新时间:2019-06-21 02:04

最满意答案

改变这个:

array_push($jobs, $item);

对此:

$jobs[$item['ref']] = $item;

Change this:

array_push($jobs, $item);

to this:

$jobs[$item['ref']] = $item;
2016-11-05

相关问答

更多

如何为关联数组赋值(How to assign a value to an associative array)

public function sendToView($email, $vehicleid) { $data['details'] = array(); $data['details']['email'] = $email; $data['details']['id'] = $vehicleid; $this->load->view('pages/OfferView',$data); //load it into the view } 访问它时,您可以循环并 ...

如何将一个数组值分配给php中的另一个数组[关闭](How to assign one array value to another array in php [closed])

如果您使用的是PHP 5.5,则可以使用array_column() : $new_array = array_column($array, 'file'); If you're using PHP 5.5 you can use array_column(): $new_array = array_column($array, 'file');

在PHP中返回关联数组的第一个键(Return first key of associative array in PHP)

尽管array_shift(array_keys($array)); 将工作, current(array_keys($array)); 速度更快,因为它不会提高内部指针。 任何一个都可以工作。 更新 正如@TomcatExodus指出的那样, array_shift(); 期望通过引用传递数组,所以第一个示例将发出错误。 最好坚持current(); Although array_shift(array_keys($array)); will work, current(array_keys($ ...

不带toString的关联数组等(Associative array without toString, etc)

这会为你工作吗? function getValue(id){ return (!isNaN(aa[id])) ? aa[id] : undefined; } 更新: 在Moss Collum和pottedmeat的帮助下,我推荐这种通用解决方案: function getValue(hash,key) { return Object.prototype.hasOwnProperty.call(hash,key) ? hash[key] : undefined; } Update2 ...

在关联数组中查找最小值的键(Finding the minimum value's key in an associative array)

array_keys是你的朋友: $pets = array( "cats" => 1, "dogs" => 2, "fish" => 3 ); array_keys($pets, min($pets)); # array('cats') PS :这里有一个dup(它有max而不是min ,但我可以清楚地记住它)。 array_keys is your friend: $pets = array( "cats" => 1, "dogs" => 2, ...

PHP fgetcsv生成了关联数组键错误(PHP fgetcsv generated associative array key error)

您可以缩短代码并使其更具可读性,以便更快地找到错误。 function parse_csv($csv_path) { if(is_file($csv_path) && file_exists($csv_path)) { $stream = fopen($csv_path, 'r'); $csvHeader = fgetcsv($stream); $csvData = []; while(!feof($stream)) ...

Smarty关联数组(Smarty Associative array)

根据smarty文档,你应该使用: $smarty->assign( 'lang', $lang); 在模板中: {$lang.city} According to smarty documentation you should use: $smarty->assign( 'lang', $lang); And in template: {$lang.city}

在PHP中删除数组中的黑名单键(Remove blacklist keys from array in PHP)

$out = array_diff_key ($data, array_flip ($bad_keys)); 我所做的只是查看Array函数列表,直到找到我需要的函数 ( _diff_key )。 $out =array_diff_key($data,array_flip($bad_keys)); All I did was look through the list of Array functions until I found the one I needed (_diff_key).

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)