1

I have tried everything I know and I have failed totally to parse this multi dimensional array so that I can get the keys and values out of both the inner and outer array for saving to a DB.

array:10 [▼
  "_token" => "aHgDik0zuDxxBqYgbi0ZTkaPyG3CVwk5WO7LtU6u"
  "contractor_invoice_no" => ""
  "date" => "26-11-2015"
  "contractor_reference" => "sdfsdf"
  "workorder_reference" => "151 alandale"
  "square_installed" => "455"
  "square_invoice" => "677"
  "other_work_" => array:3 [▶]
  "other_invoice_" => array:3 [▶]
  "comments" => "comments"
]

I have tried nested foreach loops and I always get an array to string conversion error when echoing out the values of the outer array. I will not show you every single attempt I have made, but this attempt below gives me all the outer array but then shows an error on the echo statement, which tells me it is failing when it hits the inner array.

foreach($invoice as $key=> $value)
{
    echo " Data $value  Key $key";   // Array to String conversion error here ! 

    if (is_array($value) || is_object($value))
    {
        foreach ($value as $sample=>$data)
        {
            echo "Key $sample Data $data";
        }
    }
}

How do I make this work ?

MANY THANKS !

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Vince
  • 1,405
  • 2
  • 20
  • 33

1 Answers1

1

You have this message, because some values are arrays, e.g. 'other work'.

One can iterate with:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr))

Iterating over a complex Associative Array in PHP

Get the key-value-pair and push it in the result array, like:

function getData($arr) 
{
    $res = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
    foreach($iterator as $key=>$value) {
        $pair = array($key, $value);
        array_push($res, $pair);
    }
    return $res;
}

$res = getData($arr); // All key-value-pairs are stored in $res
// Key: $res[n][0]
// Value: $res[n][1]

Full example:

$arr = [
  "_token" => "aHgDik0zuDxxBqYgbi0ZTkaPyG3CVwk5WO7LtU6u",
  "contractor_invoice_no" => "",
  "date" => "26-11-2015",
  "contractor_reference" => "sdfsdf",
  "workorder_reference" => "151 alandale",
  "square_installed" => "455",
  "square_invoice" => "677",
  "other_work_" => array(1, array('a', 'b', array('one' => 'path'))),
  "other_invoice_" => array(1, 2, 3),
  "comments" => "comments"
];

function getData($arr) 
{
    $res = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
    foreach($iterator as $key=>$value) {
        $pair = array($key, $value);
        array_push($res, $pair);
    }
    return $res;
}

$res = getData($arr); // All key-value-pairs are stored in $res
// Key: $res[n][0]
// Value: $res[n][1]

// Output the pairs
$max = count($res);
for($i = 0; $i < $max; $i++)
{
    echo "key: " . $res[$i][0] . ", value: " . $res[$i][1] . PHP_EOL;
}

Everything can be accessed through $res. $res[n][0] is the key and $res[n][1] is the corresponding value. This solution is a solution, if you have multidimensional arrays but you don't know the levels of them.


If you know that the array is a simple array, like

array('one', 'two', 'three');

you can use the implode method to make it into a string, like

$res = array();

foreach ($arr as $key => $value) {
    if(is_array($value))
    {
        $value = implode(" ", $value);
    }
    $pair = array($key, $value);
    array_push($res, $pair);
}

I hope this helps!

Community
  • 1
  • 1
  • thanks for that. Yes, I know about print_r however that does not help me do a bulk upload of my values into my DB. I need to get the values out of the inner array as a series of strings. – Vince Nov 26 '15 at 19:53
  • Many thanks ! I will have to study this as I am totally unfamiliar with this approach. – Vince Nov 27 '15 at 01:55
  • I've updated my answer, again. If the answer has helped you, feel free to accept and upvote my answer. –  Nov 27 '15 at 07:55
  • Hi Peter - thank you very much for the generosity of your time and hard work ! Yes, I will accept and upvote - cheers! – Vince Nov 27 '15 at 16:51