I_G
I_G

Reputation: 413

search key in multiple array in php

I have a multidimensionnal array

$lstTables = array();

$lstTables[] = array('tablesEGR1',array('1',array('Usa', '9350', '0.01','2015')));
$lstTables[] = array('tablesEGR1',array('1',array('France', '74', '0.01','2015')));
$lstTables[] = array('tablesEGR2',array('1',array('Italy', '74', '0.01','2015')));


Array
(
[0] => Array
    (
        [0] => tablesEGR1
        [1] => Array
            (
                [0] => 1
                [1] => Array
                    (
                        [0] => Usa
                        [1] => 9350
                        [2] => 0.01
                        [3] => 2015
                    )

            )

    )

[1] => Array
    (
        [0] => tablesEGR1
        [1] => Array
            (
                [0] => 1
                [1] => Array
                    (
                        [0] => France
                        [1] => 74
                        [2] => 0.01
                        [3] => 2015
                    )

            )

    )....

And what I want is to get some specifics value of the array. Like show me all the values where keys = "tablesEGR1" and "1".

I know its like two loops or more but I really dont know how to do it.

It should be something like

foreach($lstTables as $number_array){
foreach($number_array as $data)
    {
        print "Array number: $number_array, contains $data.  <br>";
    }
}

And the last question it is right how I insert the values in the array $lstTables. Is there a better way to store my data.

Upvotes: 2

Views: 61

Answers (1)

Rohan Khude
Rohan Khude

Reputation: 4893

The following shows the values in "tablesEGR1" and "1".

 $lstTables = array();

$lstTables[] = array('tablesEGR1',array('1',array('Usa', '9350', '0.01','2015')));
$lstTables[] = array('tablesEGR1',array('1',array('France', '74', '0.01','2015')));
$lstTables[] = array('tablesEGR2',array('1',array('Italy', '74', '0.01','2015')));

$count= count($lstTables);
foreach($lstTables as $key=>$value)
{   
    if($lstTables[$key][0]=="tablesEGR1" && $lstTables[$key][1][0]=="1")
    {
        foreach($lstTables[$key][1][1] as $k=>$v)
            echo $v."<br>";
    }
    echo "<p>";
}

How i accessed the value you can understand how to change value in above array

OUTPUT

Usa 9350 0.01 2015

France 74 0.01 2015

Upvotes: 2

Related Questions