动态地从数组中提取某些键

时间:2019-10-18 20:07:14

标签: php cakephp-3.0

我只需要动态地从数组中提取某些键。

例如:

$array = [
        "a" => true,
        "b" => false,
        "c" => [
            0 => [
                "a" => "x",
                "b" => [
                    "x" => "Y"
                ]
            ],
            1 => [
                "a" => "x",
                "b" => [
                    "x" => "Y"
                ]
            ],
        ]
    ];

然后我需要提取以下可以更改的数组:

$newArray = [
        "b" => false,
        "c" => [
            0 => [
                "b" => [
                    "x" => "Y"
                ]
            ],
            1 => [
                "b" => [
                    "x" => "Y"
                ]
            ],
        ]
    ];

2 个答案:

答案 0 :(得分:0)

使用以下递归函数可能很容易:

<?php
$array = [
            "a" => true,
            "b" => false,
            "c" => [
                0 => [
                    "a" => "x",
                    "b" => [
                        "x" => "Y"
                    ]
                ],
                1 => [
                    "a" => "x",
                    "b" => [
                        "x" => "Y"
                    ]
                ],
            ]
        ];

$cleaned_array = unset_by_key($array,"a");    
$extracted_array = arrayRecursiveDiff ($array, $cleaned_array);

print_r($cleaned_array);  
print_r($extracted_array);

function unset_by_key ($arr,$nkey) {
  unset($arr[$nkey]);
  foreach ($arr as $key=>$val) {
    if (is_array($val)) {
        $arr[$key] = unset_by_key($val,$nkey);
    }
  }
  return $arr;
}

// THX TO: https://stackoverflow.com/questions/3876435/recursive-array-diff
function arrayRecursiveDiff($aArray1, $aArray2) {
  $aReturn = array();

  foreach ($aArray1 as $mKey => $mValue) {
    if (array_key_exists($mKey, $aArray2)) {
      if (is_array($mValue)) {
        $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
        if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
      } else {
        if ($mValue != $aArray2[$mKey]) {
          $aReturn[$mKey] = $mValue;
        }
      }
    } else {
      $aReturn[$mKey] = $mValue;
    }
  }
  return $aReturn;
} 

?>

答案 1 :(得分:0)

不需要递归函数,如果只打算通过第一级按键选择深度,则可以使用//package org.harrix.sqliteexample; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.content.Context; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.File; public class DatabaseHelper extends SQLiteOpenHelper { private static String DB_NAME = "info.db"; private static String DB_PATH = ""; private static final int DB_VERSION = 1; private SQLiteDatabase mDataBase; private final Context mContext; private boolean mNeedUpdate = false; public DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); if (android.os.Build.VERSION.SDK_INT >= 17) DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; else DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; this.mContext = context; copyDataBase(); this.getReadableDatabase(); }

array_intersect_key

希望有帮助,