0

Oke so I don't know why this is so hard, all information I find is only for two arrays like array_combine.

I have 3 arrays that I get from dynamically created input fields in a form, so now I want to retrieve the data and print it out like:

Item1 (array1)
Item1 (array2)
Item1 (array3)

Item2 (array1)
Item2 (array2)
Item2 (array3)

But with my code right now it completes one array and than goes to the next.

$article_id = $_REQUEST['article_id'];
$article_descr = $_REQUEST['article_descr'];
$article_ammount = $_REQUEST['article_amount'];

foreach($article_id as $artid) {
    echo = 'Article id: '.$artid.'<br>';
}

foreach($article_descr as $art_descr) {
    echo 'Article description: '.$art_descr.'<br>';
}

foreach($article_ammount as $art_amount) {
    echo 'Article ammount: '.$art_amount.'<br>';
}
4
  • if you only want one item per each array, why not just call them directly like $article_id['item1'] $article_descr['item1'] ... ? Commented Feb 23, 2016 at 15:18
  • 1
    do the arrays relate to each other by their keys? Commented Feb 23, 2016 at 15:20
  • Because they are from dynamically added element, so I can't know how many items there are Commented Feb 23, 2016 at 15:20
  • @CodeGodie yes the keys relate to each other Commented Feb 23, 2016 at 15:22

2 Answers 2

2

Since you said that all arrays match by their keys, I will assume you have something like this:

$article_ids = [10, 22, 13];
$article_descriptions = ["this is one", "this is two", "this is three"];
$article_amounts = [20, 10, 40];

Therefore in order to obtain their information in an orderly manner, you would first need to found how many elements there are. We can use the total of the first array, by using count(), then using a for loop to iterate and obtain each array's information.

//get the number of articles
$num = count($article_ids);

//iterate through each article count
for ($i = 0; $i < $num; $i++){
    echo 'Article id: '.$article_ids[$i].'<br>';
    echo 'Article description: '.$article_descriptions[$i].'<br>';
    echo 'Article amount: '.$article_amounts[$i] .'<br>';
}
Sign up to request clarification or add additional context in comments.

9 Comments

This can give incorrect result if IDs are not subsequent.
right, but OP mentioned they do relate to each other.
related does not means that they are subsequent
I see what you mean, but really it depends on what the incoming input is. Good point though.
Thanks @CodeGodie! it is exactly like you describe it
|
1

If you are sure that for each item information is under one and the same key in all arrays, you can do following:

$article_id = $_REQUEST['article_id'];
$article_descr = $_REQUEST['article_descr'];
$article_ammount = $_REQUEST['article_amount'];

foreach ($article_id as $id => $value) {
    echo 'Article id: ' . $value . '<br>';
    echo 'Article description: ' . $article_descr[$id] . '<br>';
    echo 'Article ammount: ' . $article_ammount[$id] . '<br>';
}

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.