Jump to content

using foreach to access data


Go to solution Solved by requinix,

Recommended Posts

I am trying in vain to access the numbers in the "sims" array using the code below, I have tried various numbers of nested foreach loops but get errors every time, can anyone help point me in the correct direction please.

 

Thanks

 

Gordon

$stock = (array(1) 
	{ ["stock"]=> array(2) 
		{ [0]=> array(2) 
			{ ["operator"]=> string(3) "ECL" 
				["sims"]=> array(51) 
					{ 
					[0]=> int(8944122650438410000) 
					[1]=> int(8944122650438409000) 
					[2]=> int(8944122650438409000)
		 			} 
		 	} 
		[1]=> array(2) { 
			["operator"]=> string(2) "JT" 
				["sims"]=> NULL 
} } } )


foreach ($stock as $key1 => $item1)
            { 
                foreach($item1 as $key2 => $item2)
                { 
                		foreach($item2 as $key => $value)
                		{
                    		echo $value[sims] . "</br>";
                		}
            	}
			}
Link to comment
https://forums.phpfreaks.com/topic/292997-using-foreach-to-access-data/
Share on other sites

  • Solution

It's an important skill to be able to read dumps like this.

$stock = (array(1)
$stock is an array with one item.

{ ["stock"]=> array(2)
The key is "stock" and the value is another array, this time with two items.

{ [0]=> array(2)
The first key is 0 suggesting the parent array is something you should foreach over. The value is another array.

{ ["operator"]=> string(3) "ECL" 
  ["sims"]=> array(51)
In the array are two items, "operator" (a string) and "sims" (yet another array).

 

All together,

foreach ($stock["stock"] as $stock) {
    foreach ($stock["sims"] as $sim) {
  • Like 1

Thank you for not only taking the time to give me the correct code but also to provide some workings which I can use to understand the process.

 

It returns the values I want but for some reason I get an error at the bottom of the page: Warning: Invalid argument supplied for foreach() in .......

 

For line:   foreach ($stock["sims"] as $sim)

 

Any ideas?

    foreach ($stock["stock"] as $stock) 
    		{
    			foreach ($stock["sims"] as $sim)
    				{
                    echo $sim . "</br>";
            		}
			}

Most likely because the second item in the $stock array does not contain a sims array. You to check to see if $stock['sims'] is an array before looping through it with foreach

foreach ($stock["stock"] as $stock) 
{
    // check to see if there is a sims array for this item
    if(is_array($stock['sims']))
    {
        foreach ($stock["sims"] as $sim)
        {
            echo $sim . "</br>";
        }
    }
}
Edited by Ch0cu3r
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.