1

I have an generated array into this format and want to generated a second array to fit into a file that expects the specific format

This is the array i have :

(int) 0 => array(
        [Service] => Array
        (
            [id] => 6948229
            [document] => Array
                (
                    [number] => 0003928425
                )
        )

This is the array i want to build from the previous array (will have many indexes)

verified[id]
verified[number]

So far i build this script:

foreach($data as $key=>$value )
        {
            echo '<br>key '.$key;
            foreach($value as $k=>$v)
            {
                $Verified[$key]['id'] = $v["id"];
                $Verified[$key]['number'] = $v['document']['number'];

But just get undefined index error message.

Which indexes i must use to get the flatten array ?

4
  • Try using echo 'ID: '.$service['id']; and echo 'Number: '.$service['document']['number']; Commented Mar 16, 2016 at 12:38
  • I think there is no need of second foreach try your code removing second foreach. Commented Mar 16, 2016 at 12:44
  • Sorry i update the array format it starts on the index 0 and so on : (int) 0 => array( [Service] => Array ( Commented Mar 16, 2016 at 12:53
  • The message is undefined variable for echo 'ID: '.$service['id']; Commented Mar 16, 2016 at 12:59

3 Answers 3

1

From what I can make from your question, you can do something like this to get the desired output,

    $Verified = []; //use array() for versions below 5.5
    foreach($data as $key=>$value )
            {
                echo '<br>key '.$key;
                foreach($value as $k=>$v)
                {   
                    if(is_array($v)){
                        $Verified[$key]['number'] = $v['document']['number'];
                    }
                    $Verified[$key]['id'] = $v['id'];
Sign up to request clarification or add additional context in comments.

2 Comments

are there more elements in the array and does the 'document' array structure change in other elements of the array? where exactly are you getting undefined index..can you post it here please.
Now it works i have more indexes other than service so i add if($k=='Service') and it works, now i have a flatten array ready to return from the method. Thank you a lot!
1

Please pass your array to this function

function arrayconvert($arr) {
  if (is_array($arr)) {
    foreach($arr as $k => $v) {
      if (is_array($v)) {
        arrayconvert($v);
      } else {
        $newarr[$k] = $v;
      }
    }
  }

  return $newarr;
}

Comments

1

There is no need of second foreach and you are getting undefined index because you are using $v['id'] insted of $val['id'] in that line $Verified[$key]['id'] = $v["id"];

<?php 
$data = array('Service' => array('id' => 6948229,'document' => array ('number' => '0003928425' )));
$verified = array();
foreach($data as $key => $val)
{ 
    $verified[$key]['id'] = $val['id'];     
    $verified[$key]['number'] = $val['document']['number'];     
}
echo "<pre>"; print_r($verified);
?>

output

Array
(
    [Service] => Array
        (
            [id] => 6948229
            [number] => 0003928425
        )

)

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.