English 中文(简体)
PHP - How to appendome with some conditions
原标题:PHP - How to append array with some conditions
  • 时间:2012-04-16 11:23:03
  •  标签:
  • php
  • arrays
$list = array(
               [0]=> array(
                            [name]=> James  
                            [group]=>  
                          )
               [1]=> array(
                            [name]=> Bobby  
                            [group]=>  
                          )
             )

我期待着更新博比的名字的项目组。 我期待着以以下两种方式解决问题。 事先感谢你的答复。 乘客。 Marc.

array_push($list, ???)

以及

$list[] ??? = someting
最佳回答
问题回答

该法典可帮助您:

$listSize = count($list);

for( $i = 0; $i < $listSize; ++$i ) {
    if( $list[$i][ name ] ==  Bobby  ) {
        $list[$i][ group ] =  Hai ;
    }
}

array_push(>>>tn t really与更新价值有关,它只会给一个阵列增加另一个价值。

你们无法找到既适合这两种方式的解决办法。 暗中阵列的推导号$var[]是一个合成构造,你不能发明新的构体——当然不是在PHP,也不是大多数(所有)其他语文。

除此之外,你正在做的是而不是把一个项目推向阵列。 一种情况是,推进项目意味着一个指数化阵列(约会是一种联系),而另一个推论意味着增加阵列的关键(你想要更新的关键已经存在)。

You can write a function to do it, something like this:

function array_update(&$array, $newData, $where = array(), $strict = FALSE) {
  // Check input vars are arrays
  if (!is_array($array) || !is_array($newData) || !is_array($where)) return FALSE;
  $updated = 0;
  foreach ($array as &$item) { // Loop main array
    foreach ($where as $key => $val) { // Loop condition array and compare with current item
      if (!isset($item[$key]) || (!$strict && $item[$key] != $val) || ($strict && $item[$key] !== $val)) {
        continue 2; // if item is not a match, skip to the next one
      }
    }
    // If we get this far, item should be updated
    $item = array_merge($item, $newData);
    $updated++;
  }
  return $updated;
}

// Usage
$newData = array(
   group  =>  ??? 
);
$where = array(
   name  =>  Bobby 
);

array_update($list, $newData, $where);

// Input $array and $newData array are required, $where array can be omitted to
// update all items in $array. Supply TRUE to the forth argument to force strict
// typed comparisons when looking for item(s) to update. Multiple keys can be
// supplied in $where to match more than one condition.

// Returns the number of items in the input array that were modified, or FALSE on error.




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签