0

I Would Like To Get The First Element Of This Array And Put In New Same Array Output

One Requirement: It Cannot Be Done With Passing By reference Index eg 0

This Input Array

        [ 'id','ID','dt-text' ] ,
        [ 'name','Name','dt-text' ] ,
        [ 'artistList'=>['list','mm','defalut']  ,'Artist List','dt-select'] ,
        [ 'nationality'=>['nationality','mm','defalut']  ,'Nationality','dt-select'] ,
        [ 'view','View',''],
        [ 'status','Status' ,'']

array:6 [▼
  0 => array:3 [▼
        0 => "id"
        1 => "ID"
        2 => "dt-text"
  ]
  1 => array:3 [▼
        0 => "name"
        1 => "Name"
        2 => "dt-text"
  ]
  2 => array:3 [▼
        "artistList" => array:3 [▼
              0 => "list"
              1 => "mm"
              2 => "defalut"
        ]
        0 => "Artist List"
        1 => "dt-select"
  ]
  3 => array:3 [▼
        "nationality" => array:3 [▼
              0 => "nationality"
              1 => "mm"
              2 => "defalut"
        ]
        0 => "Nationality"
        1 => "dt-select"
  ]
  4 => array:3 [▼
        0 => "view"
        1 => "View"
        2 => ""
  ]
  5 => array:3 [▼
        0 => "status"
        1 => "Status"
        2 => ""
  ]
]

The New Array I Needed

This IS OutPUT Array

['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status']
array:6 [▼
    0 => "id"
    1 => "name"
    "artistList" => array:3 [▼
                0 => "list"
                1 => "mm"
                2 => "defalut"
                ]
    "nationality" => array:3 [▼
                0 => "nationality"
                1 => "mm"
                2 => "defalut"
                ]
    2 => "view"
    3 => "status"
]

Note I Can Controll in Input Array Same , I Try with foreach in php And Tray In Laravel Helper Function head Put I get S

array:6 [▼
  0 => "id"
  1 => "name"
  2 => array:1 [▼
    "artistList" => array:3 [▼
      0 => "list"
      1 => "mm"
      2 => "defalut"
    ]
  ]
  3 => array:1 [▼
    "nationality" => array:3 [▼
      0 => "nationality"
      1 => "mm"
      2 => "defalut"
    ]
  ]
  4 => "view"
  5 => "status"
]

Put I Cant Get Resslut So , How Can I Do this?

5
  • I don't completely understand your requirement. Do you want a new array as output, or overwrite the old array. Why can't you pass by reference? Can you use reference in the foreach? Commented May 27, 2019 at 2:53
  • yes i want array same output ['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status'] "reference" i mean in some array number 0 is associative i hope you understand me i am soory for bad english Commented May 27, 2019 at 2:58
  • Your shift button is being excessively used. It becomes hard to read Commented May 27, 2019 at 3:10
  • @Andreas I Am Edit Text Commented May 27, 2019 at 3:14
  • @Nick I need reslut same as ['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status'] array:6 [▼ 0 => "id" 1 => "name" "artistList" => array:3 [▼ 0 => "list" 1 => "mm" 2 => "defalut" ] "nationality" => array:3 [▼ 0 => "nationality" 1 => "mm" 2 => "defalut" ] 2 => "view" 3 => "status" ] Commented May 27, 2019 at 3:17

1 Answer 1

2

Since you are changing the keys (structure) of the array, there is no way to do that without either generating a new array or passing the array by reference. One way to do it by generating a new array is with array_reduce:

$array = [
        [ 'id','ID','dt-text' ] ,
        [ 'name','Name','dt-text' ] ,
        [ 'artistList'=>['list','mm','defalut']  ,'Artist List','dt-select'] ,
        [ 'nationality'=>['nationality','mm','defalut']  ,'Nationality','dt-select'] ,
        [ 'view','View',''],
        [ 'status','Status' ,'']
    ];
$array = array_reduce($array, function ($c, $v) { 
    $first_key = array_keys($v)[0];
    return array_merge($c, array($first_key => $v[$first_key])); }, []);
print_r($array);

Output:

Array (
  [0] => id
  [1] => name
  [artistList] => Array (
    [0] => list
    [1] => mm
    [2] => defalut
  )
  [nationality] => Array (
    [0] => nationality
    [1] => mm
    [2] => defalut
  )
  [2] => view
  [3] => status
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.