0

i have and array like this

Array
(
    [ID] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [Ticket_ID] => Array
        (
            [0] => e
            [1] => f
            [2] => g
            [3] => h
        )

    [Status] => Array
        (
            [0] => i
            [1] => j
            [2] => k
            [3] => l
        )
)

and i want it like this

Array
(
    [0] => Array
        (
            [ID] => a
            [Ticket_ID] => e
            [Status] => i
        )

    [1] => Array
        (
            [ID] => b
            [Ticket_ID] => f
            [Status] => j
        )

    [2] => Array
        (
            [ID] => c
            [Ticket_ID] => g
            [Status] => k
        )
    [3] => Array
        (
            [ID] => d
            [Ticket_ID] => h
            [Status] => l
        )
)

This is my code so far:

foreach($array as $k => $v) { 
  if($k = 'ns0:Request_ID') { 
      foreach($v as $kk => $vv) { 
          array_push($sd ,$vv); 
      } 
  } 
  break; 
 }
8
  • What have you attempted so far? Just create a for loop based on one array's length and populate a new array using the index Commented Jun 7, 2021 at 19:06
  • is it that, every array i.e. ID, Ticket ID and and Status will have equal no. of items ?? Commented Jun 7, 2021 at 19:07
  • 1
    Exactly, post your solution? Attempt? Commented Jun 7, 2021 at 19:07
  • foreach($array as $k => $v) { if($k = 'ns0:Request_ID') { foreach($v as $kk => $vv) { array_push($sd ,$vv); } } break; } Commented Jun 7, 2021 at 19:08
  • no there values may increase or decrease Commented Jun 7, 2021 at 19:09

1 Answer 1

1
//assuming initial data is in variable $data
$finalArray = [];

//assume that each "record" has an "ID" and as such the number of "ID" items
//will determine the number of records
if(isset($data['ID']) && is_array($data['ID'])){
    //determine number of records/items based on number of id elements
    $noOfItems = count($data['ID']);

    //foreach item check if an element exists in the respective
    //"Ticket_ID" and "Status" arrays based on the current index "$i"
    //Checks were included to ensure values were arrays and indexes were
    //present before use usinf `isset` and `is_array`. 
    // if a value did not exist for this index `null` was assigned.

    for($i=0;$i<$noOfItems;$i++){
         array_push($finalArray,[
             'ID'=>$data['ID'][$i],
             'Ticket_ID'=> ( 
                              isset($data['Ticket_ID']) &&  
                              is_array($data['Ticket_ID']) &&
                              isset($data['Ticket_ID'][$i])
                           ) ? $data['Ticket_ID'][$i] : null,
             'Status'=> ( 
                              isset($data['Status']) &&  
                              is_array($data['Status']) &&
                              isset($data['Status'][$i])
                           ) ? $data['Status'][$i] : null
         ]);
    }
}
//final results are in $finalArray


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

2 Comments

You could shorten your array building to things like 'Ticket_ID'=> $data['Ticket_ID'][$i] ?? null
its solved thanks for the solution and we can add multiple values here too Great..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.