1

I'm trying to pull specific data from a PHP multidimensional array.

Array is like so:

Array
(
    [0] => Array
        (
            [0] => Item Code Number
            [1] => Item Name
            [2] => Item Description
            [3] => MSRP
            [4] => Cost
            [5] => Weight Pounds
            [6] => Weight Kgs
            [7] => Category 1
            [8] => Category 2
            [9] => Category 3
            [10] => Category 4
            [11] => Youtube Link
            [12] => Large Image URL
        )

    [1] => Array
        (
            [0] => 1
            [1] => Amazing Magic Set
            [2] => Easy to learn magic tricks
            [3] => 24.95
            [4] => 10.5
            [5] => 0.95
            [6] => 0.43
            [7] => New Items
            [8] => Tricks
            [9] => *New Tricks*
            [10] => All Tricks
            [11] => blank
            [12] => http://www.magicmakersinc.com/images/product/large/0051.jpg
        )
)

What I want to do it loop through the array and pull keys, 0, 1, 2, 4, 6 from each entry and display that data.

This is what I've tried to use:

$keys = (array_keys($csv));             
for ($row = 0; $row < sizeof($csv); $row++) {
    echo "<tr>";
        foreach($keys[$row] as $key => $value) {
            echo "<td>".$value."</td>";
        }
        echo "</tr>";
}

I can't get this to display anything.

I've also tried:

for($i = 0; $i < count($csv); $i++) {
    echo "<tr>";
    foreach($csv[$keys[$i]] as $key => $value) {
        echo "<td>".$value(0)."</td>";
    }
    echo "</tr>";
}

I can't figure out what I'm doing wrong. Any help would be great.

5 Answers 5

1

try

for ($row = 0; $row < sizeof($csv); $row++) {
echo "<tr>";
    foreach($csv[$row] as $key => $value) {
        if($k == '0' || $k == '1' || $k == '2' || $k == '4' || $k == '6'){
            echo "<td>".$value."</td>";
        }
    }
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also take advice for future - don't use multidimensinal arrays for purpose like that - because they should be used just for data which nature can be identified with multidimensional arrays (as calculations on matrices for example). For usage like that - use objects.
1

Try This code:

foreach ($row ar $val) {
    echo "<tr>";
        foreach($val as $k => $v) {
            if($k == '0' || $k == '1' || $k == '2' || $k == '4' || $k == '6'){
                echo "<td>".$td."</td>";
            }
        }
    echo "</tr>";
}

1 Comment

Do or do not, there is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. PLEASE go fix every one of your answers where you said "try this" without any explanation of what was done. Do it today.
0

Use two foreach:

foreach ($row ar $tr) {
    echo "<tr>";
        foreach($tr as $td) {
            echo "<td>".$td."</td>";
        }
    echo "</tr>";
}

2 Comments

If specific keys needed, add if to second foreach like: if(in_array($tr, [0, 1, 2, 4, 6])) echo ...
Using the if in_array is much cleaner than a huge OR block :)
0

assuming = have

$myArray = Array(
        [0] => Array
            (
                [0] => Item Code Number
                [1] => Item Name
                [2] => Item Description
                [3] => MSRP
                [4] => Cost
                [5] => Weight Pounds
                [6] => Weight Kgs
                [7] => Category 1
                [8] => Category 2
                [9] => Category 3
                [10] => Category 4
                [11] => Youtube Link
                [12] => Large Image URL
            )

        [1] => Array
            (
                [0] => 1
                [1] => Amazing Magic Set
                [2] => Easy to learn magic tricks
                [3] => 24.95
                [4] => 10.5
                [5] => 0.95
                [6] => 0.43
                [7] => New Items
                [8] => Tricks
                [9] => *New Tricks*
                [10] => All Tricks
                [11] => blank
                [12] => http://www.magicmakersinc.com/images/product/large/0051.jpg
            )
    )

You can see the content using a couple of foreach

  foreach($myArray as $key1 => $value1){

    foreach( $value1 as $key2 => $value2 ){
      echo  $key1 .  ' ' $key2 .'  value ' .$value2
    }

  }

or for specific access to 0, 1, 2, 4, 6

  foreach($myArray as $key1 => $value1){

      echo  $key1 .  ' ' . $value1[0]  . '</br>';
      echo  $key1 .  ' ' . $value1[1]  . '</br>';

      echo  $key1 .  ' ' . $value1[2]  . '</br>';
      echo  $key1 .  ' ' . $value1[4]  . '</br>';

      echo  $key1 .  ' ' . $value1[6]  . '</br>';


    }

  }

3 Comments

Where does this specifically show cells 0, 1, 2, 4, 6 ?
This outputs: 0 I 0 t 0 e 0 0 o I can't figure out why.
could be that your array is not like in sample
0

Something different approach, Combining array together 1st as key, 2nd as value.

//$data is contains your multidimensional array!
$combinedData = array_combine($data[0],$data[1]);

Now access.... Like!

echo $combinedData["Item Code Number"] ; // output 1
echo $combinedData["Item Name"]; //output Amazing Magic Set

//And so on....

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.