0

i have this array structure:

$ar = [product_info] => Array
                (
                    [pname] => Array
                        (
                            [0] => Выделенный сервер DE-22
                            [1] => Hello 4
                            [2] => Hello World
                        )

                    [pid] => Array
                        (
                            [0] => 217
                            [1] => 342
                            [2] => 343
                        )

i want to iterate it like this (in one loop):

foreach ($ar['product_info'] as $item) {
   echo $item['pname'];
   echo $item['pid']
}

How can i do this? need help

1
  • 2
    What do you want the output to be? Commented Nov 19, 2013 at 12:30

3 Answers 3

1

i think a normal for solve it.

$total = count($ar['product_info']['pid']);
for($i=0; $i<$total; $i++){
  echo $ar['product_info']['pname'][$i];
  echo $ar['product_info']['pid'][$i];
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

$ar = array(
    'product_info' => array(
        'pname' => array(
            'Product 1',
            'Product 2',
            'Product 3',
            'Product 4 > test <',
        ),
        'pid' => array(
            217,
            342,
            343,
            666,
        ),
    ),
);

foreach($ar['product_info']['pid'] as $productIndex => $productId)
{
    $productName = $ar['product_info']['pname'][$productIndex];
    echo '<a href="?product_id=' . intval($productId) . '">';
    echo htmlspecialchars($productName);
    echo '</a>';
    echo '<br />' . PHP_EOL;
}

Comments

0

You need to iterate over the inner arrays, which are int-indexed:

$size=sizeof($ar['product_info']['pname'];
for ($i=0;$i<$size;$i++) {
   echo $ar['product_info']['pname'][$i];
   echo $ar['product_info']['pid'][$i];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.