Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP数组父项移动为子项_Php_Arrays - Fatal编程技术网

将PHP数组父项移动为子项

将PHP数组父项移动为子项,php,arrays,Php,Arrays,我有一个多维PHP数组,如下所示: Array( [id] => lO2riJGekyHmjvFbASTa [word] => abc [product_id] => 1 [variation] => Array( [attribute_1] => attribute-1 [attribute_2] => attribute-2 [attribute_3] => attribute-

我有一个多维PHP数组,如下所示:

  Array(
    [id] => lO2riJGekyHmjvFbASTa
    [word] => abc
    [product_id] => 1
    [variation] => Array(
      [attribute_1] => attribute-1
      [attribute_2] => attribute-2
      [attribute_3] => attribute-3
    )
  )
我喜欢的是将前两个项“id”和“word”移动为“variation”项的前两个子项,因此数组如下所示:

  Array(
    [product_id] => 1
    [variation] => Array(
      [id] => lO2riJGekyHmjvFbASTa
      [word] => abc
      [attribute_1] => attribute-1
      [attribute_2] => attribute-2
      [attribute_3] => attribute-3
    )
  )
我怎样才能做到这一点?
非常感谢

简单,只需将它们分配给
变体
,然后
取消设置
原始:

$array['variation']['id'] = $array['id'];
$array['variation']['word'] = $array['word'];
unset($array['id'], $array['word']);

简单地说,只需将它们分配给
变体
,然后
取消设置
原件:

$array['variation']['id'] = $array['id'];
$array['variation']['word'] = $array['word'];
unset($array['id'], $array['word']);

我也看到了上面Abracadver给出的简短回答,应该已经对你有所帮助了。如果您不想更改数组位置,请按照以下代码执行一次并重试

<?php

$arr = array("id" => "lO2riJGekyHmjvFbASTa",
    "word" => "abc",
    "product_id" => "1",
  'variation' => array('attribute_1' => "attribute-1",'attribute_2' => "attribute-2",'attribute_3' => "attribute-3")
);

foreach($arr as $id => $val){
    if($id == "word"){
        array_unshift($arr['variation'], $arr['word']);
    }
}

foreach($arr as $id => $val){
    if($id == "id"){
        array_unshift($arr['variation'], $arr['id']);
    }
}


function replaceArrayKey($array, $oldKey, $newKey){
    //If the old key doesn't exist, we can't replace it...
    if(!isset($array[$oldKey])){
        return $array;
    }
    //Get a list of all keys in the array.
    $arrayKeys = array_keys($array);
    //Replace the key in our $arrayKeys array.
    $oldKeyIndex = array_search($oldKey, $arrayKeys);
    $arrayKeys[$oldKeyIndex] = $newKey;
    //Combine them back into one array.
    $newArray =  array_combine($arrayKeys, $array);
    return $newArray;
}
$variationNew = replaceArrayKey($arr['variation'], 0, 'id');
$variationNew = replaceArrayKey($variationNew, 1, 'word');


unset($arr['id']);
unset($arr['word']);

$arr['variation'] = $variationNew;
echo "<pre>"; print_r($arr);


?>

我还看到了上面Abracadver给出的简短回答,应该已经对您有所帮助了。如果您不想更改数组位置,请按照以下代码执行一次并重试

<?php

$arr = array("id" => "lO2riJGekyHmjvFbASTa",
    "word" => "abc",
    "product_id" => "1",
  'variation' => array('attribute_1' => "attribute-1",'attribute_2' => "attribute-2",'attribute_3' => "attribute-3")
);

foreach($arr as $id => $val){
    if($id == "word"){
        array_unshift($arr['variation'], $arr['word']);
    }
}

foreach($arr as $id => $val){
    if($id == "id"){
        array_unshift($arr['variation'], $arr['id']);
    }
}


function replaceArrayKey($array, $oldKey, $newKey){
    //If the old key doesn't exist, we can't replace it...
    if(!isset($array[$oldKey])){
        return $array;
    }
    //Get a list of all keys in the array.
    $arrayKeys = array_keys($array);
    //Replace the key in our $arrayKeys array.
    $oldKeyIndex = array_search($oldKey, $arrayKeys);
    $arrayKeys[$oldKeyIndex] = $newKey;
    //Combine them back into one array.
    $newArray =  array_combine($arrayKeys, $array);
    return $newArray;
}
$variationNew = replaceArrayKey($arr['variation'], 0, 'id');
$variationNew = replaceArrayKey($variationNew, 1, 'word');


unset($arr['id']);
unset($arr['word']);

$arr['variation'] = $variationNew;
echo "<pre>"; print_r($arr);


?>


如果我理解,您想创建另一个数组,如您所监视的第二个数组!??如果我明白的话,你想创建另一个数组,就像你上次梦到的第二个一样!??